Nikolay Suslov 5 rokov pred
rodič
commit
9775d29646
4 zmenil súbory, kde vykonal 49 pridanie a 122 odobranie
  1. 0 106
      lib/helpers.js
  2. 43 11
      lib/reflector.js
  3. 3 2
      node_vwf.js
  4. 3 3
      package.json

+ 0 - 106
lib/helpers.js

@@ -1,106 +0,0 @@
-//   helpers.js
-//   This file contains some low level helper functions for the VWF nodeJS server.
-
-var libpath = require( 'path' ),
-    fs = require( 'fs' );
-
-    
-// List of valid ID characters for use in an instance.
-var ValidIDChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
-
-// IsInstanceID tests if the passed in potential Instance ID 
-// is a valid instance id.
-function IsInstanceID( potentialInstanceID ) {
-    if ( potentialInstanceID.match(/^[0-9A-Za-z]{16}$/) ) {
-        return true;
-    }
-    return false;
-}
-
-// GenerateInstanceID function creates a randomly generated instance ID.
-function GenerateInstanceID( ) {
-    var text = "";
-    
-    for( var i=0; i < 16; i++ )
-        text += ValidIDChars.charAt( Math.floor( Math.random( ) * ValidIDChars.length ) );
-
-    return text;
-}
-
-// JoinPath
-// Takes multiple arguments, joins them together into one path.
-function JoinPath( /* arguments */ ) {
-    var result = "";
-    if ( arguments.length > 0 ) {
-        if ( arguments[ 0 ] ) {
-            result = arguments[ 0 ];
-        }
-        for ( var index = 1; index < arguments.length; index++ ) {
-            var newSegment = arguments[ index ];
-            if ( newSegment == undefined ) {
-                newSegment = "";
-            }
-
-            if ( ( newSegment[ 0 ] == "/" ) && ( result[ result.length - 1 ] == "/" ) ) {
-                result = result + newSegment.slice( 1 );
-            } else if ( ( newSegment[ 0 ] == "/" ) || ( result[ result.length - 1 ] == "/" ) ) {
-                result = result + newSegment;
-            } else {
-                result = result + "/" + newSegment;
-            }
-            //result = libpath.join( result, newSegment );
-        }
-    }
-    return result;
-}
-
-// IsDirectory tests if the passed in path exists, and if it is a directory.
-function IsDirectory( path ) {
-    // var seperatorFixedPath = path.replace( /\//g, libpath.sep );
-    // if ( ! fs.existsSync( seperatorFixedPath ) ) {
-    //     return false;
-    // }
-    // return fs.statSync( seperatorFixedPath ).isDirectory();
-    return true
-}
-
-
-// IsFile tests if the passed in path exists, and if it is a file.
-function IsFile( path ) {
-    // var seperatorFixedPath = path.replace( /\//g, libpath.sep );
-    // if ( ! fs.existsSync( seperatorFixedPath ) ) {
-    //     return false;
-    // }
-    // return fs.statSync( seperatorFixedPath ).isFile();
-
-    return true
-
-}
-
-
-// GenerateSegments takes a string, breaks it into
-// '/' separated segments, and removes potential
-// blank first and last segments. 
-function GenerateSegments( argument ) {
-    var result = argument.split("/");
-    if ( result.length > 0 ) {
-        if ( result[ 0 ] == "" ) {
-            result.shift();
-        }
-    }
-    if ( result.length > 0 ) {
-        if ( result[ result.length - 1 ] == "" ) {
-            result.pop();
-        }
-    }
-    return result;
-}
-
-
-
-exports.JoinPath = JoinPath;
-exports.IsDirectory = IsDirectory;
-exports.IsFile = IsFile;
-exports.IsInstanceID = IsInstanceID;
-exports.GenerateSegments = GenerateSegments;
-exports.GenerateInstanceID = GenerateInstanceID;

+ 43 - 11
lib/reflector.js

@@ -1,12 +1,32 @@
 "use strict";
 // reflector.js
-// 
-//var parseurl = require( './parse-url' ),
-   // persistence = require( './persistence' ),
 
-   var helpers = require( './helpers' );
-   var fs = require( 'fs' );
+// JoinPath
+// Takes multiple arguments, joins them together into one path.
+function JoinPath( /* arguments */ ) {
+    var result = "";
+    if ( arguments.length > 0 ) {
+        if ( arguments[ 0 ] ) {
+            result = arguments[ 0 ];
+        }
+        for ( var index = 1; index < arguments.length; index++ ) {
+            var newSegment = arguments[ index ];
+            if ( newSegment == undefined ) {
+                newSegment = "";
+            }
 
+            if ( ( newSegment[ 0 ] == "/" ) && ( result[ result.length - 1 ] == "/" ) ) {
+                result = result + newSegment.slice( 1 );
+            } else if ( ( newSegment[ 0 ] == "/" ) || ( result[ result.length - 1 ] == "/" ) ) {
+                result = result + newSegment;
+            } else {
+                result = result + "/" + newSegment;
+            }
+            //result = libpath.join( result, newSegment );
+        }
+    }
+    return result;
+}
 
 function parseSocketUrl( socket ) {
 
@@ -46,7 +66,7 @@ function parseSocketUrl( socket ) {
 //Get the instance ID from the handshake headers for a socket
 function GetNamespace( processedURL ) {
     if ( ( processedURL[ 'instance' ] ) && ( processedURL[ 'public_path' ] ) ) {
-        return helpers.JoinPath( processedURL[ 'public_path' ], processedURL[ 'application' ], processedURL[ 'instance' ] );
+        return JoinPath( processedURL[ 'public_path' ], processedURL[ 'application' ], processedURL[ 'instance' ] );
     }
     return undefined;
 }
@@ -66,9 +86,13 @@ function OnConnection( socket ) {
    var address = socket.conn.request.headers.host;
              var obj = {};
              for (var prop in global.instances) {
+                 let user = global.instances[prop].user;
+                 let loadInfo = global.instances[prop].loadInfo;
                 obj[prop] =  {
-                    "instance":address + prop,
-                    "clients": Object.keys(global.instances[prop].clients).length
+                    "instance":address + '/'+ user + prop,
+                    "clients": Object.keys(global.instances[prop].clients).length,
+                    "user": user,
+                    "loadInfo": loadInfo
                 };
             }
             var json = JSON.stringify(obj);
@@ -93,12 +117,15 @@ function OnConnection( socket ) {
     
     var loadInfo = resObj.loadInfo //GetLoadForSocket( processedURL );
     var saveObject = resObj.saveObject //persistence.LoadSaveObject( loadInfo );
+    var user = resObj.user;
 	   
     
 
     //if it's a new instance, setup record 
     if( !global.instances[ namespace ] ) {
         global.instances[ namespace ] = { };
+        global.instances[ namespace ].loadInfo = loadInfo;
+        global.instances[ namespace ].user = user;
         global.instances[ namespace ].clients = { };
         global.instances[ namespace ].pendingList = [ ];
         global.instances[ namespace ].start_time = undefined;
@@ -400,9 +427,12 @@ function OnConnection( socket ) {
                 client.emit ( 'message', clientMessage );
             }
         }
-        if ( global.instances[ namespace ].pendingList.pending ) {
-            global.instances[ namespace ].pendingList.push( clientMessage );
-        }
+
+        if (global.instances[namespace]) {
+
+            if (global.instances[namespace].pendingList.pending) {
+                global.instances[namespace].pendingList.push(clientMessage);
+            }
         
         // If it's the last client, delete the data and the timer
         if ( Object.keys( global.instances[ namespace ].clients ).length == 0 ) {
@@ -412,6 +442,8 @@ function OnConnection( socket ) {
         } else {
            // xapi.logClient( undefined, loadInfo[ 'application_path' ], loadInfo[ 'save_name' ], namespace, clientDescriptor.properties || {}, false, false );
         }
+    }
+    
     } );
 }
 

+ 3 - 2
node_vwf.js

@@ -61,7 +61,7 @@ function startVWF() {
         } );
         var inst = Object.keys(global.instances);
         var jsonobject = {
-            "reflector": "v0.0.1"
+            "reflector": "v0.0.2"
             //"instances": inst
         }
         response.write( JSON.stringify( jsonobject ), "utf8" );
@@ -98,11 +98,12 @@ function startVWF() {
     var sslOptions = {
         key: ((argv.k || argv.key) ? fs.readFileSync(argv.k || argv.key) : undefined),
         cert: ((argv.c || argv.cert) ? fs.readFileSync(argv.c || argv.cert) : undefined),
+        ca: ( ( argv.t || argv.ca ) ? fs.readFileSync( argv.t || argv.ca ) : undefined ),
         passphrase: JSON.stringify(pass)
     };
 
     //create the server
-    var port = ((argv.p || argv.port) ? (argv.p || argv.port) : 3001);
+    var port = ((argv.p || argv.port) ? (argv.p || argv.port) : 3002);
 
     var srv = ssl ? https.createServer(sslOptions, OnRequest).listen(port) : http.createServer(OnRequest).listen(port);
     consoleNotice('Serving on port ' + port);

+ 3 - 3
package.json

@@ -1,11 +1,11 @@
 {
   "name": "lcs-reflector",
   "description": "LiveCoding.Space reflector",
-  "version": "0.0.1",
+  "version": "0.0.2",
   "author": "Nikolai Suslov",
   "scripts": {
-    "start": "node ./node-server.js -p 3001",
-    "startSSL": "node ./node-server.js -p 3001 -s -k ./cert/key.pem -c ./cert/cert.pem",
+    "start": "node ./node-server.js -p 3002",
+    "startSSL": "node ./node-server.js -p 3002 -s -k ./certs/server-key.pem -c ./certs/server-crt.pem -t ./certs/ca-crt.pem -w 12345",
     "test": "echo \"Error: no test specified\" && exit 1"
   },
   "directories": {