/* * 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.JUniformObject; import java.lang.reflect.InvocationTargetException; import java.util.logging.Level; import java.util.logging.Logger; import ragefist.core.distribution.DistributedServerPacketHandler; import ragefist.core.network.Connection; /** * * @author acherkashin */ public abstract class IncomingPacket { /** * We don't want to make private constructor here, because we will be * required to override it in all the inherited classes */ public static class IncomingPacketBuilder { public String action = null; public Integer sq = 0; public JUniformObject packet = null; public DistributedServerPacketHandler packetHandler = null; public Connection connection = null; public IncomingPacket build(Class<? extends IncomingPacket> packetClass) throws IncomingPacketException { IncomingPacket object = null; try { object = packetClass.getConstructor().newInstance(); } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException ex) { Logger.getLogger(IncomingPacket.class.getName()).log(Level.SEVERE, null, ex); throw new IncomingPacketException("ERROR_BAD_PACKET"); } if (action == null) { throw new IncomingPacketException("ERROR_BAD_PACKET", "Parameter [action] is null"); } object._action = action; if (sq == null) { throw new IncomingPacketException("ERROR_BAD_PACKET", "Parameter [sq] is null"); } object._sq = sq; if (packet == null) { throw new IncomingPacketException("ERROR_BAD_PACKET", "Parameter [packet] is null"); } object._packet = packet; if (packetHandler == null) { throw new IncomingPacketException("ERROR_BAD_PACKET", "Parameter [packetHandler] is null"); } object._packetHandler = packetHandler; if (connection == null) { throw new IncomingPacketException("ERROR_BAD_PACKET", "Parameter [connection] is null"); } object._connection = connection; return object; } } // ---------------------------------------------------------------------- // // PRIVATE // ---------------------------------------------------------------------- // private String _action = null; private Integer _sq = 0; private JUniformObject _packet = null; private DistributedServerPacketHandler _packetHandler = null; private Connection _connection = null; // ---------------------------------------------------------------------- // // PUBLIC // ---------------------------------------------------------------------- // public String getAction() { return _action; } public Integer getSequence() { return _sq; } public JUniformObject getPacket() { return _packet; } public DistributedServerPacketHandler getPacketHandler() { return _packetHandler; } public Connection getConnection() { return _connection; } public abstract void run() throws IncomingPacketException; }