root / trunk / code / projects / colonet / ColonetServer / client.cpp @ 20
History | View | Annotate | Download (2.03 KB)
1 |
/** @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 |
int len = strlen(buffer);
|
49 |
connection_pool->write_to_client(dest, buffer, len); |
50 |
printf("Put data in write buffer for client.\n");
|
51 |
} |
52 |
else
|
53 |
{ |
54 |
printf("The robot wanted to pass the data to a client not in our pool.\n");
|
55 |
return -1; |
56 |
} |
57 |
} |
58 |
else
|
59 |
{ |
60 |
printf("not a response\n");
|
61 |
} |
62 |
|
63 |
pkt = 0;
|
64 |
return 0; |
65 |
} |
66 |
|
67 |
int initWireless()
|
68 |
{ |
69 |
char* log_filename = NULL; |
70 |
|
71 |
if (optionsG.logging_enabled) {
|
72 |
log_filename = optionsG.log_filename; |
73 |
} |
74 |
|
75 |
wireless = new ColonetWireless(optionsG.wireless_port,
|
76 |
wirelessMessageHandler, log_filename, |
77 |
/*!optionsG.listener_mode*/false, true); |
78 |
//Note: last arg set to true ignores token ring; in general, this should
|
79 |
//probably be false (changed for demo purposes)
|
80 |
|
81 |
if (!wireless->run_listener_thread()) {
|
82 |
return -1; |
83 |
} |
84 |
|
85 |
return 0; |
86 |
} |
87 |
|
88 |
void send_wireless_message(unsigned char msg_dest, unsigned char msg_type, |
89 |
unsigned char msg_code, unsigned char* data) { |
90 |
wireless->send(msg_dest, msg_type, msg_code, data); |
91 |
} |