Skip to content
Snippets Groups Projects
Commit 0b885c08 authored by Neil's avatar Neil
Browse files

started on server modifications to recieve several requests/responses

parent 9eac90c3
No related branches found
No related tags found
No related merge requests found
<?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>
<?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>
<component name="CopyrightManager">
<settings default="" />
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" useUTFGuessing="true" native2AsciiForPropertiesFiles="false" />
</project>
<?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>
<?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>
<component name="DependencyValidationManager">
<state>
<option name="SKIP_IMPORT_STATEMENTS" value="false" />
</state>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>
...@@ -4,11 +4,23 @@ ...@@ -4,11 +4,23 @@
var net = require('net'); 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 ) { function Proxy( port ) {
this.PORT = port; this.PORT = port;
}; };
Proxy.prototype.clientPool = {}; Proxy.prototype.clientPool = {};
Proxy.prototype.rooms = {};
Proxy.prototype.createProxyServer = function() { Proxy.prototype.createProxyServer = function() {
var self = this; var self = this;
...@@ -30,7 +42,55 @@ Proxy.prototype.clientConnected = function( client ) { ...@@ -30,7 +42,55 @@ Proxy.prototype.clientConnected = function( client ) {
Proxy.prototype.clientPool[clientAddress] = client; Proxy.prototype.clientPool[clientAddress] = client;
client.on( 'data', function( data ) { client.on( 'data', function( data ) {
console.log('Recieved [data] from ['+client.formattedAddress+']: ' + data.length + ' bytes'); 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() { client.on( 'end', function() {
console.log('Recieved [end] from ['+client.formattedAddress+'].'); console.log('Recieved [end] from ['+client.formattedAddress+'].');
...@@ -39,6 +99,7 @@ Proxy.prototype.clientConnected = function( client ) { ...@@ -39,6 +99,7 @@ Proxy.prototype.clientConnected = function( client ) {
client.on( 'close', function() { client.on( 'close', function() {
console.log('Recieved [closed] from ['+client.formattedAddress+'].'); console.log('Recieved [closed] from ['+client.formattedAddress+'].');
Proxy.prototype.removeClient( client ); Proxy.prototype.removeClient( client );
// TODO: if the client was connected to someone, notify them?
}); });
client.on( 'error', function( err ) { client.on( 'error', function( err ) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment