The most simple P2P Object Replication Example

Here you can download the simplest P2P Object Replication example, which is easy to understand. Previous examples might have been too complicated to start with. Grab this code and start using it right the way.

Download source
Try demo – open in two or more windows. Click “startProviding” button on one of them and “startReceiving” on the rest of the windows. You can then click “writeObject” to check the received object on the receiver windows.

Good Luck!

objrep-simple

<?xml version="1.0" encoding="utf-8"?>
<!-- Created by Tom Krcha, Adobe http://flashrealtime.com -->
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
			   xmlns:s="library://ns.adobe.com/flex/spark"
			   xmlns:mx="library://ns.adobe.com/flex/mx" applicationComplete="init()" width="413">
 
<fx:Script>
	<![CDATA[
	private const SERVER:String = "rtmfp://stratus.adobe.com/";
	private const DEVKEY:String = "YOUR-DEVKEY"; // Get one here: http://labs.adobe.com/technologies/stratus/
 
	[Bindable]
	private var connected:Boolean = false;
 
	private var netConnection:NetConnection;
 
	[Bindable]
	private var netGroup:NetGroup;
 
	[Bindable]
	private var obj:Object = new Object();
	private var objSize:Number = 0;
 
	public function init():void{
		connect();
	}
 
	public function connect():void{
		writeText("Connecting...");
 
		netConnection = new NetConnection();
		netConnection.addEventListener(NetStatusEvent.NET_STATUS,netStatus);
		netConnection.connect(SERVER+DEVKEY);
	}
 
	public function startProviding():void{
		writeText("startProviding");
		fillObject()
		netGroup.addHaveObjects(0,99);
	}
 
	public function startReceiving():void{
		writeText("startReceiving");
		netGroup.addWantObjects(0,99);
	}
 
	protected function netStatus(event:NetStatusEvent):void{
		switch(event.info.code){
			case "NetConnection.Connect.Success":
				writeText(event.info.code);
				setupGroup();
				break;
 
			case "NetGroup.Connect.Success":
				writeText(event.info.code);
				connected = true;
 
				netGroup.replicationStrategy = NetGroupReplicationStrategy.LOWEST_FIRST;
 
				break;
 
			case "NetGroup.Neighbor.Connect":
				writeText(event.info.code);
				break;
 
			case "NetGroup.Replication.Fetch.SendNotify": // e.info.index
				writeText("____ index: "+event.info.index);
 
				break;
 
			case "NetGroup.Replication.Fetch.Failed": // e.info.index
				writeText("FAIL ____ index: "+event.info.index);
 
				break;
 
			case "NetGroup.Replication.Fetch.Result": // e.info.index, e.info.object
 
				// Write received object
				obj[event.info.index] = event.info.object;
 
				// Providing received object to the others (optional)
				netGroup.addHaveObjects(event.info.index,event.info.index);
 
				break;
 
			case "NetGroup.Replication.Request": // e.info.index, e.info.requestID
				netGroup.writeRequestedObject(event.info.requestID,obj[event.info.index])
 
				break;
 
			default:
				break;
		}
	}
 
 
	protected function setupGroup():void{
		var spec:GroupSpecifier = new GroupSpecifier("myObjectReplicationGroup");
		spec.serverChannelEnabled = true;
		spec.objectReplicationEnabled = true;
 
		netGroup = new NetGroup(netConnection,spec.groupspecWithAuthorizations());
		netGroup.addEventListener(NetStatusEvent.NET_STATUS,netStatus);
 
	}
 
	// writes to output
	protected function writeText(txt:String):void{
		txtHistory.appendText(txt+"\n");
	}
 
	// fills dummy test object
	private function fillObject():void{
		objSize = 100;
		obj[0] = objSize;
		for(var i:int=1;i<objSize;i++){
			obj[i] = "data"+i;
		}
 
		txtObj.text = getObj();
 
	}
 
	// writes object content to the output
	protected function writeObject():void{
 
		txtObj.text = getObj();
	}
 
	// 
	private function getObj():String{
		var str:String = "";
 
		for(var i:String in obj){
			str+=""+i+":"+obj[i]+" | ";
		}
 
		return str;
	}
 
 
 
	]]>
</fx:Script>
 
<s:TextArea left="114" right="11" top="28" id="txtHistory" height="266"/>
<s:Label x="124" y="12" text="log" />
<s:Button x="10" y="330" label="writeObject" click="writeObject()" width="97" enabled="{connected}"/>
<s:Button x="9" y="13" label="startProviding" click="startProviding()" enabled="{connected}" width="97"/>
<s:Button x="9" y="42" label="startReceiving" click="startReceiving()" enabled="{connected}"/>
<s:TextArea y="330" height="83" left="115" right="10" id="txtObj"/>
<s:Label x="122" y="310" text="object"  />
 
</s:Application>

Show Comments

Hide Comments

Comments are closed.