Changeset View
Changeset View
Standalone View
Standalone View
support/aphlict/client/src/AphlictMaster.as
| Show First 20 Lines • Show All 42 Lines • ▼ Show 20 Lines | final public class AphlictMaster extends Aphlict { | ||||
| /** | /** | ||||
| * A dictionary mapping PHID to subscribed clients. | * A dictionary mapping PHID to subscribed clients. | ||||
| */ | */ | ||||
| private var subscriptions:Dictionary; | private var subscriptions:Dictionary; | ||||
| private var socket:Socket; | private var socket:Socket; | ||||
| private var readBuffer:ByteArray; | private var readBuffer:ByteArray; | ||||
| private var status:String; | |||||
| private var statusCode:String; | |||||
| public function AphlictMaster(server:String, port:Number) { | public function AphlictMaster(server:String, port:Number) { | ||||
| super(); | super(); | ||||
| this.remoteServer = server; | this.remoteServer = server; | ||||
| this.remotePort = port; | this.remotePort = port; | ||||
| this.clients = new Dictionary(); | this.clients = new Dictionary(); | ||||
| Show All 11 Lines | final public class AphlictMaster extends Aphlict { | ||||
| /** | /** | ||||
| * Register a @{class:AphlictClient}. | * Register a @{class:AphlictClient}. | ||||
| */ | */ | ||||
| public function register(client:String):void { | public function register(client:String):void { | ||||
| if (!this.clients[client]) { | if (!this.clients[client]) { | ||||
| this.log('Registering client: ' + client); | this.log('Registering client: ' + client); | ||||
| this.clients[client] = new Date().getTime(); | this.clients[client] = new Date().getTime(); | ||||
| this.send.send(client, 'setStatus', this.status, this.statusCode); | |||||
| } | } | ||||
| } | } | ||||
| /** | /** | ||||
| * Purge stale client connections from the client pool. | * Purge stale client connections from the client pool. | ||||
| */ | */ | ||||
| private function purgeClients(event:TimerEvent):void { | private function purgeClients(event:TimerEvent):void { | ||||
| for (var client:String in this.clients) { | for (var client:String in this.clients) { | ||||
| Show All 15 Lines | final public class AphlictMaster extends Aphlict { | ||||
| * master is still alive. | * master is still alive. | ||||
| */ | */ | ||||
| public function ping(client:String):void { | public function ping(client:String):void { | ||||
| this.clients[client] = new Date().getTime(); | this.clients[client] = new Date().getTime(); | ||||
| this.send.send(client, 'pong'); | this.send.send(client, 'pong'); | ||||
| } | } | ||||
| private function connectToServer():void { | private function connectToServer():void { | ||||
| this.setStatusOnClients('connecting'); | |||||
| var socket:Socket = new Socket(); | var socket:Socket = new Socket(); | ||||
| socket.addEventListener(Event.CONNECT, didConnectSocket); | socket.addEventListener(Event.CONNECT, didConnectSocket); | ||||
| socket.addEventListener(Event.CLOSE, didCloseSocket); | socket.addEventListener(Event.CLOSE, didCloseSocket); | ||||
| socket.addEventListener(ProgressEvent.SOCKET_DATA, didReceiveSocket); | socket.addEventListener(ProgressEvent.SOCKET_DATA, didReceiveSocket); | ||||
| socket.addEventListener(IOErrorEvent.IO_ERROR, didIOErrorSocket); | socket.addEventListener(IOErrorEvent.IO_ERROR, didIOErrorSocket); | ||||
| socket.addEventListener( | socket.addEventListener( | ||||
| SecurityErrorEvent.SECURITY_ERROR, | SecurityErrorEvent.SECURITY_ERROR, | ||||
| didSecurityErrorSocket); | didSecurityErrorSocket); | ||||
| socket.connect(this.remoteServer, this.remotePort); | socket.connect(this.remoteServer, this.remotePort); | ||||
| this.readBuffer = new ByteArray(); | this.readBuffer = new ByteArray(); | ||||
| this.socket = socket; | this.socket = socket; | ||||
| } | } | ||||
| private function didConnectSocket(event:Event):void { | private function didConnectSocket(event:Event):void { | ||||
| this.externalInvoke('connected'); | this.setStatusOnClients('connected'); | ||||
| // Send subscriptions | // Send subscriptions | ||||
| var phids = new Array(); | var phids = new Array(); | ||||
| for (var phid:String in this.subscriptions) { | for (var phid:String in this.subscriptions) { | ||||
| phids.push(phid); | phids.push(phid); | ||||
| } | } | ||||
| if (phids.length) { | if (phids.length) { | ||||
| this.sendSubscribeCommand(phids); | this.sendSubscribeCommand(phids); | ||||
| } | } | ||||
| } | } | ||||
| private function didCloseSocket(event:Event):void { | private function didCloseSocket(event:Event):void { | ||||
| this.externalInvoke('close'); | this.externalInvoke('close'); | ||||
| } | } | ||||
| private function didIOErrorSocket(event:IOErrorEvent):void { | private function didIOErrorSocket(event:IOErrorEvent):void { | ||||
| this.externalInvoke('error', event.text); | this.externalInvoke('error', event.text); | ||||
| } | } | ||||
| private function didSecurityErrorSocket(event:SecurityErrorEvent):void { | private function didSecurityErrorSocket(event:SecurityErrorEvent):void { | ||||
| this.externalInvoke('error', event.text); | var text = event.text; | ||||
| // This is really gross but there doesn't seem to be anything else | |||||
| // on the object which gives us an error code. | |||||
| if (text.match(/^Error #2048/)) { | |||||
| this.setStatusOnClients('error', 'error.flash.xdomain'); | |||||
| } | |||||
| this.error(text); | |||||
| } | } | ||||
| public function subscribe(client:String, phids:Array):void { | public function subscribe(client:String, phids:Array):void { | ||||
| var newPHIDs = new Array(); | var newPHIDs = new Array(); | ||||
| for (var i:String in phids) { | for (var i:String in phids) { | ||||
| var phid = phids[i]; | var phid = phids[i]; | ||||
| if (!this.subscriptions[phid]) { | if (!this.subscriptions[phid]) { | ||||
| ▲ Show 20 Lines • Show All 117 Lines • ▼ Show 20 Lines | private function didReceiveSocket(event:Event):void { | ||||
| break; | break; | ||||
| } | } | ||||
| } while (true); | } while (true); | ||||
| } catch (err:Error) { | } catch (err:Error) { | ||||
| this.error(err); | this.error(err); | ||||
| } | } | ||||
| } | } | ||||
| private function setStatusOnClients( | |||||
| status:String, | |||||
| code:String = null):void { | |||||
| this.status = status; | |||||
| this.statusCode = code; | |||||
| for (var client:String in this.clients) { | |||||
| this.send.send(client, 'setStatus', status, code); | |||||
| } | |||||
| } | |||||
| } | } | ||||
| } | } | ||||