PageRenderTime 63ms CodeModel.GetById 36ms RepoModel.GetById 1ms app.codeStats 0ms

/SharedSlidesMobile/src/com/jxl/sharedslides/rl/models/NetworkModel.as

https://github.com/JesterXL/SharedSlideshow
ActionScript | 361 lines | 298 code | 54 blank | 9 comment | 29 complexity | 226a3a1ccbb81a4a3086703eb9110382 MD5 | raw file
  1. package com.jxl.sharedslides.rl.models
  2. {
  3. import com.adobe.crypto.MD5;
  4. import com.jxl.sharedslides.events.model.NetworkModelEvent;
  5. import com.jxl.shareslides.vo.SlideshowVO;
  6. import com.projectcocoon.p2p.LocalNetworkDiscovery;
  7. import com.projectcocoon.p2p.events.ClientEvent;
  8. import com.projectcocoon.p2p.events.GroupEvent;
  9. import com.projectcocoon.p2p.events.MessageEvent;
  10. import com.projectcocoon.p2p.events.ObjectEvent;
  11. import com.projectcocoon.p2p.vo.ObjectMetadataVO;
  12. import flash.events.Event;
  13. import flash.utils.ByteArray;
  14. import flash.utils.getTimer;
  15. import mx.collections.ArrayCollection;
  16. import org.robotlegs.mvcs.Actor;
  17. public class NetworkModel extends Actor
  18. {
  19. [Inject]
  20. public var localNetworkDiscovery:LocalNetworkDiscovery;
  21. private var _connected:Boolean = false;
  22. private var _currentSlide:int = 0;
  23. private var awaitingSlideshows:Object = {};
  24. public function get connected():Boolean { return _connected; }
  25. public function set connected(value:Boolean):void
  26. {
  27. _connected = value;
  28. if(_connected)
  29. {
  30. dispatch(new NetworkModelEvent(NetworkModelEvent.GROUP_CONNECTED));
  31. }
  32. else
  33. {
  34. dispatch(new NetworkModelEvent(NetworkModelEvent.GROUP_DISCONNECTED));
  35. }
  36. }
  37. public function get currentSlide():int { return _currentSlide; }
  38. public function set currentSlide(value:int):void
  39. {
  40. if(value !== _currentSlide)
  41. {
  42. _currentSlide = value;
  43. dispatch(new NetworkModelEvent(NetworkModelEvent.HOST_CURRENT_SLIDE_INDEX_CHANGED));
  44. }
  45. }
  46. [Bindable(event="clientsChange")]
  47. public function get clients():ArrayCollection { return localNetworkDiscovery.clients; }
  48. [Bindable(event="sharedObjectsChange")]
  49. public function get sharedObjects():ArrayCollection { return localNetworkDiscovery.sharedObjects; }
  50. [Bindable(event="receivedObjectsChange")]
  51. public function get receivedObjects():ArrayCollection { return localNetworkDiscovery.receivedObjects; }
  52. public var currentSlideshowHash:String;
  53. /*
  54. [Bindable(event="nearIDChanged")]
  55. public function get nearID():String { return localNetworkDiscovery.connection.nearID; }
  56. */
  57. public function NetworkModel()
  58. {
  59. super();
  60. }
  61. [PostConstruct]
  62. public function connect():void
  63. {
  64. Debug.log("NetworkModel::init");
  65. localNetworkDiscovery.loopback = false;
  66. localNetworkDiscovery.groupName = "com.jxl.shareslides";
  67. localNetworkDiscovery.addEventListener(GroupEvent.GROUP_CONNECTED, onGroupConnected);
  68. localNetworkDiscovery.addEventListener(GroupEvent.GROUP_CLOSED, onGroupClosed);
  69. localNetworkDiscovery.addEventListener(ObjectEvent.OBJECT_ANNOUNCED, onObjectAnnounced);
  70. localNetworkDiscovery.addEventListener(ObjectEvent.OBJECT_PROGRESS, onObjectProgress);
  71. localNetworkDiscovery.addEventListener(ObjectEvent.OBJECT_COMPLETE, onObjectComplete);
  72. localNetworkDiscovery.addEventListener("sharedObjectsChange", onSharedObjectsChanged);
  73. //localNetworkDiscovery.addEventListener("receivedObjectsChange", onReceivedObjectsChanged);
  74. //localNetworkDiscovery.addEventListener("clientsConnectedChange", onClientsConnectedChanged);
  75. //localNetworkDiscovery.addEventListener("clientsChange", onClientsChanged);
  76. localNetworkDiscovery.addEventListener(ClientEvent.CLIENT_ADDED, onClientAdded);
  77. localNetworkDiscovery.addEventListener(ClientEvent.CLIENT_UPDATE, onClientUpdate);
  78. localNetworkDiscovery.addEventListener(ClientEvent.CLIENT_REMOVED, onClientRemoved);
  79. localNetworkDiscovery.addEventListener(MessageEvent.DATA_RECEIVED, onMessageEvent);
  80. if(localNetworkDiscovery.clientName == "" || localNetworkDiscovery.clientName == null)
  81. localNetworkDiscovery.clientName = "Default User";
  82. Debug.log("NetworkModel::connect");
  83. localNetworkDiscovery.connect();
  84. }
  85. public function changeName(name:String):void
  86. {
  87. if(localNetworkDiscovery.clientName != name)
  88. localNetworkDiscovery.clientName = name;
  89. }
  90. public function shareSlideshow(slideshow:SlideshowVO):void
  91. {
  92. slideshow.updateHashIfNeeded();
  93. localNetworkDiscovery.shareWithAll(slideshow, {name: slideshow.name, hash: slideshow.hash});
  94. }
  95. public function setHostCurrentIndex(slideshowHash:String, index:int):void
  96. {
  97. localNetworkDiscovery.sendMessageToAll({message: "setCurrentSlide", currentSlide: index, slideshowHash: slideshowHash});
  98. }
  99. private function onGroupConnected(event:GroupEvent):void
  100. {
  101. Debug.info("NetworkModel::onGroupConnected");
  102. connected = true;
  103. Debug.info("localNetworkDiscovery.clientName: " + localNetworkDiscovery.clientName + ", nearID: " + localNetworkDiscovery.connection.nearID);
  104. }
  105. private function onGroupClosed(event:GroupEvent):void
  106. {
  107. Debug.info("NetworkModel::onGroupClosed");
  108. connected = false;
  109. }
  110. private function onObjectAnnounced(event:ObjectEvent):void
  111. {
  112. Debug.info("NetworkModel::onObjectAnnounced, hash: " + event.metadata.info.hash);
  113. if(awaitingSlideshows[event.metadata.info.hash] != null)
  114. {
  115. Debug.info("I was awaiting this slideshow, thank you, requesting...");
  116. delete awaitingSlideshows[event.metadata.info.hash];
  117. localNetworkDiscovery.requestObject(event.metadata);
  118. }
  119. else
  120. {
  121. // verify I don't have it
  122. var len:int = localNetworkDiscovery.receivedObjects.length;
  123. var om:ObjectMetadataVO;
  124. var slideshow:SlideshowVO;
  125. while(len--)
  126. {
  127. om = localNetworkDiscovery.receivedObjects[len];
  128. if(om.info.hash == event.metadata.info.hash)
  129. {
  130. Debug.info("Thanks, I already have this slideshow in received.");
  131. return;
  132. }
  133. }
  134. len = localNetworkDiscovery.sharedObjects.length;
  135. while(len--)
  136. {
  137. om = localNetworkDiscovery.sharedObjects[len];
  138. if(om.info.hash == event.metadata.info.hash)
  139. {
  140. Debug.info("Thanks, I already have this slideshow, I'm hosting it.");
  141. return;
  142. }
  143. }
  144. Debug.info("I have no record of requesting this, so thanks, requesting it now!");
  145. localNetworkDiscovery.requestObject(event.metadata);
  146. }
  147. }
  148. private function onObjectProgress(event:ObjectEvent):void
  149. {
  150. localNetworkDiscovery.receivedObjects.refresh();
  151. }
  152. private function onObjectComplete(event:ObjectEvent):void
  153. {
  154. Debug.debug("NetworkModel::onObjectComplete");
  155. // [jwarden 12.30.2011] HACK: My hashes aren't matching up... whether MD5 or SHA256.
  156. Debug.debug("hash on info: "+ event.metadata.info.hash);
  157. var slideshow:SlideshowVO = event.metadata.object as SlideshowVO;
  158. slideshow.hash = event.metadata.info.hash;
  159. localNetworkDiscovery.receivedObjects.refresh();
  160. }
  161. private function onSharedObjectsChanged(event:Event):void
  162. {
  163. Debug.info("NetworkModel::onSharedObjectsChanged");
  164. dispatch(new Event("sharedObjectsChange"));
  165. }
  166. private function onClientAdded(event:ClientEvent):void
  167. {
  168. Debug.info("NetworkModel::onClientAdded, event.client.clientName: " + event.client.clientName);
  169. if(event.client.isLocal)
  170. return;
  171. var hash:Object = {};
  172. var len:int = localNetworkDiscovery.receivedObjects.length;
  173. var om:ObjectMetadataVO;
  174. var slideshow:SlideshowVO;
  175. while(len--)
  176. {
  177. om = localNetworkDiscovery.receivedObjects[len];
  178. hash[om.info.hash] = om.info.hash;
  179. }
  180. len = localNetworkDiscovery.sharedObjects.length;
  181. while(len--)
  182. {
  183. om = localNetworkDiscovery.sharedObjects[len];
  184. hash[om.info.hash] = om.info.hash;
  185. }
  186. var hashes:Array = [];
  187. for(var prop:String in hash)
  188. {
  189. hashes.push(prop);
  190. }
  191. localNetworkDiscovery.sendMessageToClient({message: "doYouNeedSlideshow", hashes: hashes}, event.client.groupID);
  192. dispatch(new Event("clientAdded"));
  193. }
  194. private function onClientUpdate(event:ClientEvent):void
  195. {
  196. Debug.info("NetworkModel::onClientUpdate, clientName: " + event.client.clientName);
  197. if(event.client.isLocal)
  198. return;
  199. dispatch(new Event("clientUpdate"));
  200. }
  201. private function onClientRemoved(event:ClientEvent):void
  202. {
  203. Debug.info("NetworkModel::onClientRemoved, clientName: " + event.client.clientName);
  204. if(event.client.isLocal)
  205. return;
  206. dispatch(new Event("clientRemoved"));
  207. }
  208. private function onMessageEvent(event:MessageEvent):void
  209. {
  210. Debug.info("NetworkModel::onMessageEvent");
  211. if(event.message.data)
  212. Debug.info("\tmessage: " + event.message.data.message);
  213. var networkModelEvent:NetworkModelEvent;
  214. switch(event.message.data.message)
  215. {
  216. case "setCurrentSlide":
  217. Debug.info("currentSlideshowHash: " + currentSlideshowHash);
  218. Debug.info("event.message.data.slideshowHash: " + event.message.data.slideshowHash);
  219. if(currentSlideshowHash == event.message.data.slideshowHash)
  220. currentSlide = event.message.data.currentSlide;
  221. break;
  222. case "doYouNeedSlideshow":
  223. onVerifyIfINeedSlideshows(event);
  224. break;
  225. case "doYouNeedSlideshowAck":
  226. onSendNeededSlideshows(event);
  227. break;
  228. }
  229. }
  230. private function onVerifyIfINeedSlideshows(messageEvent:MessageEvent):void
  231. {
  232. var hashes:Array = messageEvent.message.data.hashes as Array;
  233. var missing:Array = [];
  234. var len:int = hashes.length;
  235. var hashObject:Object = {};
  236. while(len--)
  237. {
  238. var hashString:String = hashes[len];
  239. hashObject[hashString] = false;
  240. }
  241. var om:ObjectMetadataVO;
  242. var slideshow:SlideshowVO;
  243. len = localNetworkDiscovery.receivedObjects.length;
  244. while(len--)
  245. {
  246. om = localNetworkDiscovery.receivedObjects[len];
  247. hashObject[om.info.hash] = true;
  248. }
  249. len = localNetworkDiscovery.sharedObjects.length;
  250. while(len--)
  251. {
  252. om = localNetworkDiscovery.sharedObjects[len];
  253. slideshow = om.object as SlideshowVO;
  254. slideshow.updateHashIfNeeded();
  255. hashObject[slideshow.hash] = true;
  256. }
  257. for(var prop:String in hashObject)
  258. {
  259. if(hashObject[prop] == false)
  260. {
  261. missing.push(prop);
  262. awaitingSlideshows[prop] = prop;
  263. }
  264. }
  265. if(missing.length > 0)
  266. {
  267. localNetworkDiscovery.sendMessageToClient({message: "doYouNeedSlideshowAck", missing: missing}, messageEvent.message.client.groupID);
  268. }
  269. }
  270. private function onSendNeededSlideshows(event:MessageEvent):void
  271. {
  272. var missing:Array = event.message.data.missing as Array;
  273. var missingHash:Object = {};
  274. var len:int = missing.length;
  275. var hash:String;
  276. while(len--)
  277. {
  278. hash = missing[len] as String;
  279. missingHash[hash] = true;
  280. }
  281. len = localNetworkDiscovery.receivedObjects.length;
  282. var om:ObjectMetadataVO;
  283. var slideshow:SlideshowVO;
  284. while(len--)
  285. {
  286. om = localNetworkDiscovery.receivedObjects[len];
  287. slideshow = om.object as SlideshowVO;
  288. if(missingHash[slideshow.hash] == true)
  289. {
  290. missingHash[slideshow.hash] = false;
  291. localNetworkDiscovery.shareWithClient(slideshow, event.message.client.groupID, {name: slideshow.name, hash: slideshow.hash});
  292. }
  293. }
  294. len = localNetworkDiscovery.sharedObjects.length;
  295. while(len--)
  296. {
  297. om = localNetworkDiscovery.sharedObjects[len];
  298. slideshow = om.object as SlideshowVO;
  299. if(missingHash[slideshow.hash] == true)
  300. {
  301. missingHash[slideshow.hash] = false;
  302. localNetworkDiscovery.shareWithClient(slideshow, event.message.client.groupID, {name: slideshow.name, hash: slideshow.hash});
  303. }
  304. }
  305. }
  306. }
  307. }