/AtecConnectMain/server/CommandHandlers.js
JavaScript | 345 lines | 248 code | 67 blank | 30 comment | 14 complexity | 38bc4e16b9ca71a509d5b71bdec6cd7e MD5 | raw file
- RegisterConnection = function (data, connection)
- {
- Fiber(function(){
- //Finds any API_KEYS that match what the TCP Packet sent
- var Akey = APIKeys.findOne({key: GetParameter(data,'API_KEY')});
- // Checks if it found a key by checking if Akey is null
- if(Akey){
- // Updates DB
- connection.APIKey = Akey;
- var mProjectID = GetParameter(data,'PROJECT_ID');
- if(mProjectID != "SEARCH_FAILED")
- {
- connection.Auth = true;
- connection.Project = mProjectID;
- connection.socket.write("RESPONSE_REGISTER_CONNECTION|DETAIL=API_KEY_INVALID;");
- LogEvent(EventType.EVENT, EventDetail.COMMAND_HANDLER,
- "Registered Connection Success", {PID: mProjectID, RAW: GetParameter(data,'PROJECT_ID'), DATA: data.toString()});
- }
- else{
- connection.socket.write("FAILURE|DETAIL=NO_PROJECT_ID");
- console.log("NO PROJECT ID!");
- LogEvent(EventType.ERROR, EventDetail.COMMAND_HANDLER,
- "Connection Registration Rejected (PROJECT_ID)", "");
- }
- }
- else{
- // Prints and Sends Invalid Key to console and client
- console.log("FAILURE|DETAIL=API_KEY_INVALID;");
- connection.socket.write("FAILURE|DETAIL=API_KEY_INVALID;");
- LogEvent(EventType.ERROR, EventDetail.COMMAND_HANDLER,
- "Connection Registration Rejected (API KEY)", "");
- }
- }).run();
- }
- ControllerConnected = function (data, connection){
- Fiber(function(){
- //Finds any API_KEYS that match what the TCP Packet sent
- // Checks if it found a key by checking if Akey is null
- if(connection.Auth){
- // Updates DB
- Projects.update(GetParameter(data,'PROJECT_ID'), {
- $inc: {controllers: 1}
- });
- var newController = {
- ip: GetParameter(data,"IP"),
- port: GetParameter(data,"PORT"),
- maxservers: GetParameter(data,"MAXSERVERS"),
- project: GetParameter(data, 'PROJECT_ID'),
- socket: connection.id,
- createdAt: new Date()
- };
- var id = Controllers.insert(newController);
- connection.Type = ConnectionType.CONTROLLER;
- connection.DatabaseID = id;
- console.log(id);
- connection.socket.write("SUCESS|ID=" + id + ";");
- LogEvent(EventType.EVENT, EventDetail.COMMAND_HANDLER,
- "Controller Connected", connection);
- }
- else{
- // Prints and Sends Invalid Key to console and client
- console.log("FAILURE|DETAIL=NOT_AUTH;");
- connection.socket.write("FAILURE|DETAIL=NOT_AUTH;");
- }
- }).run();
- }
- ControllerDisconnected = function(data, connection, timeout)
- {
- Fiber(function()
- {
- var GameServers = Servers.find({controller: connection.id});
- var GameServerAmount = GameServers.count()
- if(timeout)
- {
- Projects.update(connection.pid, {
- $inc: {controllers: -1}
- });
- Projects.update(connection.pid, {
- $inc: {servers: -GameServerAmount}
- });
- console.log(GameServerAmount);
- Controllers.remove(connection.dbid);
- Servers.remove({controller: connection.id});
- }
- else
- {
- var Akey = APIKeys.findOne({key: GetParameter(data,'API_KEY')});
- // Checks if it found a key by checking if Akey is null
- if(Akey)
- {
- // Updates DB
- Projects.update(GetParameter(data,'PROJECT_ID'), {
- $inc: {controllers: -1}
- });
- Projects.update(GetParameter(data,'PROJECT_ID'), {
- $inc: {servers: GameServerAmount}
- });
- Controllers.remove(connection.dbid);
- }
- else{
- // Prints and Sends Invalid Key to console and client
- console.log("INVALD KEY!");
- connection.socket.write("INVALID KEY!");
- }
- }
- //Finds any API_KEYS that match what the TCP Packet sent
- }).run();
- console.log("CONTROLLER DISCONNECTEd TO PROJECT: " + connection.pid);
- }
- GameConnected = function(data, connection)
- {
- Fiber(function()
- {
- //Finds any API_KEYS that match what the TCP Packet sent
- // Checks if it found a key by checking if Akey is null
- if(connection.Auth)
- {
- // Updates DB
- var newGame = {
- createdAt: new Date(),
- ping: 0,
- ip: connection.socket.remoteAddress
- };
- console.log(connection.Project);
- Projects.update({puid: connection.Project}, {
- $inc: {numGames: 1},
- $push: {games: newGame}
- });
- var id = Games.insert(newGame);
- connection.type = ConnectionType.GAME;
- connection.dbid = id;
- //socket.write(newController._id);
- console.log("TEST");
- connection.socket.write("RESPONSE_GAME_CONNECTED|ID=" + id + ";");
- }
- else{
- // Prints and Sends Invalid Key to console and client
- console.log("FAILURE|DETAIL=API_KEY_INVALID;");
- connection.socket.write("FAILURE|DETAIL=API_KEY_INVALID;");
- }
- }).run();
- console.log("CONTROLLER CONNECT TO PROJECT: " +
- GetParameter(data,'PROJECT_ID'));
- }
- GameDisconnected = function (data, connection, timeout)
- {
- Fiber(function()
- {
- //Finds any API_KEYS that match what the TCP Packet sent
- if(timeout)
- {
- Projects.update(connection.pid, {
- $inc: {numGames: -1}
- });
- }
- else
- {
- var Akey = APIKeys.findOne({key: GetParameter(data,'API_KEY')});
- // Checks if it found a key by checking if Akey is null
- if(Akey)
- {
- // Updates DB
- Projects.update(GetParameter(data,'PROJECT_ID'), {
- $inc: {games: -1}
- });
- Games.remove(connection.dbid);
- }
- else{
- // Prints and Sends Invalid Key to console and client
- console.log("INVALD KEY!");
- connection.socket.write("INVALID KEY!");
- }
- }
- //Finds any API_KEYS that match what the TCP Packet sent
- console.log("GAME DISCONNECTED TO PROJECT: " + connection.pid);
- }).run();
- }
- GetServers =function (data, connection)
- {
- Fiber(function()
- {
- //Finds any API_KEYS that match what the TCP Packet sent
- // Checks if it found a key by checking if Akey is null
- if(connection.Auth)
- {
- var SendString = "GET_SEVERS|servers=[";
- var servers = Projects.findOne({puid: connection.Project}).servers;
- if(servers)
- {
- for(var i=0;i<servers.length;i++)
- {
- var row = servers[i];
- console.log(row.ip)
- var ServerObject = "SERVER={";
- ServerObject += "PORT="+row.port+";";
- ServerObject += "IP="+row.ip+";";
- ServerObject +="};";
- SendString += ServerObject;
- }
- }
- SendString +="];";
- console.log("RETURNING: " + SendString);
- connection.socket.write(SendString);
- }
- else{
- // Prints and Sends Invalid Key to console and client
- console.log("FAILURE|DETAIL=API_KEY_INVALID;");
- socket.write("FAILURE|DETAIL=API_KEY_INVALID;");
- }
- }).run();
- console.log("CONTROLLER CONNECT TO PROJECT: " +
- GetParameter(data,'PROJECT_ID'));
- }
- RegisterServer = function (data, connection)
- {
- WebConsole(EventType.EVENT, EventDetail.COMMAND_HANDLER,
- "Server Attempting Registration", "Server Attempting Registration: SOCKET ID=" + connection.id );
- Fiber(function()
- {
- //Finds any API_KEYS that match what the TCP Packet sent
- var Key = APIKeys.findOne({key: GetParameter(data,'API_KEY')});
- // Checks if it found a key by checking if Akey is null
- if(Key)
- {
- Projects.update(GetParameter(data,'PROJECT_ID'), {
- $inc: {servers: 1}
- });
- var newServer = {
- ip: GetParameter(data, "IP"),
- port : GetParameter(data, "PORT"),
- controller: connection.id,
- createdAt: new Date()
- };
- var id = Servers.insert(newServer);
- WebConsole(EventType.EVENT, EventDetail.COMMAND_HANDLER,
- "Server Registered", "Server Registered: SOCKET ID=" + id );
- connection.socket.write("SUCESS|ID=" + id);
- }
- else{
- // Prints and Sends Invalid Key to console and client
- console.log("FAILURE|DETAIL=API_KEY_INVALID;");
- connection.socket.write("FAILURE|DETAIL=API_KEY_INVALID;");
- }
- }).run();
- }
- RegisterLocalHost = function(data,connection)
- {
- LogEvent(EventType.EVENT, EventDetail.COMMAND_HANDLER,
- "Registering LocalHost Server...", {PID: connection.Project, IP: connection.socket.remoteAddress, PORT: 7777});
- console.log("LOCAL HOST!!!!");
- Fiber(function()
- {
- // Checks if it found a key by checking if Akey is null
- if(connection.Auth)
- {
- var newServer = {
- ip: connection.socket.remoteAddress,
- port : GetParameter(data, "PORT"),
- controller: "LOCAL_HOST",
- createdAt: new Date()
- };
- Projects.update({puid: connection.Project}, {
- $inc: {numServers: 1},
- $push: {servers: newServer}
- });
- connection.socket.write("SUCESS|;");
- LogEvent(EventType.EVENT, EventDetail.COMMAND_HANDLER,
- "Registering LocalHost Server SUCCESS!", {PID: connection.Project, IP: connection.socket.remoteAddress, PORT: 7777});
- }
- else{
- // Prints and Sends Invalid Key to console and client
- LogEvent(EventType.ERROR, EventDetail.COMMAND_HANDLER,
- "Registered Local Host", {PID: connection.Project, IP: connection.socket.remoteAddress, PORT: 7777});
- connection.socket.write("FAILURE|DETAIL=API_KEY_INVALID;");
- console.log("No Auth");
- }
- }).run();
- }