ArticleDirectory
- How does it work
- Simple Object Replication
- How was it built?
- Creating bytearray P2P file sharing
- Where to go from here
A very good explanation of the use of Flash Player 10.1 P2P features to achieve file sharing article, in order to prevent the original text by the wall, reproduced in this, the original source: http://www.flashrealtime.com/file-share-object-replication-flash-p2p/
Object Replication
Object replication is the most lowest-level P2P access available in Flash Player 10.1 (followed by multicast, posting and directed Routing ). it basically enables you to send chunks of data between peers. object replication is the only P2P access method that guarantees that all data will be transferred to all processing peers.
Demo
I 've built this simple file sharing application, which basically loads a file and then you start sharing it. Open another client to start refreshing the file.
How to use it:
Open a provider in one window-browse for a file (JPG, PNG, GIF ). once it's loaded, it will start sharing the file. open a login er in your other windows and start logging ing. provider and receiver er are supported in one app in this example.
How does it work
A classic scenario for object replication in Flash is file sharing. you have two clients, one is sending the data (provider) and the other one has es the data (receiver ). you were able to do this already in Flash Player 10 using netstream-but this worked only for two clients and there where no replication of objects to the members of a group => massive file sharing! In our scenario, you can have thousands of receivers.
provider
provides data for others. this is the originator. first you need to have an object with data you want to share. you most probably will load a file using urlstream or filereference. then you need to split this file into separate bytearray chunks and give them indexes (it can be an indexed array ). keep the chunks reasonably small to avoid transfer issues (around 64 KB ). so if you load a 2 MB file, you will have 32 chunks. finally call netgroup. addhaveobject (0, 32); which says you have in this case 32 chunks available for others.
receiver
es data from a provider. here you just call netgroup. addwantobjects (index, index); and start processing ing objects from the provider. I do this by keeping the increasing the index by 1 once stored ed a chunk. so you basically call netgroup. addwantobjects (index, index); 32 times. when you call addwantobjects, the providers gets a status "netgroup. replication. request ". at this point the provider needs to write data to a group using netgroup. writerequestedobject (event.info. requestid, Chunks [event.info. index]) . once it writes the data, the caller gets a "netgroup. replication. fetch. result " Status event and save the data locally to an object. remember, that the specified er is just processing data, it is not providing the data to other peers. after it has stored ed all chunks, the specified er just goes through the chunks and put them together into a final bytearray.
Explorer/provider
So why not to provide the stored ed data to other peers to make the trasfer faster and maybe more stable. Once you receive some data usingNetgroup. addwantobjects (index, index );And save them in"Netgroup. Replication. Fetch. Result", You can start providing the data to other peers usingNetgroup. addhaveobject (index, index );.
Of course there is lot more to be done, but first let's have a look at this schema for the above.
Simple Object Replication
To demonstrate how object replication works, let's try this second demo. in this example you have an object, which we fill with and array of 100 elements. then start sharing this array. run the second client to start refreshing ing the array.
Provider peer:
1. Once connected, clickFillobject
2. Then clickAddhaveobjects
3. That's all
Receiver peer:
1. Once connected, clickAddwantobjects
2. It shocould start refreshing objects shortly
(There is a loop, first chunk you receive is Count of objects, once you receive a chunk the index increases by 1 and asks for next chunk until they are all stored ed)
There are couple more buttons-you can try playing with it a little bit if you want.
How was it built?
Once connected to a server setup a netgroup instance like this:
Private FunctionSetupgroup():Void{
VaRSPEC: groupspecifier =NewGroupspecifier("Mygroup");
Spec.Serverchannelenabled=True;
Spec.Objectreplicationenabled=True;
Netgroup =NewNetgroup(Netconnection, Spec.Groupspecwithauthorizations());
Netgroup.Addeventlistener(Netstatusevent.Net_status, Netstatus);
}
Once connected to a netgroup, which means that user allowed P2P connections, UDP is enabled and so on-you do operations on a netgroup. first set object replication strategy. we will be using ing packets one by one (moreless ).
Netgroup.Replicationstrategy= Netgroupreplicationstrategy.Lowest_first;
In your netstatushandler-catch two codes:
// This code is called on a provider
Case "Netgroup. Replication. Request":
// Calling this causes "netgroup. Replication. Fetch. Result" invocation on a receiver
Netgroup.Writerequestedobject(Event.Info.Requestid, OBJ[Event.Info.Index])
Break;
// This code is called on a receiver
Case "Netgroup. Replication. Fetch. Result":
// Received chunks can be already provided to others
Netgroup.Addhaveobjects(Event.Info.Index, Event.Info.Index);
// Write a chunk into an object/Array
OBJ[Event.Info.Index]= Event.Info.Object;
If(Event.Info.Index=0){
// First chunk (0) holds the number of chunks
Objsize =Number(Event.Info.Object);}
Else{
// Receive chunks until you are full
If(Event.Info.Index+1<Objsize){
Netgroup.Addwantobjects(Event.Info.Index+1, Event.Info.Index+1);
Actualfetchindex = event.Info.Index+1;
}
}
Break;
The whole source code can be found here.
Creating bytearray P2P file sharing
By following the concept above it's possible to load a file from disk or URL and then start sharing it with others.
This explains how the first demo works.
For this I 've split the application into four different classes:
Localfileloader.
Loads a file using filereference and splits it into chunks (~ 64 kB each ).
P2pfileshare.
Connects to Cirrus and handles all object replication sending and processing ing
P2pdomaindobject.
A simple value object, which holds the data (bytearray), size, packetlenght, actualfetchindex and chunks; it's used by both classes above.
P2pfilesharing. mxml
The user interface, which puts it all together.
The complete source code can be found here.
TheProviderShocould look like this after sending the data:
TheCyclerShocould look like this after processing the data:
In the next tutorial, I will look at how to use object replication with VOD video.
Where to go from here
Check other tutorials on P2P in flash:
-Video-on-Demand over peer-to-peer (P2P) in Flash Player 10.1 with object Replication
-P2P groupspecifier class explained in details Part 1
-Multicast explained in Flash 10.1 P2P
-Directed routing explained in Flash 10.1 P2P
-Simple chat with P2P netgroup in FP 10.1
Video Tutorials:
-P2P chat with netgroup in Flash players 10.1
-Multicast streaming in Flash Player 10.1 tutorial