1 module hunt.imf.GatewayApplication;
2 
3 import hunt.net;
4 import hunt.imf.protocol.Protocol;
5 import hunt.imf.ConnectionEventBaseHandler;
6 import hunt.imf.ConnectionManager;
7 import std.typecons;
8 
9 class GatewayApplication
10 {
11    private {
12         NetServer[string] _servers;
13         Protocol[string] _protocols;
14         ConnectionManager!int[string] _mapConnManager;
15         __gshared GatewayApplication _app = null;
16    }
17 
18     private
19     {
20         this () {
21         }
22     }
23 
24     static GatewayApplication instance()
25     {
26         if (_app is null)
27             _app = new GatewayApplication();
28         return _app;
29     }
30 
31     NetServer[string] getServers(){
32         return _servers;
33     }
34 
35 
36     public void addServer(Protocol protocol)
37     {
38         protocol.registerHandler();
39         _protocols[protocol.getName()] = protocol;
40     }
41 
42 
43     void registerConnectionManager(string protocolName)
44     {
45         if (protocolName in _mapConnManager)
46         {
47             return;
48         }else
49         {
50             ConnectionManager!int manager = new ConnectionManager!int();
51             _mapConnManager[protocolName] = manager;
52         }
53     }
54 
55     ConnectionManager!int getConnectionManager(string protocolName)
56     {
57         return _mapConnManager.get(protocolName,null);
58     }
59 
60 
61     void run()
62     {
63         foreach(protocol;_protocols)
64         {
65             NetServer server = NetUtil.createNetServer!(ThreadMode.Single)();
66             server.setCodec(protocol.getCodec());
67             server.setHandler(protocol.getHandler());
68             if (protocol.getOptions() !is null)
69             {
70                 server.setOptions(protocol.getOptions());
71             }
72             server.listen(protocol.getHost() ,protocol.getPort());
73             _servers[protocol.getName()] = server;
74         }
75     }
76 }