Initial commit

This commit is contained in:
Flare Microsystems
2024-11-08 09:01:36 -08:00
commit 71d118f160
43 changed files with 1100 additions and 0 deletions

View File

@@ -0,0 +1,81 @@
package com.flaremicro.crossjeeves.net;
import java.io.File;
import java.io.IOException;
import com.flaremicro.crossjeeves.net.packet.Packet;
import com.flaremicro.crossjeeves.net.packet.Packet0Identify;
import com.flaremicro.crossjeeves.net.packet.Packet127KeepAlive;
import com.flaremicro.crossjeeves.net.packet.Packet1Status;
import com.flaremicro.crossjeeves.net.packet.Packet2Script;
import com.flaremicro.crossjeeves.net.packet.Packet3Clone;
import com.flaremicro.crossjeeves.net.packet.Packet4FileData;
import com.flaremicro.crossjeeves.net.packet.Packet5Artifact;
import com.flaremicro.util.ZipUtils;
public class ClientHandler extends NetworkHandler {
@Override
public void handlePacket(Packet packet) {
// TODO Auto-generated method stub
}
@Override
public void handlePacket(Packet0Identify packet) {
// TODO Auto-generated method stub
}
@Override
public void handlePacket(Packet1Status packet) {
// TODO Auto-generated method stub
}
@Override
public void handlePacket(Packet2Script packet) {
// TODO Auto-generated method stub
}
@Override
public void handlePacket(Packet3Clone packet) {
long fileId = packet.getFileID();
String workspace = System.getenv("WORKSPACE");
if(workspace == null || workspace.trim().length() <= 0)
{
//disconnect
}
try
{
File workspaceZip = File.createTempFile("workspace-"+fileId, ".zip");
ZipUtils.zipDirectory(new File(workspace), workspaceZip);
}
catch (IOException e)
{
//disconnect
e.printStackTrace();
}
}
@Override
public void handlePacket(Packet4FileData packet) {
// TODO Auto-generated method stub
}
@Override
public void handlePacket(Packet5Artifact packet) {
// TODO Auto-generated method stub
}
@Override
public void handlePacket(Packet127KeepAlive packet) {
// TODO Auto-generated method stub
}
}

View File

@@ -0,0 +1,24 @@
package com.flaremicro.crossjeeves.net;
import com.flaremicro.crossjeeves.net.packet.Packet;
import com.flaremicro.crossjeeves.net.packet.Packet0Identify;
import com.flaremicro.crossjeeves.net.packet.Packet127KeepAlive;
import com.flaremicro.crossjeeves.net.packet.Packet1Status;
import com.flaremicro.crossjeeves.net.packet.Packet2Script;
import com.flaremicro.crossjeeves.net.packet.Packet3Clone;
import com.flaremicro.crossjeeves.net.packet.Packet4FileData;
import com.flaremicro.crossjeeves.net.packet.Packet5Artifact;
public abstract class NetworkHandler {
public abstract void handlePacket(Packet packet);
public abstract void handlePacket(Packet0Identify packet);
public abstract void handlePacket(Packet1Status packet);
public abstract void handlePacket(Packet2Script packet);
public abstract void handlePacket(Packet3Clone packet);
public abstract void handlePacket(Packet4FileData packet);
public abstract void handlePacket(Packet5Artifact packet);
public abstract void handlePacket(Packet127KeepAlive packet);
}

View File

@@ -0,0 +1,59 @@
package com.flaremicro.crossjeeves.net;
import com.flaremicro.crossjeeves.net.packet.Packet;
import com.flaremicro.crossjeeves.net.packet.Packet0Identify;
import com.flaremicro.crossjeeves.net.packet.Packet127KeepAlive;
import com.flaremicro.crossjeeves.net.packet.Packet1Status;
import com.flaremicro.crossjeeves.net.packet.Packet2Script;
import com.flaremicro.crossjeeves.net.packet.Packet3Clone;
import com.flaremicro.crossjeeves.net.packet.Packet4FileData;
import com.flaremicro.crossjeeves.net.packet.Packet5Artifact;
public class ServerHandler extends NetworkHandler {
@Override
public void handlePacket(Packet packet) {
// TODO Auto-generated method stub
}
@Override
public void handlePacket(Packet0Identify packet) {
// TODO Auto-generated method stub
}
@Override
public void handlePacket(Packet1Status packet) {
}
@Override
public void handlePacket(Packet2Script packet) {
// TODO Auto-generated method stub
}
@Override
public void handlePacket(Packet3Clone packet) {
}
@Override
public void handlePacket(Packet4FileData packet) {
// TODO Auto-generated method stub
}
@Override
public void handlePacket(Packet5Artifact packet) {
// TODO Auto-generated method stub
}
@Override
public void handlePacket(Packet127KeepAlive packet) {
// TODO Auto-generated method stub
}
}

View File

@@ -0,0 +1,65 @@
package com.flaremicro.crossjeeves.net.packet;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import com.flaremicro.crossjeeves.net.NetworkHandler;
public abstract class Packet {
public static final int PROTOCOL_VERSION = 0;
public static Map<Class<?>, Byte> packetToOp = new HashMap<Class<?>, Byte>();
public static Map<Byte, Packet> opToPacket = new HashMap<Byte, Packet>();
static
{
registerPacket((byte) 0, new Packet0Identify());
registerPacket((byte) 1, new Packet1Status());
registerPacket((byte) 2, new Packet2Script());
registerPacket((byte) 3, new Packet3Clone());
registerPacket((byte) 4, new Packet4FileData());
registerPacket((byte) 5, new Packet5Artifact());
registerPacket((byte) 127, new Packet127KeepAlive());
}
public abstract Packet cloneTypeOnly();
public static void registerPacket(byte opcode, Packet packet)
{
opToPacket.put(Byte.valueOf(opcode), packet);
packetToOp.put(packet.getClass(), Byte.valueOf(opcode));
}
public final byte getId()
{
return ((Byte)packetToOp.get(this.getClass())).byteValue();
}
public Packet(){
}
public void recievePacket(DataInputStream in) throws IOException {
}
public void processPacket(NetworkHandler networkHandler){
networkHandler.handlePacket(this);
}
public void sendPacket(DataOutputStream out) throws IOException {
out.writeByte(getId());
}
public static byte[] toBytes(String data, int length) {
byte[] result = new byte[length];
System.arraycopy(data.getBytes(), 0, result, 0, data.length());
return result;
}
}

View File

@@ -0,0 +1,42 @@
package com.flaremicro.crossjeeves.net.packet;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import com.flaremicro.crossjeeves.net.NetworkHandler;
public class Packet0Identify extends Packet{
public int protocolVersion;
int flags;
public Packet0Identify(){
}
public Packet0Identify(int flags){
this.protocolVersion = PROTOCOL_VERSION;
this.flags = flags;
}
public void recievePacket(DataInputStream in) throws IOException {
this.protocolVersion = in.readInt();
flags = in.readInt();
}
public void sendPacket(DataOutputStream out) throws IOException {
super.sendPacket(out);
out.writeInt(protocolVersion);
out.writeInt(flags);
}
public void processPacket(NetworkHandler networkHandler){
networkHandler.handlePacket(this);
}
@Override
public Packet cloneTypeOnly() {
return new Packet0Identify();
}
}

View File

@@ -0,0 +1,44 @@
package com.flaremicro.crossjeeves.net.packet;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import com.flaremicro.crossjeeves.net.NetworkHandler;
public class Packet127KeepAlive extends Packet{
long fileId;
short chunkSize;
byte[] fileChunk;
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 {
fileId = in.readLong();
}
public void sendPacket(DataOutputStream out) throws IOException {
super.sendPacket(out);
out.writeLong(fileId);
out.writeShort(chunkSize);
out.write(fileChunk);
}
public void processPacket(NetworkHandler networkHandler){
networkHandler.handlePacket(this);
}
@Override
public Packet cloneTypeOnly() {
return new Packet127KeepAlive();
}
}

View File

@@ -0,0 +1,41 @@
package com.flaremicro.crossjeeves.net.packet;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import com.flaremicro.crossjeeves.net.NetworkHandler;
public class Packet1Status extends Packet{
public int protocolVersion;
int flags;
public static final int BUSY = 0x00000001;
public Packet1Status(){
}
public Packet1Status(int flags){
this.flags = flags;
}
public void recievePacket(DataInputStream in) throws IOException {
flags = in.readInt();
}
public void sendPacket(DataOutputStream out) throws IOException {
super.sendPacket(out);
out.writeInt(flags);
}
public void processPacket(NetworkHandler networkHandler){
networkHandler.handlePacket(this);
}
@Override
public Packet cloneTypeOnly() {
return new Packet1Status();
}
}

View File

@@ -0,0 +1,38 @@
package com.flaremicro.crossjeeves.net.packet;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import com.flaremicro.crossjeeves.net.NetworkHandler;
public class Packet2Script extends Packet{
String script;
public Packet2Script(){
}
public Packet2Script(String script){
this.script = script;
}
public void recievePacket(DataInputStream in) throws IOException {
script = in.readUTF();
}
public void sendPacket(DataOutputStream out) throws IOException {
super.sendPacket(out);
out.writeUTF(script);
}
public void processPacket(NetworkHandler networkHandler){
networkHandler.handlePacket(this);
}
@Override
public Packet cloneTypeOnly() {
return new Packet2Script();
}
}

View File

@@ -0,0 +1,51 @@
package com.flaremicro.crossjeeves.net.packet;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import com.flaremicro.crossjeeves.net.NetworkHandler;
public class Packet3Clone extends Packet{
long fileId;
long fileSize;
public Packet3Clone(){
}
//TODO more parameters
public Packet3Clone(long fileId, long fileSize){
this.fileId = fileId;
this.fileSize = fileSize;
}
public void recievePacket(DataInputStream in) throws IOException {
fileId = in.readLong();
fileSize = in.readLong();
}
public void sendPacket(DataOutputStream out) throws IOException {
super.sendPacket(out);
out.writeLong(fileId);
out.writeLong(fileSize);
}
public void processPacket(NetworkHandler networkHandler){
networkHandler.handlePacket(this);
}
@Override
public Packet cloneTypeOnly() {
return new Packet3Clone();
}
public long getFileID() {
return fileId;
}
public long getFileSize() {
return fileSize;
}
}

View File

@@ -0,0 +1,45 @@
package com.flaremicro.crossjeeves.net.packet;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import com.flaremicro.crossjeeves.net.NetworkHandler;
public class Packet4FileData extends Packet{
long fileId;
byte[] fileChunk;
public Packet4FileData(){
}
public Packet4FileData(long fileId, byte[] fileChunk){
this.fileId = fileId;
this.fileChunk = fileChunk;
}
public void recievePacket(DataInputStream in) throws IOException {
fileId = in.readLong();
int chunkSize = in.readShort();
fileChunk = new byte[chunkSize];
in.readFully(fileChunk);
}
public void sendPacket(DataOutputStream out) throws IOException {
super.sendPacket(out);
out.writeLong(fileId);
out.writeShort(fileChunk.length);
out.write(fileChunk);
}
public void processPacket(NetworkHandler networkHandler){
networkHandler.handlePacket(this);
}
@Override
public Packet cloneTypeOnly() {
return new Packet4FileData();
}
}

View File

@@ -0,0 +1,41 @@
package com.flaremicro.crossjeeves.net.packet;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import com.flaremicro.crossjeeves.net.NetworkHandler;
public class Packet5Artifact extends Packet{
long fileId;
String relativeFile;
public Packet5Artifact(){
}
public Packet5Artifact(long fileId, String relativeFile){
this.fileId = fileId;
}
public void recievePacket(DataInputStream in) throws IOException {
fileId = in.readLong();
relativeFile = in.readUTF();
}
public void sendPacket(DataOutputStream out) throws IOException {
super.sendPacket(out);
out.writeLong(fileId);
out.writeUTF(relativeFile);
}
public void processPacket(NetworkHandler networkHandler){
networkHandler.handlePacket(this);
}
@Override
public Packet cloneTypeOnly() {
return new Packet5Artifact();
}
}