Skip to content

Commit

Permalink
Merge pull request #8 from youngmonkeys/plugin
Browse files Browse the repository at this point in the history
add plugin
  • Loading branch information
tvd12 authored Jul 17, 2021
2 parents 1522d65 + 3e3c417 commit a49a99b
Show file tree
Hide file tree
Showing 22 changed files with 510 additions and 11 deletions.
6 changes: 5 additions & 1 deletion EzyClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ public interface EzyClient

EzyApp getAppById(int appId);

EzyPingManager getPingManager();
EzyPlugin getPlugin();

EzyPlugin getPluginById(int pluginId);

EzyPingManager getPingManager();

EzyPingSchedule getPingSchedule();

Expand Down
24 changes: 23 additions & 1 deletion EzyTcpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
31 changes: 31 additions & 0 deletions entity/EzyPlugin.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
112 changes: 112 additions & 0 deletions entity/EzySimplePlugin.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
14 changes: 13 additions & 1 deletion entity/EzySimpleZone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ 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)
{
this.id = id;
this.name = name;
this.client = client;
this.appManager = new EzySimpleAppManager(name);
this.pluginManager = new EzySimplePluginManager(name);
}

public int getId()
Expand Down Expand Up @@ -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(")
Expand Down
4 changes: 4 additions & 0 deletions entity/EzyZone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ public interface EzyZone

EzyAppManager getAppManager();

EzyPluginManager getPluginManager();

EzyApp getApp();

EzyPlugin getPlugin();
}

}
17 changes: 17 additions & 0 deletions handler/EzyAbstractPluginDataHandler.cs
Original file line number Diff line number Diff line change
@@ -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<D> :
EzyLoggable,
EzyPluginDataHandler where D : EzyData
{
public void handle(EzyPlugin plugin, EzyData data)
{
process(plugin, (D)data);
}

protected abstract void process(EzyPlugin plugin, D data);
}
}
3 changes: 1 addition & 2 deletions handler/EzyAppDataHandler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using com.tvd12.ezyfoxserver.client.entity;
using com.tvd12.ezyfoxserver.client.entity;

namespace com.tvd12.ezyfoxserver.client.handler
{
Expand Down
13 changes: 13 additions & 0 deletions handler/EzyPluginDataHandler.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
29 changes: 29 additions & 0 deletions handler/EzyPluginDataHandlers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;

namespace com.tvd12.ezyfoxserver.client.handler
{
public class EzyPluginDataHandlers
{
private readonly IDictionary<Object, EzyPluginDataHandler> handlers;

public EzyPluginDataHandlers()
{
this.handlers = new Dictionary<Object, EzyPluginDataHandler>();
}

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;
}

}
}
32 changes: 32 additions & 0 deletions handler/EzyPluginInfoHandler.cs
Original file line number Diff line number Diff line change
@@ -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<int>(0);
String pluginName = data.get<String>(1);
EzySimplePlugin plugin = new EzySimplePlugin(zone, pluginId, pluginName);
return plugin;
}
}
}
28 changes: 28 additions & 0 deletions handler/EzyPluginResponseHandler.cs
Original file line number Diff line number Diff line change
@@ -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<int>(0);
EzyArray commandData = data.get<EzyArray>(1);
String cmd = commandData.get<String>(0);
EzyData responseData = commandData.get<EzyData>(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);
}
}
}
2 changes: 2 additions & 0 deletions manager/EzyHandlerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ public interface EzyHandlerManager
void addEventHandler(Object eventType, EzyEventHandler eventHandler);

EzyAppDataHandlers getAppDataHandlers(String appName);

EzyPluginDataHandlers getPluginDataHandlers(String pluginName);
}
}
13 changes: 13 additions & 0 deletions manager/EzyPluginByIdGroup.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading

0 comments on commit a49a99b

Please sign in to comment.