diff --git a/EzyClient.cs b/EzyClient.cs index 82e36e2..eb9981a 100644 --- a/EzyClient.cs +++ b/EzyClient.cs @@ -64,7 +64,11 @@ public interface EzyClient EzyApp getAppById(int appId); - EzyPingManager getPingManager(); + EzyPlugin getPlugin(); + + EzyPlugin getPluginById(int pluginId); + + EzyPingManager getPingManager(); EzyPingSchedule getPingSchedule(); diff --git a/EzyTcpClient.cs b/EzyTcpClient.cs index 516c7c4..a99ca10 100644 --- a/EzyTcpClient.cs +++ b/EzyTcpClient.cs @@ -246,7 +246,29 @@ public EzyApp getAppById(int appId) return null; } - public EzyPingManager getPingManager() + public EzyPlugin getPlugin() + { + if (zone != null) + { + EzyPluginManager pluginManager = zone.getPluginManager(); + EzyPlugin plugin = pluginManager.getPlugin(); + return plugin; + } + return null; + } + + public EzyPlugin getPluginById(int pluginId) + { + if (zone != null) + { + EzyPluginManager pluginManager = zone.getPluginManager(); + EzyPlugin plugin = pluginManager.getPluginById(pluginId); + return plugin; + } + return null; + } + + public EzyPingManager getPingManager() { return pingManager; } diff --git a/entity/EzyPlugin.cs b/entity/EzyPlugin.cs new file mode 100644 index 0000000..a2b21a7 --- /dev/null +++ b/entity/EzyPlugin.cs @@ -0,0 +1,31 @@ +using System; +using com.tvd12.ezyfoxserver.client.handler; +using com.tvd12.ezyfoxserver.client.request; + +namespace com.tvd12.ezyfoxserver.client.entity +{ + public interface EzyPlugin + { + int getId(); + + String getName(); + + EzyClient getClient(); + + EzyZone getZone(); + + void send(EzyRequest request); + + void send(String cmd); + + void send(String cmd, EzyData data); + + void udpSend(EzyRequest request); + + void udpSend(String cmd); + + void udpSend(String cmd, EzyData data); + + EzyPluginDataHandler getDataHandler(Object cmd); + } +} diff --git a/entity/EzySimplePlugin.cs b/entity/EzySimplePlugin.cs new file mode 100644 index 0000000..374ef8c --- /dev/null +++ b/entity/EzySimplePlugin.cs @@ -0,0 +1,112 @@ +using System; +using System.Text; +using com.tvd12.ezyfoxserver.client.builder; +using com.tvd12.ezyfoxserver.client.constant; +using com.tvd12.ezyfoxserver.client.factory; +using com.tvd12.ezyfoxserver.client.handler; +using com.tvd12.ezyfoxserver.client.request; + +namespace com.tvd12.ezyfoxserver.client.entity +{ + public class EzySimplePlugin : EzyEntity, EzyPlugin + { + protected readonly int id; + protected readonly String name; + protected readonly EzyZone zone; + protected readonly EzyClient client; + protected readonly EzyPluginDataHandlers dataHandlers; + + public EzySimplePlugin(EzyZone zone, int id, String name) + { + this.client = zone.getClient(); + this.zone = zone; + this.id = id; + this.name = name; + this.dataHandlers = client.getHandlerManager().getPluginDataHandlers(name); + } + + public void send(EzyRequest request) + { + String cmd = (String)request.getCommand(); + EzyData data = request.serialize(); + send(cmd, data); + } + + public void send(String cmd) + { + send(cmd, EzyEntityFactory.EMPTY_OBJECT); + } + + public void send(String cmd, EzyData data) + { + EzyArrayBuilder commandData = EzyEntityFactory.newArrayBuilder() + .append(cmd) + .append(data); + EzyArray requestData = EzyEntityFactory.newArrayBuilder() + .append(id) + .append(commandData.build()) + .build(); + client.send(EzyCommand.PLUGIN_REQUEST, requestData); + } + + public void udpSend(EzyRequest request) + { + String cmd = (String)request.getCommand(); + EzyData data = request.serialize(); + udpSend(cmd, data); + } + + public void udpSend(String cmd) + { + udpSend(cmd, EzyEntityFactory.EMPTY_OBJECT); + } + + public void udpSend(String cmd, EzyData data) + { + EzyArrayBuilder commandData = EzyEntityFactory.newArrayBuilder() + .append(cmd) + .append(data); + EzyArray requestData = EzyEntityFactory.newArrayBuilder() + .append(id) + .append(commandData.build()) + .build(); + client.udpSend(EzyCommand.PLUGIN_REQUEST, requestData); + } + + public int getId() + { + return id; + } + + public String getName() + { + return name; + } + + public EzyClient getClient() + { + return client; + } + + public EzyZone getZone() + { + return zone; + } + + public EzyPluginDataHandler getDataHandler(Object cmd) + { + EzyPluginDataHandler handler = dataHandlers.getHandler(cmd); + return handler; + } + + public override string ToString() + { + return new StringBuilder() + .Append("Plugin(") + .Append("id: ").Append(id).Append(", ") + .Append("name: ").Append(name) + .Append(")") + .ToString(); + } + } +} diff --git a/entity/EzySimpleZone.cs b/entity/EzySimpleZone.cs index dd55a14..c39317e 100644 --- a/entity/EzySimpleZone.cs +++ b/entity/EzySimpleZone.cs @@ -10,6 +10,7 @@ public class EzySimpleZone : EzyEntity, EzyZone protected readonly String name; protected readonly EzyClient client; protected readonly EzyAppManager appManager; + protected readonly EzyPluginManager pluginManager; public EzySimpleZone(EzyClient client, int id, String name) { @@ -17,6 +18,7 @@ public EzySimpleZone(EzyClient client, int id, String name) this.name = name; this.client = client; this.appManager = new EzySimpleAppManager(name); + this.pluginManager = new EzySimplePluginManager(name); } public int getId() @@ -44,7 +46,17 @@ public EzyApp getApp() return appManager.getApp(); } - public override string ToString() + public EzyPluginManager getPluginManager() + { + return pluginManager; + } + + public EzyPlugin getPlugin() + { + return pluginManager.getPlugin(); + } + + public override string ToString() { return new StringBuilder() .Append("Zone(") diff --git a/entity/EzyZone.cs b/entity/EzyZone.cs index a3c03b7..c0f7c58 100644 --- a/entity/EzyZone.cs +++ b/entity/EzyZone.cs @@ -13,7 +13,11 @@ public interface EzyZone EzyAppManager getAppManager(); + EzyPluginManager getPluginManager(); + EzyApp getApp(); + + EzyPlugin getPlugin(); } } diff --git a/handler/EzyAbstractPluginDataHandler.cs b/handler/EzyAbstractPluginDataHandler.cs new file mode 100644 index 0000000..9a20403 --- /dev/null +++ b/handler/EzyAbstractPluginDataHandler.cs @@ -0,0 +1,17 @@ +using com.tvd12.ezyfoxserver.client.entity; +using com.tvd12.ezyfoxserver.client.util; + +namespace com.tvd12.ezyfoxserver.client.handler +{ + public abstract class EzyAbstractPluginDataHandler : + EzyLoggable, + EzyPluginDataHandler where D : EzyData + { + public void handle(EzyPlugin plugin, EzyData data) + { + process(plugin, (D)data); + } + + protected abstract void process(EzyPlugin plugin, D data); + } +} diff --git a/handler/EzyAppDataHandler.cs b/handler/EzyAppDataHandler.cs index 29bb097..3fceb9e 100644 --- a/handler/EzyAppDataHandler.cs +++ b/handler/EzyAppDataHandler.cs @@ -1,5 +1,4 @@ -using System; -using com.tvd12.ezyfoxserver.client.entity; +using com.tvd12.ezyfoxserver.client.entity; namespace com.tvd12.ezyfoxserver.client.handler { diff --git a/handler/EzyPluginDataHandler.cs b/handler/EzyPluginDataHandler.cs new file mode 100644 index 0000000..a7fb76a --- /dev/null +++ b/handler/EzyPluginDataHandler.cs @@ -0,0 +1,13 @@ +using com.tvd12.ezyfoxserver.client.entity; + +namespace com.tvd12.ezyfoxserver.client.handler +{ + public interface IEzyPluginDataHandler + { + } + + public interface EzyPluginDataHandler : IEzyPluginDataHandler + { + void handle(EzyPlugin plugin, EzyData data); + } +} diff --git a/handler/EzyPluginDataHandlers.cs b/handler/EzyPluginDataHandlers.cs new file mode 100644 index 0000000..7762e91 --- /dev/null +++ b/handler/EzyPluginDataHandlers.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; + +namespace com.tvd12.ezyfoxserver.client.handler +{ + public class EzyPluginDataHandlers + { + private readonly IDictionary handlers; + + public EzyPluginDataHandlers() + { + this.handlers = new Dictionary(); + } + + public EzyPluginDataHandler getHandler(Object cmd) + { + EzyPluginDataHandler handler = null; + if (handlers.ContainsKey(cmd)) + handler = handlers[cmd]; + return handler; + } + + public void addHandler(Object cmd, EzyPluginDataHandler handler) + { + handlers[cmd] = handler; + } + + } +} \ No newline at end of file diff --git a/handler/EzyPluginInfoHandler.cs b/handler/EzyPluginInfoHandler.cs new file mode 100644 index 0000000..cb95b77 --- /dev/null +++ b/handler/EzyPluginInfoHandler.cs @@ -0,0 +1,32 @@ +using System; +using com.tvd12.ezyfoxserver.client.entity; +using com.tvd12.ezyfoxserver.client.manager; + +namespace com.tvd12.ezyfoxserver.client.handler +{ + public class EzyPluginInfoHandler : EzyAbstractDataHandler + { + + public override void handle(EzyArray data) + { + EzyZone zone = client.getZone(); + EzyPluginManager pluginManager = zone.getPluginManager(); + EzyPlugin plugin = newPlugin(zone, data); + pluginManager.addPlugin(plugin); + postHandle(plugin, data); + logger.info("access plugin: " + plugin.getName() + " successfully"); + } + + protected virtual void postHandle(EzyPlugin plugin, EzyArray data) + { + } + + protected virtual EzyPlugin newPlugin(EzyZone zone, EzyArray data) + { + int pluginId = data.get(0); + String pluginName = data.get(1); + EzySimplePlugin plugin = new EzySimplePlugin(zone, pluginId, pluginName); + return plugin; + } + } +} diff --git a/handler/EzyPluginResponseHandler.cs b/handler/EzyPluginResponseHandler.cs new file mode 100644 index 0000000..58567db --- /dev/null +++ b/handler/EzyPluginResponseHandler.cs @@ -0,0 +1,28 @@ +using System; +using com.tvd12.ezyfoxserver.client.entity; + +namespace com.tvd12.ezyfoxserver.client.handler +{ + public class EzyPluginResponseHandler : EzyAbstractDataHandler + { + public override void handle(EzyArray data) + { + int pluginId = data.get(0); + EzyArray commandData = data.get(1); + String cmd = commandData.get(0); + EzyData responseData = commandData.get(1, null); + + EzyPlugin plugin = client.getPluginById(pluginId); + if (plugin == null) + { + logger.info("receive message when has not joined plugin yet"); + return; + } + EzyPluginDataHandler dataHandler = plugin.getDataHandler(cmd); + if (dataHandler != null) + dataHandler.handle(plugin, responseData); + else + logger.warn("plugin: " + plugin.getName() + " has no handler for command: " + cmd); + } + } +} diff --git a/manager/EzyHandlerManager.cs b/manager/EzyHandlerManager.cs index d8b29d2..1ff8e45 100644 --- a/manager/EzyHandlerManager.cs +++ b/manager/EzyHandlerManager.cs @@ -19,5 +19,7 @@ public interface EzyHandlerManager void addEventHandler(Object eventType, EzyEventHandler eventHandler); EzyAppDataHandlers getAppDataHandlers(String appName); + + EzyPluginDataHandlers getPluginDataHandlers(String pluginName); } } diff --git a/manager/EzyPluginByIdGroup.cs b/manager/EzyPluginByIdGroup.cs new file mode 100644 index 0000000..c9f3c32 --- /dev/null +++ b/manager/EzyPluginByIdGroup.cs @@ -0,0 +1,13 @@ +using com.tvd12.ezyfoxserver.client.entity; + +namespace com.tvd12.ezyfoxserver.client.manager +{ + public interface EzyPluginByIdGroup + { + void addPlugin(EzyPlugin plugin); + + EzyPlugin removePlugin(int pluginId); + + EzyPlugin getPluginById(int pluginId); + } +} diff --git a/manager/EzyPluginGroup.cs b/manager/EzyPluginGroup.cs new file mode 100644 index 0000000..07f5c4a --- /dev/null +++ b/manager/EzyPluginGroup.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using com.tvd12.ezyfoxserver.client.entity; + +namespace com.tvd12.ezyfoxserver.client.manager +{ + public interface EzyPluginGroup : EzyPluginByIdGroup + { + EzyPlugin getPlugin(); + + IList getPluginList(); + + EzyPlugin getPluginByName(String pluginName); + + } + +} diff --git a/manager/EzyPluginManager.cs b/manager/EzyPluginManager.cs new file mode 100644 index 0000000..9ce6eb9 --- /dev/null +++ b/manager/EzyPluginManager.cs @@ -0,0 +1,7 @@ +namespace com.tvd12.ezyfoxserver.client.manager +{ + public interface EzyPluginManager : EzyPluginGroup + { + void clear(); + } +} diff --git a/manager/EzySimpleHandlerManager.cs b/manager/EzySimpleHandlerManager.cs index cf6518b..ce3e5e3 100644 --- a/manager/EzySimpleHandlerManager.cs +++ b/manager/EzySimpleHandlerManager.cs @@ -14,7 +14,8 @@ public class EzySimpleHandlerManager : EzyHandlerManager protected readonly EzyPingSchedule pingSchedule; protected readonly EzyEventHandlers eventHandlers; protected readonly EzyDataHandlers dataHandlers; - protected readonly IDictionary appDataHandlerss; + protected readonly IDictionary appDataHandlersByAppName; + protected readonly IDictionary pluginDataHandlersByPluginName; public EzySimpleHandlerManager(EzyClient client) { @@ -22,7 +23,8 @@ public EzySimpleHandlerManager(EzyClient client) this.pingSchedule = client.getPingSchedule(); this.eventHandlers = newEventHandlers(); this.dataHandlers = newDataHandlers(); - this.appDataHandlerss = new Dictionary(); + this.appDataHandlersByAppName = new Dictionary(); + this.pluginDataHandlersByPluginName = new Dictionary(); } private EzyEventHandlers newEventHandlers() @@ -43,7 +45,9 @@ private EzyDataHandlers newDataHandlers() handlers.addHandler(EzyCommand.APP_ACCESS, new EzyAppAccessHandler()); handlers.addHandler(EzyCommand.APP_REQUEST, new EzyAppResponseHandler()); handlers.addHandler(EzyCommand.APP_EXIT, new EzyAppExitHandler()); - handlers.addHandler(EzyCommand.UDP_HANDSHAKE, new EzyUdpHandshakeHandler()); + handlers.addHandler(EzyCommand.PLUGIN_INFO, new EzyPluginInfoHandler()); + handlers.addHandler(EzyCommand.PLUGIN_REQUEST, new EzyPluginResponseHandler()); + handlers.addHandler(EzyCommand.UDP_HANDSHAKE, new EzyUdpHandshakeHandler()); return handlers; } @@ -69,14 +73,29 @@ public EzyEventHandler getEventHandler(Object eventType) public EzyAppDataHandlers getAppDataHandlers(String appName) { EzyAppDataHandlers answer = null; - if (appDataHandlerss.ContainsKey(appName)) + if (appDataHandlersByAppName.ContainsKey(appName)) { - answer = appDataHandlerss[appName]; + answer = appDataHandlersByAppName[appName]; } if (answer == null) { answer = new EzyAppDataHandlers(); - appDataHandlerss[appName] = answer; + appDataHandlersByAppName[appName] = answer; + } + return answer; + } + + public EzyPluginDataHandlers getPluginDataHandlers(String pluginName) + { + EzyPluginDataHandlers answer = null; + if (pluginDataHandlersByPluginName.ContainsKey(pluginName)) + { + answer = pluginDataHandlersByPluginName[pluginName]; + } + if (answer == null) + { + answer = new EzyPluginDataHandlers(); + pluginDataHandlersByPluginName[pluginName] = answer; } return answer; } diff --git a/manager/EzySimplePluginManager.cs b/manager/EzySimplePluginManager.cs new file mode 100644 index 0000000..2f12a67 --- /dev/null +++ b/manager/EzySimplePluginManager.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using com.tvd12.ezyfoxserver.client.entity; + +namespace com.tvd12.ezyfoxserver.client.manager +{ + public class EzySimplePluginManager : EzyPluginManager + { + protected readonly String zoneName; + protected readonly IList pluginList; + protected readonly IDictionary pluginsById; + protected readonly IDictionary pluginsByName; + + public EzySimplePluginManager(String zoneName) + { + this.zoneName = zoneName; + this.pluginList = new List(); + this.pluginsById = new Dictionary(); + this.pluginsByName = new Dictionary(); + } + + public void addPlugin(EzyPlugin plugin) + { + this.pluginList.Add(plugin); + this.pluginsById[plugin.getId()] = plugin; + this.pluginsByName[plugin.getName()] = plugin; + } + + public EzyPlugin removePlugin(int pluginId) { + EzyPlugin plugin = null; + if(pluginsById.ContainsKey(pluginId)) { + plugin = pluginsById[pluginId]; + pluginsById.Remove(pluginId); + pluginsByName.Remove(plugin.getName()); + pluginList.Remove(plugin); + } + return plugin; + } + + public EzyPlugin getPlugin() + { + if (pluginList.Count == 0) + throw new ArgumentException("has no plugin in zone: " + zoneName); + EzyPlugin plugin = pluginList[0]; + return plugin; + } + + public IList getPluginList() + { + IList list = new List(pluginList); + return list; + } + + public EzyPlugin getPluginById(int pluginId) + { + EzyPlugin plugin = null; + if (pluginsById.ContainsKey(pluginId)) + plugin = pluginsById[pluginId]; + return plugin; + } + + public EzyPlugin getPluginByName(String pluginName) + { + EzyPlugin plugin = null; + if (pluginsByName.ContainsKey(pluginName)) + plugin = pluginsByName[pluginName]; + return plugin; + } + + public void clear() + { + pluginList.Clear(); + pluginsById.Clear(); + pluginsByName.Clear(); + } + } +} diff --git a/setup/EzyPluginSetup.cs b/setup/EzyPluginSetup.cs new file mode 100644 index 0000000..2a6a029 --- /dev/null +++ b/setup/EzyPluginSetup.cs @@ -0,0 +1,13 @@ +using System; +using com.tvd12.ezyfoxserver.client.handler; + +namespace com.tvd12.ezyfoxserver.client.setup +{ + public interface EzyPluginSetup + { + EzyPluginSetup addDataHandler(Object cmd, EzyPluginDataHandler dataHandler); + + EzySetup done(); + + } +} diff --git a/setup/EzySetup.cs b/setup/EzySetup.cs index 7db8d79..0fce857 100644 --- a/setup/EzySetup.cs +++ b/setup/EzySetup.cs @@ -12,6 +12,8 @@ public interface EzySetup EzyAppSetup setupApp(String appName); + EzyPluginSetup setupPlugin(String pluginName); + } } diff --git a/setup/EzySimplePluginSetup.cs b/setup/EzySimplePluginSetup.cs new file mode 100644 index 0000000..160ca68 --- /dev/null +++ b/setup/EzySimplePluginSetup.cs @@ -0,0 +1,28 @@ +using System; +using com.tvd12.ezyfoxserver.client.handler; + +namespace com.tvd12.ezyfoxserver.client.setup +{ + public class EzySimplePluginSetup : EzyPluginSetup + { + protected readonly EzyPluginDataHandlers dataHandlers; + protected readonly EzySetup parent; + + public EzySimplePluginSetup(EzyPluginDataHandlers dataHandlers, EzySetup parent) + { + this.parent = parent; + this.dataHandlers = dataHandlers; + } + + public EzyPluginSetup addDataHandler(Object cmd, EzyPluginDataHandler dataHandler) + { + dataHandlers.addHandler(cmd, dataHandler); + return this; + } + + public EzySetup done() + { + return parent; + } + } +} diff --git a/setup/EzySimpleSetup.cs b/setup/EzySimpleSetup.cs index b471aca..031cc8f 100644 --- a/setup/EzySimpleSetup.cs +++ b/setup/EzySimpleSetup.cs @@ -10,11 +10,13 @@ public class EzySimpleSetup : EzySetup { protected readonly EzyHandlerManager handlerManager; protected readonly IDictionary appSetups; + protected readonly IDictionary pluginSetups; public EzySimpleSetup(EzyHandlerManager handlerManager) { this.handlerManager = handlerManager; this.appSetups = new Dictionary(); + this.pluginSetups = new Dictionary(); } public EzySetup addDataHandler(Object cmd, EzyDataHandler dataHandler) @@ -44,6 +46,22 @@ public EzyAppSetup setupApp(String appName) } return appSetup; } + + public EzyPluginSetup setupPlugin(String pluginName) + { + EzyPluginSetup pluginSetup = null; + if (appSetups.ContainsKey(pluginName)) + { + pluginSetup = pluginSetups[pluginName]; + } + else + { + EzyPluginDataHandlers dataHandlers = handlerManager.getPluginDataHandlers(pluginName); + pluginSetup = new EzySimplePluginSetup(dataHandlers, this); + pluginSetups[pluginName] = pluginSetup; + } + return pluginSetup; + } } }