Loading IP's from XML, load balancing and failover
Hello Everybody the code that I'm working on I guess I can say is almost there but not quite. I have an xml file that contains the ip values (2) to our servers, what I want to accomplish is: 1st read the xml and grab those values, 2nd randomly select one of those ip's and create the rtmp://...path to play the video, 3rd if one of those servers is down then use the other one to play the video and 4th if all servers are down display some sort of message to try again later. I am reading the xml and grabbing those values, I am randomly selecting an ip and creating the rtmp://...path, but where I am having problems is playing the video from the other server. What's happening is that when it hits the box that is down and swithes to the other box it actually show the first frame of the video but it doesn't play. Here is my code in my .as file:
package
{
import flash.display.Sprite;
import fl.video. *;
import flash.events. *;
import flash.net. *;
import flash.utils. *;
import flash.display.DisplayObject;
public class makethemagichappen
{
var appName : String = "";
var fileName : String = "";
var xml : XML;
var total_applications : Number;
var random_application : Number;
var this_application : Number;
var all_servers_are_down : Boolean = false;
var connection : NetConnection;
var vid : FLVPlayback = new FLVPlayback ();
private function readXML (e : Event) : void
{
this.xml = new XML (e.target.data);
this.total_applications = this.xml. *.length ();
this.random_application = Math.floor (this.total_applications * Math.random ());
this.this_application = this.random_application;
connectToServer ("rtmp://" + this.xml.application.@ip [this.this_application] + ":" + this.xml.application.@port [0] + "/" + this.appName + "/" + this.fileName);
}
private function netStatusHandler (event : NetStatusEvent) : void
{
trace ("netStatusHandler: event.info.code = " + event.info.code);
if (event.info.code != "NetConnection.Connect.Success")
{
trace ("netStatusHandler: this_application was = " + this.this_application);
this.this_application = (1 + this.this_application) % this.total_applications;
trace ("netStatusHandler: this_application is now = " + this.this_application);
if (this.this_application != this.random_application)
{
trace ("In NetStatusHandler: " + ("rtmp://" + this.xml.application.@ip [this.this_application] + ":" + this.xml.application.@port [0] + "/" + this.appName + "/" + this.fileName));
connectToServer ("rtmp://" + this.xml.application.@ip [this.this_application] + ":" + this.xml.application.@port [0] + "/" + this.appName + "/" + this.fileName);
}
else
{
trace ("netStatusHandler: ALL SERVERS ARE DOWN");
this.all_servers_are_down = true;
}
}
}
private function errorHandler (event : AsyncErrorEvent) : void
{
// trace ("errorHandler1: " + event.text);
// trace ("errorHandler2: " + event.info.code);
// trace ("errorHandler3: " + event.error);
}
private function securityErrorHandler (event : SecurityErrorEvent) : void
{
trace ("securityErrorHandler: " + event);
}
private function connectToServer (video_source : String)
{
this.vid.source = video_source;
trace ("connectToServer: now connecting to = " + video_source);
this.connection = new NetConnection ();
this.connection.addEventListener (AsyncErrorEvent.ASYNC_ERROR, errorHandler, false, 0, true);
this.connection.addEventListener (NetStatusEvent.NET_STATUS, netStatusHandler, false, 0, true);
this.connection.addEventListener (SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
this.connection.connect (video_source);
}
public function getVideo ()
{
return this.vid;
}
public function loadVideo (appName : String, fileName : String, skinName : String, skinHide : Boolean, skinOpacity : Boolean, Width : int, Height : int, OnStagePosX : int, OnStagePosY : int)
{
var loader : URLLoader = new URLLoader ();
loader.addEventListener (Event.COMPLETE, readXML);
this.appName = appName;
this.fileName = fileName;
this.vid.width = Width;
this.vid.height = Height;
this.vid.x = OnStagePosX;
this.vid.y = OnStagePosY;
this.vid.skinAutoHide = skinHide;
this.vid.skinBackgroundColor = 0x666666;
if (skinOpacity == true)
{
this.vid.skinBackgroundAlpha = 0.2;
}
else
{
this.vid.skinBackgroundAlpha = 1.0;
}
this.vid.skin = "../../../general/flashskins/as3/" + skinName;
if (appName == "pfsweb")
{
loader.load (new URLRequest ("../../../general/other/fmsConfig.xml"));
}
else if (appName == "pol")
{
loader.load (new URLRequest ("../../../general/other/fmsConfig.xml"));
}
else if (appName == "elogic")
{
loader.load (new URLRequest ("../../../general/other/fmsConfig.xml"));
}
else if (appName == "able")
{
loader.load (new URLRequest ("../../../general/other/fmsConfig.xml"));
}
else if (appName == "fieldinst")
{
loader.load (new URLRequest ("../../../general/other/fmsConfig.xml"));
}
else if (appName == "public")
{
loader.load (new URLRequest ("../../../general/other/fmsConfig.xml"));
}
else
{
trace ('loadVideo: Unknown application name!');
}
} // loadVideo
} // makethemagichappen
} // package
I wanted to upload my FLA file but the system wouldn't let me, too big.
|