/* * Created by Andrey Cherkashin (acherkashin) * http://acherkashin.me * * License * Copyright (c) 2015 Andrey Cherkashin * The project released under the MIT license: http://opensource.org/licenses/MIT */ package ragefist.packets; import com.juniform.JUniformMutableObject; import com.juniform.JUniformObject; import java.util.logging.Logger; import ragefist.core.ProcessorTask; import ragefist.core.ProcessorTask.IProcessorTaskDelegate; import ragefist.core.distribution.DistributedServerController.DistributedServerControllerAPI; import ragefist.core.distribution.DistributedServerPacketHandler; import ragefist.core.network.Connection; import ragefist.packets.IncomingPacket.IncomingPacketBuilder; import ragefist.packets.rpc.ExecuteCode; import ragefist.packets.rpc.Reload; /** * * @author acherkashin */ public class PacketHandlerRPC extends DistributedServerPacketHandler implements IProcessorTaskDelegate { private final IncomingPacketBuilder _packetBuilder; public PacketHandlerRPC(DistributedServerControllerAPI serverControllerAPI) { super(serverControllerAPI); _packetBuilder = new IncomingPacketBuilder(); _packetBuilder.packetHandler = this; } public Class<? extends IncomingPacket> getPacketClassForAction(String action) throws IncomingPacketException { switch(action) { default: throw new IncomingPacketException("ERROR_UNKNOWN_ACTION"); case "ExecuteCode": return ExecuteCode.class; case "Reload": return Reload.class; } } @Override public void handleConnection(Connection connection) { } @Override public void handlePacket(JUniformObject packet, Connection connection) { Integer sq = null; try { String action = packet.getProperty("action").toString(); if (action == null) { throw new IncomingPacketException("ERROR_UNKNOWN_ACTION"); } sq = packet.getProperty("sq").toInteger(); _packetBuilder.packet = packet; _packetBuilder.action = action; _packetBuilder.sq = sq; _packetBuilder.connection = connection; // CREATE A PACKET Class<? extends IncomingPacket> packetClass = getPacketClassForAction(action); IncomingPacket packetExecutor = _packetBuilder.build(packetClass); // EXECUTING packetExecutor.run(); } catch (IncomingPacketException ex) { JUniformMutableObject result = JUniformMutableObject.newMap(); result.setProperty("rs", 0); result.setProperty("error", ex.getCode()); if (this.getServerControllerAPI().isTestServer()) { result.setProperty("debug", ex.getMessage()); } if (sq != null) result.setProperty("sq", sq); connection.sendPacket(result); } } @Override public void whenTaskIsFinished(ProcessorTask task) { if (task.getAttachment() == null || (task.getAttachment() instanceof IncomingPacket) == false) { Logger.getLogger(PacketHandlerRPC.class.getName()).warning("Packet is finished without an appropriate IncomingPacket attachment in it. Can't return its result"); return; } IncomingPacket packetExecutor = (IncomingPacket)task.getAttachment(); this.whenIncomingPacketIsFinished(packetExecutor, true, task.getData()); } @Override public void whenIncomingPacketIsFinished(IncomingPacket packet, boolean status, Object responseData) { // SEND RESPONSE JUniformMutableObject resp = JUniformMutableObject.newMap(); resp.setProperty("rs", status ? 1 : 0); resp.setProperty("sq", packet.getSequence()); if (responseData != null) { resp.setProperty("data", responseData); } packet.getConnection().sendPacket(resp); } }