package { import flash.events.Event; import flash.events.EventDispatcher; import flash.events.NetStatusEvent; import flash.events.StatusEvent; import flash.net.GroupSpecifier; import flash.net.NetConnection; import flash.net.NetGroup; import flash.net.NetGroupReplicationStrategy; import flash.utils.ByteArray; [Event(name="complete",type="flash.events.Event")] [Event(name="status",type="flash.events.StatusEvent")] public class P2PFileShare extends EventDispatcher { [Bindable] public var connected:Boolean = false; public var netConnection:NetConnection; [Bindable] public var netGroup:NetGroup; private const SERVER:String = "rtmfp://stratus.adobe.com/"; private const DEVKEY:String = "YOUR-DEV-KEY"; public var p2pSharedObject:P2PSharedObject; public function P2PFileShare() { } public function connect():void{ netConnection = new NetConnection(); netConnection.addEventListener(NetStatusEvent.NET_STATUS,netStatus); netConnection.connect(SERVER+DEVKEY); } public function startSharing(p2pSharedObject:P2PSharedObject):void{ writeText("startSharing - chunks shared: "+p2pSharedObject.packetLenght); this.p2pSharedObject = p2pSharedObject; netGroup.addHaveObjects(0,p2pSharedObject.packetLenght); } public function startReceiving():void{ writeText("startReceiving"); p2pSharedObject = new P2PSharedObject(); p2pSharedObject.chunks = new Object(); receiveObject(0); } protected function onGroupConnected():void{ netGroup.replicationStrategy = NetGroupReplicationStrategy.LOWEST_FIRST; } protected function netStatus(event:NetStatusEvent):void{ writeText(event.info.code); switch(event.info.code){ case "NetConnection.Connect.Success": setupGroup(); break; case "NetGroup.Connect.Success": connected = true; onGroupConnected(); break; case "NetGroup.Replication.Fetch.SendNotify": // e.info.index writeText("____ index: "+event.info.index); break; case "NetGroup.Replication.Fetch.Failed": // e.info.index writeText("____ index: "+event.info.index); break; case "NetGroup.Replication.Fetch.Result": // e.info.index, e.info.object //writeText("____ index: "+event.info.index+" | object: "+event.info.object); netGroup.addHaveObjects(event.info.index,event.info.index); p2pSharedObject.chunks[event.info.index] = event.info.object; if(event.info.index == 0){ p2pSharedObject.packetLenght = Number(event.info.object); writeText("p2pSharedObject.packetLenght: "+p2pSharedObject.packetLenght); receiveObject(1); }else{ if(event.info.index+1