Rudimentary protocol support, data parsing
authorbobbybee <admin@magnesiumbeta.com>
Mon, 29 Jul 2013 22:16:36 +0000 (18:16 -0400)
committerbobbybee <admin@magnesiumbeta.com>
Mon, 29 Jul 2013 22:16:36 +0000 (18:16 -0400)
Apollo.js
protocol.js [new file with mode: 0644]

index 011bd07..326c91e 100644 (file)
--- a/Apollo.js
+++ b/Apollo.js
@@ -3,6 +3,17 @@ var network = require('./network'); // network module is a wrapper around the
 
 var config = require('./config'); // configuration loader
 
+var protocol = require('./protocol'); // provides methods for handling the Apollo protocol
+
 network.createServer(function(conn){
-       console.log("Client Connected");
+       console.log("Client connected");
+
+       conn.on('data', function(d){
+               var message = protocol.parsePacket(d); // parse the message
+               console.log(message); // dump the message
+       });
+
+       conn.on('end', function(){
+               console.log("Client disconnected");
+       });
 }).listen(config.port, config.host);
diff --git a/protocol.js b/protocol.js
new file mode 100644 (file)
index 0000000..35830ea
--- /dev/null
@@ -0,0 +1,38 @@
+// protocol.js
+// defines the Apollo messaging protocol
+
+// currently, as of version 0.0.1, Apollo uses an extremely verbose protocol
+// packets are stored in their literal JSON form
+// compressed or full binary packets are planned for future
+
+// parsePacket
+// parses a given packet
+// arguments: packet
+// packet: string representation of packet
+// return value:
+// object form of packet
+
+function parsePacket(packet){
+       try{
+               return JSON.parse(packet);
+       } catch(e){
+               console.log("Encountered error parsing packet");
+               console.log(e);
+               return {"type":"error", "message":e};
+       }       
+}
+
+// generatePacket
+// generates a string represetnation of a packet
+// arguments: obj
+// obj: object represting packet
+// return value:
+// string form of packet
+function generatePacket(obj){
+       return JSON.stringify(obj);
+}
+
+
+// deal with module exports
+module.exports.parsePacket = parsePacket;
+module.exports.generatePacket = generatePacket;
This page took 0.04434 seconds and 4 git commands to generate.