Fms4 P2P: Image Sharing

Source: Internet
Author: User

This is actually the revision of the file sharing example in the http://www.flashrealtime.com/file-share-object-replication-flash-p2p/, the original example is based on flex, I changed to the Flash version (the general principle is exactly the same as the previous ):

There are three basic classes:

1. p2pdomaindobject. As is used to define the (image) data class to be shared.

 
Package P2P {import flash. utils. bytearray; public class p2p;dobject {public var size: Number = 0; Public var packetlenght: uint = 0; Public var actualfetchindex: Number = 0; Public var data: bytearray; Public var chunks: object = new object (); Public Function p2p1_dobject (){}}}

2. localfileloader. As is used to browse local images and load the images to the stage. The images are automatically divided into one block in about 64000 bytes.

Package P2P {import flash. events. event; import flash. events. eventdispatcher; import flash. events. ioerrorevent; import flash. events. progressevent; import flash. events. securityerrorevent; import flash. events. statusevent; import flash.net. filereference; import flash. utils. bytearray; public class localfileloader extends eventdispatcher {public function localfileloader () {} private var file: filereference; Public var p2p1_dobject: p2p1_dobject; Public Function browsefilesystem (): void {file = new filereference (); file. addeventlistener (event. select, selecthandler); file. addeventlistener (ioerrorevent. io_error, ioerrorhandler); file. addeventlistener (progressevent. progress, progresshandler); file. addeventlistener (securityerrorevent. security_error, securityerrorhandler) file. addeventlistener (event. complete, completehandler); file. browse ();} protected function selecthandler (Event: Event): void {writetext ("filechosen"); writetext (file. name + "|" + file. size); file. load ();} protected function ioerrorhandler (Event: ioerrorevent): void {writetext ("ioerrorhandler:" + Event);} protected function securityerrorhandler (Event: securityerrorevent ): void {writetext ("securityerror:" + Event);} protected function progresshandler (Event: progressevent): void {var file: filereference = progress get); writetext ("progresshandler: bytesloaded = "+ event. bytesloaded + "/" + event. bytestotal);} protected function completehandler (Event: Event): void {writetext ("completehandler"); p2p;dobject = new p2p;dobject (); p2p;dobject. size = file. size; p2p1_dobject. packetlenght = math. floor (file. size/64000) + 1; p2p1_dobject. data = file. data; p2p1_dobject. chunks = new object (); p2p?dobject. chunks [0] = p2p1_dobject. packetlenght + 1; for (var I: Int = 1; I <p2p1_dobject. packetlenght; I ++) {p2p1_dobject. chunks [I] = new bytearray (); p2p1_dobject. data. readbytes (p2p1_dobject. chunks [I],);} // + 1 last packetp2p1_dobject. chunks [p2p1_dobject. packetlenght] = new bytearray (); p2p1_dobject. data. readbytes (p2p1_dobject. chunks [I], 0, p2p1_dobject. data. bytesavailable); p2p1_dobject. packetlenght + = 1; writetext ("----- p2p;dobject -----"); writetext ("packetlenght:" + (p2p;dobject. packetlenght); dispatchevent (new event (event. complete);} protected function writetext (STR: string): void {var E: statusevent = new statusevent (statusevent. status, false, false, "status", STR); dispatchevent (e );}}}

3. p2pfileshare. As is used to process P2P file sharing (that is, sending one peer to another).

Package P2P {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; public class p2pfileshare extends eventdispatcher {public var connected: Boolean = false; Public var netconnection: netconnection; Public var netgroup: netgroup; private const server: String = "rtmfp: // localhost/"; private const devkey: String =" helloserver "; Public var p2p1_dobject: p2p1_dobject; Public Function p2pfileshare () {} public function connect (): void {netconnection = new netconnection (); netconnection. addeventlistener (netstatusevent. net_status, netstatus); netconnection. connect (SERVER + devkey);} public function startsharing (p2p;dobject: p2p;dobject): void {triggerevent ("startsharing-Chunks shared:" + p2p;dobject. packetlenght); this. p2pdomaindobject = p2pdomaindobject; netgroup. addhaveobjects (0, p2p1_dobject. packetlenght);} public function startreceiving (): void {triggerevent ("startreceiving"); p2p;dobject = new p2p;dobject (); p2p;dobject. chunks = new object (); receiveobject (0);} protected function ongroupconnected (): void {netgroup. replicationstrategy = netgroupreplicationstrategy. lowest_first;} protected function netstatus (Event: netstatusevent): void {triggerevent (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. sendpolicy ": // e.info. indextriggerevent ("____ index:" + event.info. index); break; Case "netgroup. replication. fetch. failed ": // e.info. indextriggerevent ("____ index:" + event.info. index); break; Case "netgroup. replication. fetch. result ": // e.info. index, e.info. object // triggerevent ("____ index:" + event.info. index + "| object:" + event.info. object); netgroup. addhaveobjects (event.info. index, event.info. index); p2p1_dobject. chunks [event.info. index] = event.info. object; If (event.info. index = 0) {p2p1_dobject. packetlenght = Number (event.info. object); triggerevent ("p2p1_dobject. packetlenght: "+ p2p1_dobject. packetlenght); receiveobject (1);} else {If (event.info. index + 1 <p2p1_dobject. packetlenght) {receiveobject (event.info. index + 1);} else {triggerevent ("processing ing done"); triggerevent ("p2p1_dobject. packetlenght: "+ p2p1_dobject. packetlenght); p2p1_dobject. data = new bytearray (); For (var I: Int = 1; I <p2p1_dobject. packetlenght; I ++) {p2p1_dobject. data. writebytes (p2p1_dobject. chunks [I]);} triggerevent ("p2p1_dobject. data. bytesavailable: "+ p2p1_dobject. data. bytesavailable); triggerevent ("p2p1_dobject. data. length: "+ p2p1_dobject. data. length); dispatchevent (new event (event. complete) ;}} break; Case "netgroup. replication. request ": // e.info. index, e.info. requestidnetgroup. writerequestedobject (event.info. requestid, p2p1_dobject. chunks [event.info. index]) // triggerevent ("____ ID:" + event.info. requestid + ", index:" + event.info. index); break; default: break;} protected function setupgroup (): void {triggerevent ("setupgroup"); var SPEC: groupspecifier = new groupspecifier ("mygroup "); spec. serverchannelenabled = true; spec. objectreplicationenabled = true; netgroup = new netgroup (netconnection, spec. groupspecwithauthorizations (); netgroup. addeventlistener (netstatusevent. net_status, netstatus);} protected function receiveobject (Index: Number): void {netgroup. addwantobjects (index, index); p2p1_dobject. actualfetchindex = index;} protected function triggerevent (STR: string): void {trace ("p2pfilshare. triggerevent is called: str-> ", STR); var E: statusevent = new statusevent (statusevent. status, false, false, "status", STR); dispatchevent (e );}}}

last flash peer:

Package {import FL. controls. button; import FL. controls. textarea; import FL. controls. textinput; import flash. display. stagealign; import flash. display. stagescalemode; import flash. events. event; import flash. events. statusevent; import flash. display. loader; import flash. display. movieclip; import flash. events. mouseevent; import flash.net. netgroupinfo; import P2P. localfileloader; import P2P. p2pfileshare; import P2P. p2pdomaindobject; public class p2p_fileshare extends movieclip {private VaR _ btnbrowse: button; private VaR _ btnshare: button; private VaR _ btnreceive: button; private VaR _ txtoutput: textarea; private VaR _ txtreceive: textinput; private VaR _ txtsend: textinput; private VaR _ localfileloader: localfileloader; private VaR _ Loader: loader; private VaR _ fileshare: p2pfileshare; Public Function p2p_fileshare () {// constructor codeinit ();} private function Init (): void {This. stage. align = stagealign. top_left; this. stage. scalemode = stagescalemode. no_scale; this. _ localfileloader = new localfileloader (); this. _ localfileloader. addeventlistener (statusevent. status, onstatus); this. _ localfileloader. addeventlistener (event. complete, fileloadercomplete); _ fileshare = new p2pfileshare (); _ fileshare. addeventlistener (statusevent. status, onstatus); _ fileshare. addeventlistener (event. complete, file1_complete); this. _ fileshare. connect (); this. _ loader = new loader (); addchild (_ loader); _ loader. X = 218; _ loader. y = 43.35; this. _ btnbrowse = btnbrowse; this. _ btnshare = btnstartshare; this. _ btnreceive = btnreceive; this. _ txtoutput = txtoutput; this. _ txtreceive = txtreceive; this. _ txtsend = txtsend; this. _ btnbrowse. addeventlistener (mouseevent. click, _ btnbrowse_click); this. _ btnreceive. addeventlistener (mouseevent. click, _ btnreceive_click); this. _ btnshare. addeventlistener (mouseevent. click, _ btnshare_click);} private function onstatus (Event: statusevent): void {writetext (event. level); If (event. level = "netgroup. connect. success ") {_ btnshare. enabled = false; _ btnreceive. enabled = true;} Try {refreshinfo ();} catch (E: Error) {}} private function fileloadercomplete (Event: Event): void {writetext ("fileloadercomplete "); _ loader. unload (); _ loader. loadbytes (_ localfileloader. p2p1_dobject. data); this. _ fileshare. startsharing (this. _ localfileloader. p2p1_dobject); this. _ btnshare. enabled = true; this. _ btnreceive. enabled = false; this. _ btnbrowse. enabled = false;} private function file1_complete (Event: Event): void {writetext ("file1_complete"); _ loader. unload (); _ loader. loadbytes (_ fileshare. p2p1_dobject. data);} private function writetext (txt: string): void {trace ("p2p_file).writetext called: TXT->", txt); this. _ txtoutput. appendtext (txt + "\ n");} private function _ btnbrowse_click (E: mouseevent): void {This. _ localfileloader. browsefilesystem ();} private function _ btn1__click (E: mouseevent): void {This. _ fileshare. startsharing (this. _ localfileloader. p2p1_dobject); this. _ btnshare. enabled = false;} private function _ btnreceive_click (E: mouseevent): void {This. _ fileshare. startreceiving ();} protected function refreshinfo (): void {var OBJ: netgroupinfo = This. _ filecmd.netgroup.info; _ txtreceive. TEXT = obj. objectreplicationreceivebytespersecond + ""; _ txtsend. TEXT = obj. objectreplicationsendbytespersecond + "";}}}

Run

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.