PageRenderTime 30ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/scripts/jahova/OS/Cores/Network/jahova.os.cores.network.js

https://bitbucket.org/GTL/surveyjs-builder
JavaScript | 580 lines | 403 code | 109 blank | 68 comment | 24 complexity | a52ec9938baaf9e0911421e185998d44 MD5 | raw file
  1. //JaHOVA OS : Cores : Network
  2. // Dependent on JaHOVA OS : Cores
  3. /**h* InternalCores/Network
  4. * LINKS
  5. * |html <p><a href="http://jahovaos.com/JaHOVA/Documentation/Full/toc_index.html"> Table of Contents </a></p>
  6. *
  7. * NAME
  8. * JaHOVA OS Network Core
  9. *
  10. * AUTHOR
  11. * Corey Clark
  12. * cclark@coreyclarkphd.com
  13. *
  14. * HISTORY
  15. * Created: 4/14/2011
  16. *
  17. *
  18. * DESCRIPTION
  19. * This Singleton holds the Internal JaHOVA OS Network Core
  20. *
  21. * EXAMPLE
  22. * network = com.jahova.os.Instance().Cores.Instance().Network.Instance();
  23. *
  24. **/
  25. com.jahova.os.Instance().Cores.Instance().Network = (function()
  26. {
  27. var pInstance;
  28. function constructor()
  29. {
  30. //PRIVATE ATTRIBUTES
  31. var NAME = "JaHOVA OS Internal API : Network Core";
  32. var VERSION = "0v3";
  33. var PATH = "scripts/jahova/OS/Cores/Network/jahova.os.cores.network.js";
  34. var ID = null;
  35. var os = com.jahova.os.Instance();
  36. var utilities = com.jahova.utilities.Instance();
  37. //Private Classes
  38. var CNetworkMessage = function(){
  39. this.SenderID; // CUser ID
  40. this.ReceiverID; // CSession Name
  41. this.Data = {
  42. Type: null, // Message Type
  43. Args: null, // Arguments for Interface Call
  44. ModuleID: null // CModule Name
  45. }
  46. this.InterfaceID // CFunction Name
  47. }
  48. var CJOSServerMessage = function(){
  49. this.UserID = "";
  50. this.SessionID = "";
  51. this.ModuleID = "";
  52. this.Data = {
  53. Type: "",
  54. Args: ""
  55. };
  56. this.FunctionID = "";
  57. this.Parse = function(msgJSON){
  58. var msg = "";
  59. try{
  60. msg = JSON.parse(msgJSON);
  61. this.UserID = msg.UserID;
  62. this.SessionID = msg.SessionID;
  63. this.Data = msg.Data;
  64. this.ModuleID = msg.ModuleID;
  65. this.FunctionID = msg.FunctionID;
  66. }
  67. catch(e){
  68. os.windows.Create.ErrorWindow("ERROR: JOS Server", "Unable to parse message: <br/>" + msgJSON);
  69. }
  70. };
  71. this.Serialize = function(){
  72. return JSON.stringify(this);
  73. };
  74. }
  75. var CJOSServer = function(){
  76. //**********************
  77. // PRIVATE METHODS
  78. //**********************
  79. var _onOpen = function(){
  80. os.console.Comment("JaHOVA OS Server: Connection Opened");
  81. if(this.Events.onOpen){
  82. this.Events.onOpen();
  83. }
  84. }.bind(this);
  85. var _onClose = function(){
  86. os.console.Comment("JaHOVA OS Server: Connection Closed");
  87. if(this.Events.onClose){
  88. this.Events.onClose();
  89. }
  90. }.bind(this);
  91. var _onMessage = function(msgJSON){
  92. var msg = new CJOSServerMessage();
  93. msg.Parse(msgJSON);
  94. if(this.Events.onMessage){
  95. this.Events.onMessage(msg);
  96. }
  97. if(msg.Data.Type == "userID"){
  98. this.userID = msg.Data.Args[0];
  99. this.mainSessionID = msg.SessionID;
  100. os.console.Comment("JaHOVA OS Server ID Set: " + this.userID);
  101. }
  102. }.bind(this);
  103. var _onError = function(e){
  104. if(this.Events.onError){
  105. this.Events.onError(e);
  106. }
  107. }.bind(this);
  108. this.socket = null;
  109. this.message = null;
  110. this.userID = null;
  111. this.mainSessionID = null;
  112. this.Events = {
  113. onOpen: null,
  114. onMessage: null,
  115. onClose: null,
  116. onError: null
  117. };
  118. this.Connect = function(){
  119. this.socket.Events.onOpen = _onOpen;
  120. this.socket.Events.onMessage = _onMessage;
  121. this.socket.Events.onClose = _onClose;
  122. this.socket.Events.onError = _onError;
  123. this.socket.Open();
  124. }
  125. this.Send = function(msg){
  126. this.socket.Send(msg.Serialize());
  127. };
  128. this.Disconnect = function(){
  129. //Send Disconnect Message to Server
  130. var msg = new CJOSServerMessage();
  131. msg.UserID = this.userID;
  132. msg.Data.Type = "disconnect";
  133. this.Send(msg);
  134. };
  135. this.Create ={
  136. Session: function(sessionType, password, params){
  137. var msg = new CJOSServerMessage();
  138. msg.SessionID = this.mainSessionID;
  139. msg.UserID = this.userID;
  140. msg.Data.Type = "createSession";
  141. msg.Data.Args = [sessionType, password, params];
  142. msg.ModuleID = "";
  143. this.Send(msg);
  144. }.bind(this),
  145. RegisteredSession: function(sessionType, password){
  146. var msg = new CJOSServerMessage();
  147. msg.SessionID = this.mainSessionID;
  148. msg.UserID = this.userID;
  149. msg.Data.Type = "createRegisteredSession";
  150. msg.Data.Args = [sessionType, password];
  151. msg.ModuleID = "";
  152. this.Send(msg);
  153. }.bind(this),
  154. Module: function(moudule, password){
  155. var msg = new CJOSServerMessage();
  156. msg.SessionID = "";
  157. msg.UserID = this.userID;
  158. msg.Data.Type = "createModule";
  159. msg.Data.Args = [password];
  160. msg.ModuleID = module;
  161. this.Send(msg);
  162. }.bind(this),
  163. Function: function(module, funcName, funcBody, funcInput){
  164. var msg = new CJOSServerMessage();
  165. msg.SessionID = "";
  166. msg.UserID = this.userID;
  167. msg.Data.Type = "createFunction";
  168. msg.Data.Args = [module, funcName, funcBody, funcInput];
  169. msg.ModuleID = module;
  170. this.Send(msg);
  171. }.bind(this),
  172. Message: function(){
  173. var msg = new CJOSServerMessage();
  174. msg.UserID = this.userID;
  175. return msg;
  176. }.bind(this)
  177. }
  178. this.Get = {
  179. SessionList: function(sessionType){
  180. var msg = new CJOSServerMessage();
  181. msg.SessionID = this.mainSessionID;
  182. msg.UserID = this.userID;
  183. msg.Data.Type = "getList";
  184. msg.Data.Args = [sessionType];
  185. msg.ModuleID = "";
  186. this.Send(msg);
  187. }.bind(this),
  188. SessionPropertyTypes: function(sessionID){
  189. var msg = new CJOSServerMessage();
  190. msg.SessionID = sessionID;
  191. msg.UserID = this.userID;
  192. msg.Data.Type = "getSessionProperties";
  193. msg.Data.Args = [];
  194. msg.ModuleID = "";
  195. this.Send(msg);
  196. }.bind(this),
  197. SessionPropertyValues: function(sessionID){
  198. var msg = new CJOSServerMessage();
  199. msg.SessionID = sessionID;
  200. msg.UserID = this.userID;
  201. msg.Data.Type = "getSessionValues";
  202. msg.Data.Args = [];
  203. msg.ModuleID = "";
  204. this.Send(msg);
  205. }.bind(this)
  206. }
  207. this.Set = {
  208. SessionProperty: function(session, key, value){
  209. var msg = new CJOSServerMessage();
  210. msg.SessionID = session;
  211. msg.UserID = this.userID;
  212. msg.Data.Type = "setSessionProperty";
  213. msg.Data.Args = [key,value];
  214. msg.ModuleID = "";
  215. this.Send(msg);
  216. }.bind(this)
  217. }
  218. this.JoinSession = function(session, password){
  219. var msg = new CJOSServerMessage();
  220. msg.SessionID = session;
  221. msg.UserID = this.userID;
  222. msg.Data.Type = "joinSession";
  223. msg.Data.Args = [password];
  224. msg.ModuleID = "";
  225. this.Send(msg);
  226. };
  227. this.Broadcast = function(session, data){
  228. var msg = new CJOSServerMessage();
  229. msg.SessionID = session;
  230. msg.UserID = this.userID;
  231. msg.Data.Type = "sessionBroadcast";
  232. msg.Data.Args = [data];
  233. msg.ModuleID = "";
  234. this.Send(msg);
  235. }
  236. this.LeaveSession = function(session){
  237. var msg = new CJOSServerMessage();
  238. msg.SessionID = session;
  239. msg.UserID = this.userID;
  240. msg.Data.Type = "leaveSession";
  241. msg.ModuleID = "";
  242. this.Send(msg);
  243. }
  244. this.AccessModule = function(session, module, password){
  245. var msg = new CJOSServerMessage();
  246. msg.SessionID = session;
  247. msg.UserID = this.userID; //"MainSession";
  248. msg.Data.Type = "accessModule";
  249. msg.Data.Args = [password]; //["godfather"];
  250. msg.ModuleID = module; //"AdminModule";
  251. this.Send(msg);
  252. };
  253. this.ExecuteFunction = function(params, module, func){
  254. var msg = new CJOSServerMessage();
  255. msg.SessionID = ""
  256. msg.UserID = this.userID;
  257. msg.Data.Type = "executeFunction";
  258. msg.Data.Args = [param];
  259. msg.ModuleID = module;
  260. this.Send(msg);
  261. };
  262. }
  263. var CSocketIO = function(domain, port, path, id){
  264. //**********************
  265. // PRIVATE ATTRIBUTES
  266. //**********************
  267. var PATH = path;
  268. var DOMAIN = domain;
  269. var PORT = port;
  270. var ID = id;
  271. var socket = null;
  272. //**********************
  273. // PRIVATE METHODS
  274. //**********************
  275. var _onOpen = function(){
  276. if(this.Events.onOpen){
  277. this.Events.onOpen();
  278. }
  279. }.bind(this);
  280. var _onClose = function(){
  281. if(this.Events.onClose){
  282. this.Events.onClose();
  283. }
  284. }.bind(this);
  285. var _onMessage = function(msg){
  286. if(this.Events.onMessage){
  287. this.Events.onMessage(msg);
  288. }
  289. }.bind(this);
  290. var _onError = function(e){
  291. if(this.Events.onError){
  292. this.Events.onError(e);
  293. }
  294. }.bind(this);
  295. //**********************
  296. // PUBLIC ATTRIBUTES
  297. //**********************
  298. this.Events = {
  299. onOpen: null,
  300. onMessage: null,
  301. onClose: null,
  302. onError: null
  303. };
  304. //**********************
  305. // PUBLIC METHODS
  306. //**********************
  307. this.GetID = function(){
  308. return ID;
  309. }
  310. this.Open = function(){
  311. if(socket == null){
  312. socket = io.connect( DOMAIN + ":" + PORT +"/" + PATH);
  313. socket.on("connect", _onOpen);
  314. socket.on("disconnect" , _onClose);
  315. socket.on("message" , _onMessage);
  316. socket.on("error" , _onError);
  317. }
  318. };
  319. this.Close = function(){
  320. };
  321. this.Send = function(msg){
  322. socket.send(msg);
  323. }
  324. }
  325. var CWebSocket = function(domain, port, path, id){
  326. //**********************
  327. // PRIVATE ATTRIBUTES
  328. //**********************
  329. var PATH = path;
  330. var DOMAIN = domain;
  331. var PORT = port;
  332. var ID = id;
  333. var socket = null;
  334. //**********************
  335. // PRIVATE METHODS
  336. //**********************
  337. var _OnOpenFunc = function(msg){
  338. this.state = socket.readyState;
  339. if(this.Event.onConnect != null){
  340. this.Event.onConnect();
  341. }
  342. }.bind(this);
  343. var _OnCloseFunc = function(msg){
  344. this.state = socket.readyState;
  345. if(this.Events.onClose != null){
  346. this.Events.onClose();
  347. }
  348. }.bind(this);
  349. var _OnMessageFunc = function(msg){
  350. this.state = socket.readyState;
  351. if(this.Events.onMessage != null){
  352. this.Events.onMessage();
  353. }
  354. }.bind(this);
  355. var _OnErrorFunc = function(){
  356. this.state = socket.readyState;
  357. if(this.Events.onError != null){
  358. this.Events.onError();
  359. }
  360. }.bind(this);
  361. //**********************
  362. // PUBLIC ATTRIBUTES
  363. //**********************
  364. this.Events = {
  365. onOpen: null,
  366. onMessage: null,
  367. onClose: null,
  368. onError: null
  369. };
  370. //**********************
  371. // PUBLIC METHODS
  372. //**********************
  373. this.GetID = function(){
  374. return ID;
  375. }
  376. this.Open = function(){
  377. if(socket == null){
  378. socket = new WebSocket("ws://" + DOMAIN + ":" + PORT +"/" + PATH);
  379. this.state = socket.readyState;
  380. socket.onopen = _OnOpenFunc.bind(this);
  381. socket.onclose = _OnCloseFunc.bind(this);//function(){alert("Socket Closed");};
  382. socket.onmessage = _OnMessageFunc.bind(this);
  383. socket.onerror = _OnErrorFunc.bind(this);
  384. return this.state;
  385. }
  386. else{
  387. return "Already Initialized";
  388. }
  389. };
  390. this.Close = function(){
  391. socket.close();
  392. };
  393. this.Send = function(msg){
  394. socket.send(msg);
  395. }
  396. };
  397. return{
  398. //PUBLIC ATTRIBUTES
  399. //PUBLIC PRIVILEDGE METHODS
  400. GetName: function(){
  401. return NAME;
  402. },
  403. GetVersion: function(){
  404. return VERSION;
  405. },
  406. GetPath: function(){
  407. return PATH;
  408. },
  409. GetID: function(){
  410. return ID;
  411. },
  412. Initialize: function(){
  413. },
  414. SocketManager:{
  415. Sockets: os.resschmgr.Create.Map(),
  416. CreateSocket: function(domain,port,path){
  417. //Get new ID for socket
  418. var id = (new Date()).getTime();
  419. //Create new socket
  420. var socket = new CWebSocket(domain, port, path, id);
  421. //register socket with Socket Manager
  422. this.Sockets.put(id, socket);
  423. return socket;
  424. }
  425. },
  426. Create: {
  427. Socket: function(domain,port,path){
  428. //Get new ID for socket
  429. var id = (new Date()).getTime();
  430. //Create new socket
  431. var socket = new CWebSocket(domain, port, path, id);
  432. //register socket with Socket Manager
  433. os.network.SocketManager.Sockets.put(id, socket);
  434. return socket;
  435. },
  436. SocketIO: function(domain, port, path){
  437. //Get new ID for socket
  438. var id = (new Date()).getTime();
  439. //Create new socket
  440. var socket = new CSocketIO(domain, port, path, id);
  441. //register socket with Socket Manager
  442. os.network.SocketManager.Sockets.put(id, socket);
  443. return socket;
  444. },
  445. JOSServer: function(domain,port,path){
  446. var joss = new CJOSServer();
  447. //Test to see if socket.io is installed
  448. if(io){
  449. joss.socket = os.network.Create.SocketIO(domain, port, path);
  450. }
  451. else{
  452. joss.socket = os.network.Create.Socket(domain, port, path);
  453. }
  454. return joss;
  455. }
  456. }
  457. }
  458. }
  459. return {
  460. //OBJECT ACCESSOR
  461. Instance: function()
  462. {
  463. if(!pInstance)
  464. {
  465. //Instantiate if pInstance does not exist
  466. pInstance = constructor();
  467. }
  468. return pInstance;
  469. }
  470. }
  471. })();