Add preliminary net code
This commit is contained in:
@@ -2,6 +2,7 @@ package com.flaremicro.crossjeeves.net;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
import com.flaremicro.crossjeeves.net.packet.Packet;
|
import com.flaremicro.crossjeeves.net.packet.Packet;
|
||||||
import com.flaremicro.crossjeeves.net.packet.Packet0Identify;
|
import com.flaremicro.crossjeeves.net.packet.Packet0Identify;
|
||||||
@@ -15,6 +16,10 @@ import com.flaremicro.util.ZipUtils;
|
|||||||
|
|
||||||
public class ClientHandler extends NetworkHandler {
|
public class ClientHandler extends NetworkHandler {
|
||||||
|
|
||||||
|
public ClientHandler(Socket socket) throws IOException {
|
||||||
|
super(socket);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handlePacket(Packet packet) {
|
public void handlePacket(Packet packet) {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
|
|||||||
@@ -1,5 +1,12 @@
|
|||||||
package com.flaremicro.crossjeeves.net;
|
package com.flaremicro.crossjeeves.net;
|
||||||
|
|
||||||
|
import java.io.DataInputStream;
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.Socket;
|
||||||
|
import java.util.concurrent.BlockingQueue;
|
||||||
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
|
|
||||||
import com.flaremicro.crossjeeves.net.packet.Packet;
|
import com.flaremicro.crossjeeves.net.packet.Packet;
|
||||||
import com.flaremicro.crossjeeves.net.packet.Packet0Identify;
|
import com.flaremicro.crossjeeves.net.packet.Packet0Identify;
|
||||||
import com.flaremicro.crossjeeves.net.packet.Packet127KeepAlive;
|
import com.flaremicro.crossjeeves.net.packet.Packet127KeepAlive;
|
||||||
@@ -8,17 +15,112 @@ import com.flaremicro.crossjeeves.net.packet.Packet2Script;
|
|||||||
import com.flaremicro.crossjeeves.net.packet.Packet3Clone;
|
import com.flaremicro.crossjeeves.net.packet.Packet3Clone;
|
||||||
import com.flaremicro.crossjeeves.net.packet.Packet4FileData;
|
import com.flaremicro.crossjeeves.net.packet.Packet4FileData;
|
||||||
import com.flaremicro.crossjeeves.net.packet.Packet5Artifact;
|
import com.flaremicro.crossjeeves.net.packet.Packet5Artifact;
|
||||||
|
import com.flaremicro.util.Util;
|
||||||
|
|
||||||
public abstract class NetworkHandler {
|
public abstract class NetworkHandler {
|
||||||
|
private boolean connected;
|
||||||
|
private Socket socket;
|
||||||
|
private DataInputStream in;
|
||||||
|
private DataOutputStream out;
|
||||||
|
private Thread readThread;
|
||||||
|
|
||||||
|
private BlockingQueue<Packet> outbox = new LinkedBlockingQueue<Packet>();
|
||||||
|
|
||||||
|
public NetworkHandler(Socket socket) throws IOException {
|
||||||
|
connected = true;
|
||||||
|
this.in = new DataInputStream(socket.getInputStream());
|
||||||
|
this.out = new DataOutputStream(socket.getOutputStream());
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isConnected() {
|
||||||
|
return connected;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void enqueue(Packet packet) {
|
||||||
|
outbox.add(packet);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void process(Packet packet) {
|
||||||
|
packet.processPacket(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO error code?
|
||||||
|
protected void disconnect() {
|
||||||
|
connected = false;
|
||||||
|
readThread.interrupt();
|
||||||
|
Util.cleanClose(in);
|
||||||
|
Util.cleanClose(out);
|
||||||
|
Util.cleanClose(socket);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void beginReading() {
|
||||||
|
while (connected)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
int opcode = in.readByte();
|
||||||
|
Packet packet = Packet.opToPacket.get(opcode);
|
||||||
|
if (packet != null)
|
||||||
|
{
|
||||||
|
packet = packet.cloneTypeOnly();
|
||||||
|
packet.recievePacket(in);
|
||||||
|
packet.processPacket(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (IOException e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
disconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void beginWriteThread() {
|
||||||
|
readThread = new Thread(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
while (connected)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Packet packet = outbox.take();
|
||||||
|
if (packet != null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
packet.sendPacket(out);
|
||||||
|
}
|
||||||
|
catch (IOException e)
|
||||||
|
{
|
||||||
|
disconnect();
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (InterruptedException e)
|
||||||
|
{
|
||||||
|
disconnect();
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
readThread.start();
|
||||||
|
}
|
||||||
|
|
||||||
public abstract void handlePacket(Packet packet);
|
public abstract void handlePacket(Packet packet);
|
||||||
|
|
||||||
public abstract void handlePacket(Packet0Identify packet);
|
public abstract void handlePacket(Packet0Identify packet);
|
||||||
|
|
||||||
public abstract void handlePacket(Packet1Status packet);
|
public abstract void handlePacket(Packet1Status packet);
|
||||||
|
|
||||||
public abstract void handlePacket(Packet2Script packet);
|
public abstract void handlePacket(Packet2Script packet);
|
||||||
|
|
||||||
public abstract void handlePacket(Packet3Clone packet);
|
public abstract void handlePacket(Packet3Clone packet);
|
||||||
|
|
||||||
public abstract void handlePacket(Packet4FileData packet);
|
public abstract void handlePacket(Packet4FileData packet);
|
||||||
|
|
||||||
public abstract void handlePacket(Packet5Artifact packet);
|
public abstract void handlePacket(Packet5Artifact packet);
|
||||||
|
|
||||||
public abstract void handlePacket(Packet127KeepAlive packet);
|
public abstract void handlePacket(Packet127KeepAlive packet);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
package com.flaremicro.crossjeeves.net;
|
package com.flaremicro.crossjeeves.net;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
import com.flaremicro.crossjeeves.net.packet.Packet;
|
import com.flaremicro.crossjeeves.net.packet.Packet;
|
||||||
import com.flaremicro.crossjeeves.net.packet.Packet0Identify;
|
import com.flaremicro.crossjeeves.net.packet.Packet0Identify;
|
||||||
import com.flaremicro.crossjeeves.net.packet.Packet127KeepAlive;
|
import com.flaremicro.crossjeeves.net.packet.Packet127KeepAlive;
|
||||||
@@ -11,6 +14,10 @@ import com.flaremicro.crossjeeves.net.packet.Packet4FileData;
|
|||||||
|
|
||||||
public class ServerHandler extends NetworkHandler {
|
public class ServerHandler extends NetworkHandler {
|
||||||
|
|
||||||
|
public ServerHandler(Socket socket) throws IOException {
|
||||||
|
super(socket);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handlePacket(Packet packet) {
|
public void handlePacket(Packet packet) {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
|
|||||||
@@ -7,28 +7,15 @@ import java.io.IOException;
|
|||||||
import com.flaremicro.crossjeeves.net.NetworkHandler;
|
import com.flaremicro.crossjeeves.net.NetworkHandler;
|
||||||
|
|
||||||
public class Packet127KeepAlive extends Packet{
|
public class Packet127KeepAlive extends Packet{
|
||||||
long fileId;
|
|
||||||
short chunkSize;
|
|
||||||
byte[] fileChunk;
|
|
||||||
|
|
||||||
public Packet127KeepAlive(){
|
public Packet127KeepAlive(){
|
||||||
}
|
}
|
||||||
|
|
||||||
public Packet127KeepAlive(long fileId, byte[] fileChunk){
|
|
||||||
this.fileId = fileId;
|
|
||||||
this.chunkSize = (short)fileChunk.length;
|
|
||||||
this.fileChunk = fileChunk;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void recievePacket(DataInputStream in) throws IOException {
|
public void recievePacket(DataInputStream in) throws IOException {
|
||||||
fileId = in.readLong();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendPacket(DataOutputStream out) throws IOException {
|
public void sendPacket(DataOutputStream out) throws IOException {
|
||||||
super.sendPacket(out);
|
super.sendPacket(out);
|
||||||
out.writeLong(fileId);
|
|
||||||
out.writeShort(chunkSize);
|
|
||||||
out.write(fileChunk);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void processPacket(NetworkHandler networkHandler){
|
public void processPacket(NetworkHandler networkHandler){
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
package com.flaremicro.util;
|
package com.flaremicro.util;
|
||||||
|
|
||||||
|
import java.io.Closeable;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.Socket;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@@ -32,4 +35,32 @@ public class Util {
|
|||||||
}
|
}
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void cleanClose(Closeable closeable) {
|
||||||
|
if(closeable != null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
closeable.close();
|
||||||
|
}
|
||||||
|
catch (IOException e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void cleanClose(Socket socket) {
|
||||||
|
if(socket != null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
socket.close();
|
||||||
|
}
|
||||||
|
catch (IOException e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ public abstract class PacketTestBase {
|
|||||||
buffer.empty();
|
buffer.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void assertBufferEmpty() {
|
public static void assertBufferEmpty() {
|
||||||
assertEquals(0, buffer.size());
|
assertEquals(0, buffer.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user