Advertisement
diddlside

Untitled

Apr 6th, 2013
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * ArcusNode Service
  3.  *  
  4.  * Copyright 2011 OpenRTMFP
  5.  *
  6.  * This program is free software: you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation, either version 3 of the License, or
  9.  * (at your option) any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License received along this program for more
  15.  * details (or else see http://www.gnu.org/licenses/).
  16.  *
  17.  * Author: arcusdev <arcus.node@gmail.com>
  18.  *
  19.  * This file is a part of ArcusNode.
  20.  */
  21.  
  22. var util = require('util');
  23. var ArcusNode = require('./lib/arcus_node.js');
  24.  
  25. //Take command line arguments
  26. var settings = {};
  27. process.argv.forEach(function (val, index, array) {
  28.   if(index < 2)
  29.     return;
  30.   var valArr = val.split('=');
  31.   switch(valArr[0]){
  32.     case 'logLevel':
  33.         settings.logLevel = valArr[1];
  34.         util.print('Setting logLevel to ' + valArr[1] + '\n');
  35.       break;
  36.     case 'logFile':
  37.         settings.logFile = valArr[1];
  38.         util.print('Setting logFile to ' + valArr[1] + '\n');
  39.       break;
  40.     case '-h':
  41.     case '-help':
  42.         util.print(
  43.           '### USAGE ###\n\n' +
  44.           'node service.js [argument1 argument2 ...]\n\n' +
  45.           'Arguments:\n' +
  46.           'logLevel=[level]            [level] can be one of: fatal, error, warn, info, debug' +
  47.           'logFile=[path]              [path] The path to a file to log output to' +
  48.           '\n\n'
  49.         );
  50.         process.exit();
  51.       break;
  52.     default:
  53.       util.print('Argument unknown or malformed: ' + val + '\nStopping process.');
  54.       process.exit();
  55.   }
  56. });
  57.  
  58. //Startup
  59. util.print(
  60.   'Starting up ArcusNode RTMFP Service.\nCopyright (C) 2011 OpenRTMFP \n' +
  61.   'This program comes with ABSOLUTELY NO WARRANTY.\n' +
  62.   'This is free software, and you are welcome to redistribute it under certain conditions.\n' +
  63.   '(For usage help type "node service.js -h")\n'
  64. );
  65.  
  66. var usercount = 0;
  67. var messages = [];
  68. var usernames = [];
  69. /*
  70. settings.manageInterval = 24 * 60 * 60;
  71. settings.connectionTimeout = (24 * 60 * 60)*1000;
  72. settings.clientKeepalive = 1000;
  73. settings.serverKeepalive = 1000;
  74. settings.maxKeepalives = 30000;
  75. */
  76. var arc = new ArcusNode(settings);
  77.  
  78. arc.on('connect', function(nc, obj){
  79.   usercount++;
  80.   usernames.push({
  81.     id : nc.__p.id,
  82.     name : 'Guest ' + usercount
  83.   });
  84.   messages.push('Guest ' + usercount + ' joined!');
  85.   nc.accept();
  86. });
  87.  
  88. function getUsername(nc) {
  89.   var username = '';
  90.   for (var i = usernames.length - 1; i >= 0; i--) {
  91.     if(nc.__p.id == usernames[i].id)
  92.     {
  93.       username = usernames[i].name;
  94.     }
  95.   };
  96.  
  97.   return username;
  98. }
  99.  
  100. arc.on('disconnect', function(){
  101.   usercount--;
  102. });
  103.  
  104. util.print(util.inspect(arc, false, null));
  105.  
  106. arc.onCommand('getWelcomeMessage', function(nc, obj){
  107.   var message = 'Welcome to this testroom, there are currently '+usercount+' user online';
  108.   messages.push(message);
  109.   var username = getUsername(nc);
  110.   util.print('client connected:'+nc.__p.id+"\n");
  111.   //util.print(util.inspect(nc, false, null));
  112.  
  113.   return { what: message, name : username };
  114. });
  115.  
  116. arc.onCommand('sendMessage', function(nc, obj){
  117.   var username = getUsername(nc);
  118.   messages.push(username + ' : ' + obj.message);
  119.   return { what: messages.join("\n") };
  120. });
  121.  
  122. arc.onCommand('getMessages', function(nc, obj){
  123.   return { what: messages.join("\n") };
  124. });
  125.  
  126. arc.run();
  127.  
  128. process.on('SIGINT', function () {
  129.   util.print('\033[36mArcusNode shutting down...\033[0m\n');
  130.   arc.stop();
  131. });
  132. process.on('exit', function(){
  133.   util.print('\033[36mArcusNode stopped.\033[0m\n');
  134. });
  135.  
  136. util.print('ArcusNode RTMFP Service running at ' + arc.address.address + ((arc.address.port != '') ? ':' + arc.address.port : '') + '\n');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement