init.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*******************************************************************************
  2. AToMPM - A Tool for Multi-Paradigm Modelling
  3. Copyright (c) 2011 Raphael Mannadiar (raphael.mannadiar@mail.mcgill.ca)
  4. Modified by Conner Hansen (chansen@crimson.ua.edu)
  5. This file is part of AToMPM.
  6. AToMPM is free software: you can redistribute it and/or modify it under the
  7. terms of the GNU Lesser General Public License as published by the Free Software
  8. Foundation, either version 3 of the License, or (at your option) any later
  9. version.
  10. AToMPM is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  12. PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License along
  14. with AToMPM. If not, see <http://www.gnu.org/licenses/>.
  15. *******************************************************************************/
  16. //TODO: shred initClient()
  17. /* initialize client
  18. PART 1: setup connection to backend
  19. 0. extract GET parameters, if any, from URL
  20. 1. setup socket and its event handlers (to handle changelogs etc.), then
  21. connect socket
  22. 2. on socket connection,
  23. a) if no GET params or if modelshare params,
  24. i. spawn new csworker
  25. ii. on spawn, subscribe to new csworker changes
  26. a) otherwise (screenshare), subscribe to specified csworker changes
  27. 3. on reception of socket messages,
  28. a) if message has a status code (i.e., response to csworker subscription)
  29. i. load mainmenu button model
  30. * CASE 1: no GET params *
  31. ii. spawn and subscribe to new asworker
  32. iii. get user preferences and autoload toolbars and model, if any
  33. iv. enable collaboration links
  34. v. launch autosave daemon
  35. * CASE 2: modelshare *
  36. ii. clone state of given csworker and subscribe to given asworker
  37. iii. update client state based on our csworker's updated state
  38. iv. enable collaboration links
  39. v. launch autosave daemon
  40. * CASE 3: screenshare *
  41. ii. retrieve state of given csworker
  42. iii. update client state based on our csworker's updated state
  43. iv. enable collaboration links
  44. v. launch autosave daemon
  45. a) otherwise, handle changelog
  46. PART 2: setup frontend
  47. 1. setup various GUI details and behaviour statecharts
  48. 2. setup exit prompt */
  49. function __initClient()
  50. {
  51. /** PART 1 **/
  52. var params = {};
  53. window.location.search.substring(1).split('&').forEach(
  54. function(arg)
  55. {
  56. var _arg = arg.split('=');
  57. params[_arg[0]] = _arg[1];
  58. });
  59. var socket = io.connect(
  60. window.location.hostname,{'port':8124,'reconnect':false});
  61. socket.on('message',
  62. function(msg)
  63. {
  64. console.debug(' >> '+utils.jsons(msg));
  65. if( msg['statusCode'] != undefined )
  66. {
  67. if( msg['statusCode'] == 201 )
  68. {
  69. _loadToolbar(__MAINMENU_PATH);
  70. if( window.location.search == '' )
  71. HttpUtils.httpReq(
  72. 'PUT',
  73. '/aswSubscription?wid='+__wid,
  74. undefined,
  75. function(statusCode,resp)
  76. {
  77. console.debug(statusCode);
  78. console.debug(resp);
  79. __aswid = utils.jsonp(resp)['data'];
  80. _getUserPreferences(
  81. function(prefs)
  82. {
  83. console.debug("Get User Preferences in Init.js (96)");
  84. console.debug(prefs);
  85. __prefs = prefs;
  86. prefs['autoloaded-toolbars']['value'].
  87. forEach(_loadToolbar);
  88. if( prefs['autoloaded-model']['value'] != '' )
  89. _loadModel(prefs['autoloaded-model']['value']);
  90. Collaboration.enableCollaborationLinks();
  91. __launchAutosave();
  92. });
  93. });
  94. else if( 'aswid' in params && 'cswid' in params )
  95. {
  96. __user = params['host'];
  97. HttpUtils.httpReq(
  98. 'PUT',
  99. '/aswSubscription?wid='+__wid,
  100. {'aswid':params['aswid'],
  101. 'cswid':params['cswid']},
  102. function(statusCode,resp)
  103. {
  104. resp = utils.jsonp(resp);
  105. __handleState(resp['data'],resp['sequence#']);
  106. Collaboration.enableCollaborationLinks();
  107. __launchAutosave();
  108. });
  109. }
  110. else
  111. {
  112. __wid = params['cswid'];
  113. __user = params['host'];
  114. HttpUtils.httpReq(
  115. 'GET',
  116. '/current.state?wid='+params['cswid'],
  117. undefined,
  118. function(statusCode,resp)
  119. {
  120. resp = utils.jsonp(resp);
  121. __handleState(resp['data'],resp['sequence#']);
  122. Collaboration.enableCollaborationLinks();
  123. __launchAutosave();
  124. });
  125. }
  126. }
  127. else
  128. WindowManagement.openDialog(__FATAL_ERROR, 'failed to connect to back-end');
  129. }
  130. else
  131. __handleChangelog(
  132. msg['data']['changelog'],
  133. msg['data']['sequence#'],
  134. msg['data']['hitchhiker']);
  135. });
  136. socket.on('disconnect',
  137. function()
  138. {
  139. WindowManagement.openDialog(__FATAL_ERROR, 'lost connection to back-end');
  140. });
  141. socket.on('connect',
  142. function()
  143. {
  144. if( window.location.search == '' ||
  145. ('aswid' in params && 'cswid' in params) )
  146. HttpUtils.httpReq(
  147. 'POST',
  148. '/csworker',
  149. undefined,
  150. function(statusCode,resp)
  151. {
  152. __wid = resp;
  153. socket.emit(
  154. 'message',
  155. {'method':'POST','url':'/changeListener?wid='+__wid});
  156. });
  157. else if( 'cswid' in params )
  158. socket.emit(
  159. 'message',
  160. {'method':'POST','url':'/changeListener?wid='+params['cswid']});
  161. else
  162. WindowManagement.openDialog(__FATAL_ERROR, 'invalid URL parameters '+
  163. utils.jsons(params));
  164. });
  165. /** PART 2 **/
  166. $('#a_logout').title = 'logout '+__user;
  167. __canvas = Raphael(GUIUtils.$$('div_canvas'),__CANVAS_SIZE,__CANVAS_SIZE);
  168. __canvas.canvas.setAttribute('vector-effect','non-scaling-stroke');
  169. __canvas.canvas.setAttribute('xmlns:xlink','http://www.w3.org/1999/xlink');
  170. BehaviorManager.setActiveBehaviourStatechart(__SC_DIALOG,true);
  171. BehaviorManager.setActiveBehaviourStatechart(__SC_CANVAS,true);
  172. WindowManagement.setWindowTitle();
  173. window.onbeforeunload =
  174. /* prompt on non-logout exit */
  175. function(ev)
  176. {
  177. if( __user == undefined )
  178. return;
  179. else if( __prefs['confirm-exit']['value'] && ! __isSaved() )
  180. return __EXITWARNING;
  181. };
  182. }