Permalink
Browse files

Adding Factory for server instances

  • Loading branch information...
MattTuttle committed Feb 20, 2014
1 parent 1cb6b34 commit 86d9a5b6427b98b26d66c5f8cf64bde357b25852
View
@@ -0,0 +1,19 @@
package hxnet.base;
import hxnet.interfaces.IFactory;
import hxnet.interfaces.IProtocol;
class Factory implements IFactory
{
public function new(protocol:Class<IProtocol>)
{
this.protocol = protocol;
}
public function buildProtocol():IProtocol
{
return Type.createInstance(protocol, []);
}
private var protocol:Class<IProtocol>;
}
@@ -1,9 +1,9 @@
package hxnet.protocols;
package hxnet.base;
import hxnet.interfaces.IConnection;
import haxe.io.Input;
class BaseProtocol implements hxnet.interfaces.IProtocol
class Protocol implements hxnet.interfaces.IProtocol
{
public function new() { }
@@ -0,0 +1,6 @@
package hxnet.interfaces;
interface IFactory
{
public function buildProtocol():IProtocol;
}
View
@@ -10,7 +10,7 @@ import neko.Lib;
import cpp.Lib;
#end
class RPC extends BaseProtocol
class RPC extends hxnet.base.Protocol
{
public var dispatcher:Dynamic;
@@ -4,7 +4,7 @@ import haxe.io.Input;
import haxe.io.Bytes;
import haxe.io.BytesOutput;
class Telnet extends BaseProtocol
class Telnet extends hxnet.base.Protocol
{
public override function dataReceived(input:Input)
{
@@ -13,7 +13,7 @@ import neko.Lib;
import cpp.Lib;
#end
class WebSocket extends BaseProtocol
class WebSocket extends hxnet.base.Protocol
{
private static var WEBSOCKET_VERSION = 13;
View
@@ -4,6 +4,7 @@ import sys.net.Host;
import sys.net.Socket;
import haxe.io.Bytes;
import haxe.io.BytesInput;
import hxnet.interfaces.IFactory;
import hxnet.interfaces.IProtocol;
class Server implements hxnet.interfaces.IServer
@@ -12,12 +13,11 @@ class Server implements hxnet.interfaces.IServer
public var host(default, null):String;
public var port(default, null):Int;
public function new(protocol:Class<IProtocol>, port:Int, ?hostname:String)
public function new(factory:IFactory, port:Int, ?hostname:String)
{
protocolClass = protocol;
bytes = Bytes.alloc(1024);
this.factory = factory;
this.host = (hostname == null ? Host.localhost() : hostname);
this.port = port;
@@ -40,7 +40,7 @@ class Server implements hxnet.interfaces.IServer
client.setBlocking(false);
readSockets.push(client);
var cnx = Type.createInstance(protocolClass, []);
var cnx = factory.buildProtocol();
var connection = new Connection(client);
client.custom = cnx;
cnx.makeConnection(connection);
@@ -83,7 +83,7 @@ class Server implements hxnet.interfaces.IServer
listener.close();
}
private var protocolClass:Class<IProtocol>;
private var factory:IFactory;
private var readSockets:Array<Socket>;
private var listener:Socket;
View
@@ -4,6 +4,7 @@ import sys.net.UdpSocket;
import sys.net.Address;
import sys.net.Host;
import hxnet.interfaces.IProtocol;
import hxnet.interfaces.IFactory;
import haxe.io.Bytes;
import haxe.io.BytesInput;
import haxe.Timer;
@@ -16,11 +17,11 @@ typedef ClientConnection = {
class Server implements hxnet.interfaces.IServer
{
public function new(protocol:Class<IProtocol>, port:Int, ?hostname:String)
public function new(factory:IFactory, port:Int, ?hostname:String)
{
connections = new Map<Address, ClientConnection>();
buffer = Bytes.alloc(1024);
protocolClass = protocol;
this.factory = factory;
var host = new Host(hostname == null ? Host.localhost() : hostname);
@@ -64,7 +65,7 @@ class Server implements hxnet.interfaces.IServer
else
{
// new connection
var protocol = Type.createInstance(protocolClass, []);
var protocol = factory.buildProtocol();
var client = new UdpSocket();
protocol.makeConnection(new Connection(client, address));
cnx = { protocol: protocol, timeout: 10 };
@@ -88,7 +89,7 @@ class Server implements hxnet.interfaces.IServer
private var listener:UdpSocket;
private var address:Address;
private var buffer:Bytes;
private var protocolClass:Class<IProtocol>;
private var factory:IFactory;
private var connections:Map<Address, ClientConnection>;
}
View
@@ -11,7 +11,7 @@ class TcpTest extends haxe.unit.TestCase
public function createRPCServer()
{
var port = Thread.readMessage(true);
var server = new hxnet.tcp.Server(PingPong, port);
var server = new hxnet.tcp.Server(new hxnet.base.Factory(PingPong), port);
while (Thread.readMessage(false) == null)
{
server.update();
View
@@ -11,7 +11,7 @@ class UdpTest extends haxe.unit.TestCase
public function createRPCServer()
{
var port = Thread.readMessage(true);
var server = new hxnet.udp.Server(PingPong, port);
var server = new hxnet.udp.Server(new hxnet.base.Factory(PingPong), port);
while (Thread.readMessage(false) != "finish")
{
server.update();

0 comments on commit 86d9a5b

Please sign in to comment.