diff --git a/.idea/CapstoneServer.iml b/.idea/CapstoneServer.iml new file mode 100644 index 0000000000000000000000000000000000000000..ef582b1af949d87db636dc6bd9627d7d984d8a20 --- /dev/null +++ b/.idea/CapstoneServer.iml @@ -0,0 +1,10 @@ +<?xml version="1.0" encoding="UTF-8"?> +<module type="JAVA_MODULE" version="4"> + <component name="NewModuleRootManager" inherit-compiler-output="true"> + <exclude-output /> + <content url="file://$MODULE_DIR$" /> + <orderEntry type="inheritedJdk" /> + <orderEntry type="sourceFolder" forTests="false" /> + </component> +</module> + diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000000000000000000000000000000000000..217af471a9e60576e8c36373fa22c50a63a00b2c --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,23 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="CompilerConfiguration"> + <option name="DEFAULT_COMPILER" value="Javac" /> + <resourceExtensions /> + <wildcardResourcePatterns> + <entry name="!?*.java" /> + <entry name="!?*.form" /> + <entry name="!?*.class" /> + <entry name="!?*.groovy" /> + <entry name="!?*.scala" /> + <entry name="!?*.flex" /> + <entry name="!?*.kt" /> + <entry name="!?*.clj" /> + </wildcardResourcePatterns> + <annotationProcessing> + <profile default="true" name="Default" enabled="false"> + <processorPath useClasspath="true" /> + </profile> + </annotationProcessing> + </component> +</project> + diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000000000000000000000000000000000000..e7bedf3377d40335424fd605124d4761390218bb --- /dev/null +++ b/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ +<component name="CopyrightManager"> + <settings default="" /> +</component> \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000000000000000000000000000000000000..e206d70d8595e2a50675ba11de48efcfa012497d --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,5 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="Encoding" useUTFGuessing="true" native2AsciiForPropertiesFiles="false" /> +</project> + diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000000000000000000000000000000000000..830de86f31c6a7214cf2d02fd23a1899eac9e9b1 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,21 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="ProjectRootManager" version="2" languageLevel="JDK_1_6" assert-keyword="true" jdk-15="true" /> + <component name="masterDetails"> + <states> + <state key="ProjectJDKs.UI"> + <settings> + <last-edited>1.8</last-edited> + <splitter-proportions> + <option name="proportions"> + <list> + <option value="0.2" /> + </list> + </option> + </splitter-proportions> + </settings> + </state> + </states> + </component> +</project> + diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000000000000000000000000000000000000..4e22ebd352fd05edac2aaed8c1919868616d0087 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,9 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="ProjectModuleManager"> + <modules> + <module fileurl="file://$PROJECT_DIR$/.idea/CapstoneServer.iml" filepath="$PROJECT_DIR$/.idea/CapstoneServer.iml" /> + </modules> + </component> +</project> + diff --git a/.idea/scopes/scope_settings.xml b/.idea/scopes/scope_settings.xml new file mode 100644 index 0000000000000000000000000000000000000000..922003b8433bcad6ce9778a37628d738faa26389 --- /dev/null +++ b/.idea/scopes/scope_settings.xml @@ -0,0 +1,5 @@ +<component name="DependencyValidationManager"> + <state> + <option name="SKIP_IMPORT_STATEMENTS" value="false" /> + </state> +</component> \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000000000000000000000000000000000000..275077f82558f02f5c5151cc5c676b9f5f60dce3 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,7 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="VcsDirectoryMappings"> + <mapping directory="" vcs="Git" /> + </component> +</project> + diff --git a/server.js b/server.js index f84f1fe55a69d449f9408ec651f2575086da74f6..87a0bb22a2c02094527f5550f00342cff299858d 100644 --- a/server.js +++ b/server.js @@ -4,11 +4,23 @@ var net = require('net'); +var PROTOCOL = { + HEADER_LENGTH: 5, // 5 bytes + HEADER_TYPE_FIELD: 1, + HEADER_CONTENT_FIELD: 4, + CONTENT_CLIENT_LENGTH: 4, // Length of a single client in connected clients response. + CONTENT_CLIENTS: 1, + CONTENT_CONNECTING: 2, + CONTENT_OBJECT: 3, + CONTENT_CONNECTED: 4 +}; + function Proxy( port ) { this.PORT = port; }; Proxy.prototype.clientPool = {}; +Proxy.prototype.rooms = {}; Proxy.prototype.createProxyServer = function() { var self = this; @@ -30,7 +42,55 @@ Proxy.prototype.clientConnected = function( client ) { Proxy.prototype.clientPool[clientAddress] = client; client.on( 'data', function( data ) { console.log('Recieved [data] from ['+client.formattedAddress+']: ' + data.length + ' bytes'); - Proxy.prototype.emit( client, data ); + // Parse the data recieved. + var content_type = data.readInt8( 0 ); + var content_length = data.readInt32BE( 1 ); + switch ( content_type ) { + case PROTOCOL.CONTENT_CLIENTS: + console.log('\tRecieved content[CLIENTS]'); + // Send back a list of clients that are not currently connected to someone. + var unconnectedClients = ""; + for (var i = 0; i < Proxy.prototye.clientPool.length; i++) { + var potentialClient = Proxy.prototye.clientPool[i]; + // TODO: check that this client is yourself + if ( potentialClient.connectedTo == undefined ) + unconnectedClients += "\t" + potentialClient.formattedAddress; + }; + console.log('\tResponse: ', unconnectedClients); + var response = new Buffer( PROTOCOL.HEADER_LENGTH + unconnectedClients.length ); + response.write( PROTOCOL.CONTENT_CLIENTS, 0, PROTOCOL.HEADER_TYPE_FIELD ); + response.write( unconnectedClients.length, PROTOCOL.HEADER_TYPE_FIELD, PROTOCOL.HEADER_TYPE_FIELD + PROTOCOL.HEADER_CONTENT_FIELD ); + response.write( unconnectedClients ); + client.write( response ); + break; + case PROTOCOL.CONTENT_CONNECTING: + console.log('\tRecieved content[CONNECTING]'); + // TODO: cant use substring, need to use as buffer. + var targetClientAddress = data.substring( PROTOCOL.HEADER_LENGTH, PROTOCOL.HEADER_LENGTH + content_length ); + var targetClient = Proxy.prototype.clientPool[ targetClientAddress ]; + if ( targetClient && targetClient.connectedTo == client.connectedTo ) { + // We have successfully connected, send both parties the CONNECTED response + + } else { + // This client is trying to connect to targetClient, but targetClient has not yet connected. + // Send targetClient notification that this client is trying to connect. + + } + break; + case PROTOCOL.CONTENT_CONNECTED: + console.log('\tRecieved content[CONNECTED]'); + break; + case PROTOCOL.CONTENT_OBJECT: + console.log('\tRecieved content[OBJECT]'); + default: + // For now lets just default to the object data. + if ( client.connectedTo ) { + var partnerClient = Proxy.prototype.clientPool[ client.connectedTo ]; + partnerClient.write( data ); + } else { + console.log("ERROR: tried to write data to partner, but partner did not exist."); + } + } }); client.on( 'end', function() { console.log('Recieved [end] from ['+client.formattedAddress+'].'); @@ -39,6 +99,7 @@ Proxy.prototype.clientConnected = function( client ) { client.on( 'close', function() { console.log('Recieved [closed] from ['+client.formattedAddress+'].'); Proxy.prototype.removeClient( client ); + // TODO: if the client was connected to someone, notify them? }); client.on( 'error', function( err ) {