-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from youngmonkeys/plugin
add plugin
- Loading branch information
Showing
22 changed files
with
510 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.