Revision 618
added some comments
trunk/code/projects/colonet/server/colonet_wireless.cpp | ||
---|---|---|
50 | 50 |
static int log_packet(unsigned char* packet, int len); |
51 | 51 |
|
52 | 52 |
/**************************** Public functions *******************************/ |
53 |
/** |
|
54 |
* @brief Initializes the wireless library |
|
55 |
* |
|
56 |
* @param wl_port_ The port to listen for wireless messages on |
|
57 |
* @param log_filename_ The name of the log file |
|
58 |
* |
|
59 |
* @return 0 on success, negative error code on failure |
|
60 |
*/ |
|
53 | 61 |
int colonet_wl_init(char* wl_port_, char* log_filename_) { |
54 | 62 |
if (log_filename_ != NULL) { |
55 | 63 |
logging_enabled = true; |
... | ... | |
78 | 86 |
return 0; |
79 | 87 |
} |
80 | 88 |
|
89 |
/** |
|
90 |
* @brief This function will kill the thread that is listening for wireless messages |
|
91 |
*/ |
|
81 | 92 |
void colonet_wl_kill_listener_thread() { |
82 | 93 |
pthread_kill(listener_thread, 9); |
83 | 94 |
} |
84 | 95 |
|
96 |
/** |
|
97 |
* @brief This function spawns a thread to listen for wireless messages |
|
98 |
* |
|
99 |
* @return 0 on success, negative error code on failure |
|
100 |
*/ |
|
85 | 101 |
int colonet_wl_run_listener_thread() { |
86 | 102 |
dbg_printf("Spawning listener thread...\n"); |
87 | 103 |
|
... | ... | |
93 | 109 |
return 0; |
94 | 110 |
} |
95 | 111 |
|
112 |
/** |
|
113 |
* @brief This function sends a message out on the wireless |
|
114 |
* |
|
115 |
* @param client_source The index in the connection pool of the client that is sending the message |
|
116 |
* @param dest The robot that is meant to receive this message |
|
117 |
* @param msg_type The type of the message |
|
118 |
* @param msg_code The message code |
|
119 |
* @param args The arguments to send with the message |
|
120 |
* |
|
121 |
* @return 0 on success, a negative error code on failure |
|
122 |
*/ |
|
96 | 123 |
int colonet_wl_send(short client_source, short dest, ColonetRobotMessageType msg_type, unsigned char msg_code, |
97 | 124 |
unsigned char* args) { |
98 | 125 |
//printf("colonet_wl_send: client_source:%d, dest:%d, msg_code:%d\n", client_source, dest, msg_code); |
99 | 126 |
|
127 |
//create a new packet to be sent |
|
100 | 128 |
ColonetRobotServerPacket pkt; |
129 |
|
|
130 |
//set up the packet |
|
131 |
|
|
101 | 132 |
pkt.client_id = client_source; |
102 | 133 |
pkt.msg_code = msg_code; |
103 | 134 |
|
135 |
//copy over the args into the data portion of the packet |
|
104 | 136 |
for (int i = 0; i < PACKET_DATA_LEN; i++) { |
105 | 137 |
//printf("data %i: %u\n", i, args[i]); |
106 | 138 |
pkt.data[i] = args[i]; |
... | ... | |
123 | 155 |
return 0; |
124 | 156 |
} |
125 | 157 |
|
158 |
/** |
|
159 |
* @brief Used to get the number of robots in the token ring |
|
160 |
* |
|
161 |
* @return The result of wl_token_get_num_robots() |
|
162 |
*/ |
|
126 | 163 |
int colonet_get_num_robots(void) { |
127 | 164 |
return wl_token_get_num_robots(); |
128 | 165 |
} |
129 | 166 |
|
167 |
/** |
|
168 |
* @brief Gets a list of the xbee ids of the ones in the token ring |
|
169 |
* |
|
170 |
* @param numrobots A pointer to the variable where you want the number of robots to be stored |
|
171 |
* |
|
172 |
* @return A dynamically allocated piece of memory that contains the list of xbee ids. |
|
173 |
* The function calling this function must free this memory when done with it. |
|
174 |
*/ |
|
130 | 175 |
int* colonet_get_xbee_ids(int* numrobots) { |
131 | 176 |
int num_robots = wl_token_get_num_robots(); |
132 | 177 |
int* ids = (int*)malloc(num_robots * sizeof(int)); |
... | ... | |
143 | 188 |
return ids; |
144 | 189 |
} |
145 | 190 |
|
146 |
// Returns int**; should be freed |
|
191 |
/** |
|
192 |
* @brief Gets the sensor matrix from the wireless library |
|
193 |
* |
|
194 |
* @param numrobots A pointer to the variable where you want the number of robots to be stored |
|
195 |
* @param ids_ A pointer to a pointer that you want to point to the list of xbee ids. This memory is dynamically allocated and should be freed by the calling process when it is done with it |
|
196 |
* |
|
197 |
* @return The 2d matrix reprsenting the sensor matrix. This memory is dynamically allocated and should be freed by the calling process when it is done with it |
|
198 |
*/ |
|
147 | 199 |
int** colonet_get_sensor_matrix(int* numrobots, int** ids_) { |
148 | 200 |
int num_robots; |
149 | 201 |
int* ids = colonet_get_xbee_ids(&num_robots); |
... | ... | |
189 | 241 |
log_packet(data, len); |
190 | 242 |
} |
191 | 243 |
|
244 |
//see if a robot wants the server to tell it where it is |
|
192 | 245 |
if (pkt->msg_code == ROBOT_REQUEST_POSITION_FROM_SERVER) { |
193 | 246 |
/* Robot has requested its position. */ |
194 | 247 |
int robot_x, robot_y; |
195 | 248 |
PositionMonitor* pm = colonet_server.getPositionMonitor(); |
196 | 249 |
int num_robots = pm->getNumVisibleRobots(); |
250 |
//ask the position monitor for where that robot is. |
|
197 | 251 |
int ret = pm->getRobotPosition(source, &robot_x, &robot_y); |
198 | 252 |
if (ret != 0 && num_robots != 1) { |
199 | 253 |
fprintf(stderr, "Robot %d requested position, but its position is not known.\n", source); |
trunk/code/projects/colonet/server/ColonetServer.cpp | ||
---|---|---|
91 | 91 |
return 0; |
92 | 92 |
} |
93 | 93 |
|
94 |
/** |
|
95 |
* @brief The function that is run in the new pthread in order to do the position monitoring |
|
96 |
* |
|
97 |
* @param arg the argument to give to the new thread |
|
98 |
*/ |
|
94 | 99 |
void* run_thread(void* arg) { |
95 | 100 |
PositionMonitor* p = (PositionMonitor*) arg; |
96 | 101 |
p->run(); |
97 | 102 |
return NULL; |
98 | 103 |
} |
99 | 104 |
|
105 |
/** |
|
106 |
* @brief This function spawns a new thread to do the position monitoring based off data |
|
107 |
* retrieved from the library that parses the image from the camera |
|
108 |
*/ |
|
100 | 109 |
int ColonetServer::run_position_monitor() { |
101 | 110 |
pthread_t posmon_thread; |
102 | 111 |
pthread_create(&posmon_thread, NULL, run_thread, &position_monitor); |
... | ... | |
108 | 117 |
* @brief Starts the server running (starts an infinite loop) |
109 | 118 |
*/ |
110 | 119 |
int ColonetServer::run_server() { |
120 |
//add the socket that you will listen to connections on to the connection pool |
|
111 | 121 |
connection_pool->add_new_socket_to_pool(listen_socket); |
112 | 122 |
|
113 | 123 |
int accept_socket = 0; |
... | ... | |
119 | 129 |
} |
120 | 130 |
|
121 | 131 |
while(1) { |
132 |
//sleep for a little bit so this program doesn't hog cpu |
|
122 | 133 |
usleep(10000); |
123 | 134 |
|
135 |
//tell the connection pool to look at the file descriptors and see which ones are ready |
|
124 | 136 |
connection_pool->perform_select(listen_socket); |
125 | 137 |
|
126 | 138 |
//either no descriptors are ready or there was an error |
... | ... | |
128 | 140 |
continue; |
129 | 141 |
} |
130 | 142 |
|
143 |
//check to see if a new client is trying to connect |
|
131 | 144 |
if (connection_pool->is_socket_ready_to_read(listen_socket)) { |
132 | 145 |
printf("Something is trying to connect...\n"); |
146 |
//accept the connection |
|
133 | 147 |
if ((accept_socket = accept(listen_socket, (struct sockaddr*) &client_addr, &client_addr_size)) < 0) { |
134 | 148 |
if (errno == EMFILE) { |
135 | 149 |
printf("\tWhen attempting to accept a connection, reached the per process limit of file descriptors." |
136 |
" Dropping the new connection.\n");
|
|
150 |
" Dropping the new connection.\n");
|
|
137 | 151 |
continue; |
138 | 152 |
} else { |
139 | 153 |
printf("\tThere was an error when attempting to accept a connection"); |
... | ... | |
144 | 158 |
if (optionsG.logging_enabled) { |
145 | 159 |
char log_buffer[LOG_BUFFER_LENGTH]; |
146 | 160 |
snprintf(log_buffer, LOG_BUFFER_LENGTH, "Client at address %s attempting to connect.", |
147 |
inet_ntoa(client_addr.sin_addr));
|
|
161 |
inet_ntoa(client_addr.sin_addr));
|
|
148 | 162 |
logger->log_string(LOG_TYPE_CONNECT, log_buffer); |
149 | 163 |
} |
150 | 164 |
|
165 |
//add the new client to the connection pool |
|
151 | 166 |
if (connection_pool->add_client(accept_socket) < 0) { |
152 | 167 |
printf("\tThere was an error when trying to add a client to the connection pool."); |
153 | 168 |
continue; |
... | ... | |
161 | 176 |
} |
162 | 177 |
} |
163 | 178 |
|
179 |
//check all the clients in the connection pool |
|
164 | 180 |
if (connection_pool->check_clients() < 0) { |
165 | 181 |
printf("\tThere was an error trying to update the clients."); |
166 | 182 |
continue; |
... | ... | |
171 | 187 |
} |
172 | 188 |
|
173 | 189 |
/** |
174 |
* @param source - ID of robot that message is from. |
|
175 |
* @param dest - ID of internet client to send message to. |
|
176 |
* @param data - Data to send to internet client. |
|
177 |
* @param len - Length of the data param. |
|
178 |
*/ |
|
190 |
* @brief when the server receives a new wireless message, it calls this function to process it |
|
191 |
* |
|
192 |
* @param source - ID of robot that message is from. |
|
193 |
* @param dest - ID of internet client to send message to. |
|
194 |
* @param data - Data to send to internet client. |
|
195 |
* @param len - Length of the data param. |
|
196 |
*/ |
|
179 | 197 |
int ColonetServer::process_received_wireless_message(int dest, char* data, int len) { |
198 |
//try to pass the robot's response onto the client it wanted to respond to |
|
180 | 199 |
if (connection_pool->write_to_client(dest, data, len) == ERROR_INVALID_CLIENT_ID) { |
181 | 200 |
printf("The robot wanted to pass the data to a client not in the pool.\n"); |
182 | 201 |
return -1; |
... | ... | |
200 | 219 |
printf("Logging disabled.\n"); |
201 | 220 |
} |
202 | 221 |
|
222 |
//initialize the wireless library giving it the port and the log file name |
|
203 | 223 |
if (colonet_wl_init(optionsG.wireless_port, log_filename) != 0) { |
204 | 224 |
fprintf(stderr, "ERROR - colonet_wl_init failed.\n"); |
205 | 225 |
return -1; |
206 | 226 |
} |
207 | 227 |
|
228 |
//call the function that will start the thread that will listen for wireless messages |
|
208 | 229 |
if (colonet_wl_run_listener_thread()) { |
209 | 230 |
fprintf(stderr, "%s: colonet_wl_run_listener_thread failed.\n", __FUNCTION__); |
210 | 231 |
return -1; |
... | ... | |
261 | 282 |
return 0; |
262 | 283 |
} |
263 | 284 |
|
285 |
/** |
|
286 |
* @brief returns the position monitor |
|
287 |
*/ |
|
264 | 288 |
PositionMonitor* ColonetServer::getPositionMonitor() { |
265 | 289 |
return &position_monitor; |
266 | 290 |
} |
Also available in: Unified diff