root / trunk / code / projects / colonet / ColonetServer / client.cpp @ 11
History | View | Annotate | Download (2.37 KB)
1 | 11 | emarinel | /** @file client.c
|
---|---|---|---|
2 | *
|
||
3 | * @author Jason Knichel
|
||
4 | * @author Eugene Marinelli
|
||
5 | *
|
||
6 | * @bug Weird segfault when processing tokens
|
||
7 | */
|
||
8 | |||
9 | #include <string.h> |
||
10 | #include <stdio.h> |
||
11 | #include <unistd.h> |
||
12 | #include <ctype.h> |
||
13 | #include <errno.h> |
||
14 | |||
15 | #include <colonet_wireless.h> |
||
16 | |||
17 | #include "includes/client.h" |
||
18 | #include "includes/options.h" |
||
19 | #include "includes/ConnectionPool.h" |
||
20 | |||
21 | /* Globals */
|
||
22 | static ColonetWireless* wireless;
|
||
23 | extern ConnectionPool connection_pool;
|
||
24 | |||
25 | |||
26 | /* Public functions */
|
||
27 | int wirelessMessageHandler(ColonetPacket* pkt)
|
||
28 | { |
||
29 | printf("Received wireless message!!!\n");
|
||
30 | if (!pkt)
|
||
31 | { |
||
32 | printf("The packet pointer was null.\n");
|
||
33 | return -1; |
||
34 | } |
||
35 | |||
36 | if ((int)pkt->msg_type == COLONET_RESPONSE) |
||
37 | { |
||
38 | printf("response\n");
|
||
39 | |||
40 | int dest = (int)(pkt->msg_dest); |
||
41 | if (dest < connection_pool.get_next_available_slot())
|
||
42 | { |
||
43 | char buffer[WRITE_BUFFER_SIZE];
|
||
44 | |||
45 | int value = (int)(pkt->data[0]); |
||
46 | snprintf(buffer, WRITE_BUFFER_SIZE, "%d\n", value);
|
||
47 | |||
48 | //TODO:make a connection pool write function and move this code into it
|
||
49 | int len = strlen(buffer);
|
||
50 | if (len > (WRITE_BUFFER_SIZE-connection_pool.get_write_buffer_size(dest)))
|
||
51 | { |
||
52 | printf("There is not enough room in the write buffer to send the data to the client.\n");
|
||
53 | return -1; |
||
54 | } |
||
55 | |||
56 | memcpy(connection_pool.get_write_buffer(dest), buffer, len); |
||
57 | connection_pool.set_write_buffer_size(dest, len); |
||
58 | printf("Put data in write buffer for client.\n");
|
||
59 | } |
||
60 | else
|
||
61 | { |
||
62 | printf("The robot wanted to pass the data to a client not in our pool.\n");
|
||
63 | return -1; |
||
64 | } |
||
65 | } |
||
66 | else
|
||
67 | { |
||
68 | printf("not a response\n");
|
||
69 | } |
||
70 | |||
71 | pkt = 0;
|
||
72 | return 0; |
||
73 | } |
||
74 | |||
75 | int initWireless()
|
||
76 | { |
||
77 | char* log_filename = NULL; |
||
78 | |||
79 | if (optionsG.logging_enabled) {
|
||
80 | log_filename = optionsG.log_filename; |
||
81 | } |
||
82 | |||
83 | wireless = new ColonetWireless(optionsG.wireless_port,
|
||
84 | wirelessMessageHandler, log_filename, |
||
85 | /*!optionsG.listener_mode*/false, true); |
||
86 | //Note: last arg set to true ignores token ring; in general, this should
|
||
87 | //probably be false (changed for demo purposes)
|
||
88 | |||
89 | if (!wireless->run_listener_thread()) {
|
||
90 | return -1; |
||
91 | } |
||
92 | |||
93 | return 0; |
||
94 | } |
||
95 | |||
96 | void send_wireless_message(unsigned char msg_dest, unsigned char msg_type, |
||
97 | unsigned char msg_code, unsigned char* data) { |
||
98 | wireless->send(msg_dest, msg_type, msg_code, data); |
||
99 | } |