aboutsummaryrefslogtreecommitdiff
path: root/parse.h
diff options
context:
space:
mode:
authorBen Burwell <bburwell1@gmail.com>2013-04-29 16:13:34 -0400
committerBen Burwell <bburwell1@gmail.com>2013-04-29 16:13:34 -0400
commitca9e46c937e75df43d2f80f0d957fabb07892c29 (patch)
tree0d8ae2bc1d1e3e248c746586a5daf7cb58a79463 /parse.h
Init
Diffstat (limited to 'parse.h')
-rw-r--r--parse.h71
1 files changed, 71 insertions, 0 deletions
diff --git a/parse.h b/parse.h
new file mode 100644
index 0000000..f2d97e8
--- /dev/null
+++ b/parse.h
@@ -0,0 +1,71 @@
+/**
+ * Localchat
+ * Ben Burwell
+ *
+ * Parse received command packets
+ */
+
+void parse_command(char packet[256]) {
+
+ char separators[4] = ":";
+ char *type;
+ char *peer_username;
+ char *peer_ip;
+ char *peer_in_chat;
+ int updated_peer;
+ int i;
+
+ type = strtok(packet, separators);
+
+ if (strcmp(type, "ONLINE") == 0) {
+
+ // send a status broadcast
+ status_broadcast_once();
+
+ } else if (strcmp(type, "STATUS") == 0) {
+
+ // lock the peers
+ pthread_mutex_lock(&peer_table_lock);
+
+ // get username from packet
+ peer_username = strtok(NULL, separators);
+ peer_ip = strtok(NULL, separators);
+ peer_in_chat = strtok(NULL, separators);
+
+ // so we know whether to add a peer later
+ updated_peer = 0;
+
+ for (i = 0; i < num_peers_in_table; i++) {
+ if (strcmp(peer_username, peers[i].username) == 0) {
+
+ // found a matching username, update the peer
+ strcpy(peers[i].in_chat, peer_in_chat);
+ time( &(peers[i].last_seen) );
+
+ // we updated the peers, so we won't add it later
+ updated_peer = 1;
+
+ }
+ }
+
+ if (!updated_peer) {
+ // we didn't update a peer, so we need to add it to the table
+
+ strcpy(peers[num_peers_in_table].username, peer_username);
+ strcpy(peers[num_peers_in_table].ip, peer_ip);
+ strcpy(peers[num_peers_in_table].in_chat, peer_in_chat);
+ time( &(peers[num_peers_in_table].last_seen) );
+
+ num_peers_in_table++;
+
+ //printf("*** %s has come online. \n", peer_username);
+
+ }
+
+ pthread_mutex_unlock(&peer_table_lock);
+
+ } else {
+ if (DEBUG) printf("Unknown message type: %s \n", type);
+ }
+
+} \ No newline at end of file