commit 28e7c729804014eb2a240e635b5b9bcaa49b7be0 Author: github-classroom[bot] <66690702+github-classroom[bot]@users.noreply.github.com> Date: Wed Nov 8 07:08:46 2023 +0000 Initialize codecInterface diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..73f69e0 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..397a5c3 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..c5a6386 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..797acea --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Rpc20.zip b/Rpc20.zip new file mode 100644 index 0000000..441e17e Binary files /dev/null and b/Rpc20.zip differ diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..4930fd9 --- /dev/null +++ b/pom.xml @@ -0,0 +1,33 @@ + + + 4.0.0 + + ch.hevs.synd.sdi + Rpc20 + 1.0-SNAPSHOT + + + 15 + 15 + + + + + + com.fasterxml.jackson.core + jackson-core + 2.13.0 + + + + + com.fasterxml.jackson.core + jackson-databind + 2.13.0 + + + + + \ No newline at end of file diff --git a/src/main/java/client/Client.java b/src/main/java/client/Client.java new file mode 100644 index 0000000..7297fe3 --- /dev/null +++ b/src/main/java/client/Client.java @@ -0,0 +1,10 @@ +package client; + +public class Client { + private static final ClientStub client = new ClientStub(); + + public static void main(String[] args) { + System.out.println("15 + 4 = " + client.add(15, 4)); + System.out.println("15 - 4 = " + client.sub(15, 4)); + } +} diff --git a/src/main/java/client/ClientStub.java b/src/main/java/client/ClientStub.java new file mode 100644 index 0000000..0e03efa --- /dev/null +++ b/src/main/java/client/ClientStub.java @@ -0,0 +1,55 @@ +package client; + +import common.*; + +import java.io.IOException; +import java.net.Socket; + +public class ClientStub implements IRPCProcedures { + private final static String SERVER_ADDRESS = "localhost"; + private final static int RPC_PORT = 1234; + + @Override + public double add(double a, double b) { + return do_rpc(new Request(ProcedureID.addID, a, b)); + } + + @Override + public double sub(double a, double b) { + return do_rpc(new Request(ProcedureID.subID, a, b)); + } + + private double do_rpc(Request request) { + Socket socket = null; + Codec codec = null; + try { + socket = new Socket(SERVER_ADDRESS, RPC_PORT); + } catch (IOException e) { + e.printStackTrace(); + System.out.println("Server not available"); + System.exit(2); + } + try { + //codec = new JsonCodec(socket); + codec = new BinaryCodec(socket); + } catch (IOException e) { + e.printStackTrace(); + System.out.println("Streams broken"); + System.exit(2); + } + + codec.sendRequest(request); + + double response = codec.fetchResponse(); + + try { + socket.close(); + } catch (IOException e) { + e.printStackTrace(); + } + + return response; + } +} + + diff --git a/src/main/java/common/BinaryCodec.java b/src/main/java/common/BinaryCodec.java new file mode 100644 index 0000000..47ab25c --- /dev/null +++ b/src/main/java/common/BinaryCodec.java @@ -0,0 +1,105 @@ +package common; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.Socket; + +public class BinaryCodec implements Codec { + + private InputStream _in; + private OutputStream _out; + + public BinaryCodec(InputStream in, OutputStream out) { + _in = in; + _out = out; + } + + public BinaryCodec(Socket socket) throws IOException { + this(socket.getInputStream(), socket.getOutputStream()); + + } + + private void writeLong(long value) throws IOException { + byte[] data = new byte[8]; + for (int i = 0; i < 8; ++i) { + data[i] = (byte)((value >> (8 * i)) & 0xFF); + } + _out.write(data,0, 8); + } + + public long readLong() throws IOException { + byte[] data = new byte[8]; + if (_in.read(data, 0 , 8) != 8) { + throw new IOException("Error reading long from input stream!"); + } + long value = 0; + for (int i = 0; i < 8; ++i) { + value |= ((long)data[i] & 0xFF) << (8 * i); + } + return value; + } + + private void writeDouble(double value) throws IOException { + writeLong(Double.doubleToLongBits(value)); + } + + private double readDouble() throws IOException { + return Double.longBitsToDouble(readLong()); + } + + @Override + public void sendRequest(Request request) { + try { + writeLong(request.getProcedure().ordinal()); + for (int i = 0; i < request.paramsSize(); i++) { + + } + writeDouble(request.getParam(0)); + writeDouble(request.getParam(1)); + } catch (IOException e) { + e.printStackTrace(); + System.out.println("Can not send request"); + System.exit(1); + } + } + + @Override + public void sendResponse(double response) { + try { + writeDouble(response); + } catch (IOException e) { + e.printStackTrace(); + System.out.println("Can not send params"); + System.exit(1); + } + } + + @Override + public Request fetchRequest() { + Request request = null; + try { + ProcedureID procedureID = ProcedureID.values()[(int)readLong()]; + request = new Request(procedureID, readDouble(), readDouble()); + + } catch (IOException e) { + e.printStackTrace(); + System.out.println("Can not fetch request"); + System.exit(1); + } + return request; + } + + @Override + public double fetchResponse() { + double response = 0.0; + try { + response = readDouble(); + } catch (IOException e) { + e.printStackTrace(); + System.out.println("Can not fetch request"); + System.exit(1); + } + return response; + } +} diff --git a/src/main/java/common/Codec.java b/src/main/java/common/Codec.java new file mode 100644 index 0000000..58fc783 --- /dev/null +++ b/src/main/java/common/Codec.java @@ -0,0 +1,11 @@ +package common; + +import java.util.Vector; + +public interface Codec { + public void sendRequest(Request request); + public void sendResponse(double response); + public Request fetchRequest(); + public double fetchResponse(); +} + diff --git a/src/main/java/common/IRPCProcedures.java b/src/main/java/common/IRPCProcedures.java new file mode 100644 index 0000000..3efd978 --- /dev/null +++ b/src/main/java/common/IRPCProcedures.java @@ -0,0 +1,6 @@ +package common; + +public interface IRPCProcedures { + double add(double a, double b); + double sub(double a, double b); +} diff --git a/src/main/java/common/JsonCodec.java b/src/main/java/common/JsonCodec.java new file mode 100644 index 0000000..85ff3c5 --- /dev/null +++ b/src/main/java/common/JsonCodec.java @@ -0,0 +1,87 @@ +package common; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; + +import java.io.*; +import java.lang.reflect.Array; +import java.net.Socket; +import java.util.Arrays; + +public class JsonCodec implements Codec { + + private InputStream _in; + private OutputStream _out; + private ObjectMapper _mapper; + + public JsonCodec(Socket socket) throws IOException { + this(socket.getInputStream(), socket.getOutputStream()); + } + + public JsonCodec(InputStream in, OutputStream out) { + _in = in; + _out = out; + _mapper = new ObjectMapper(); + _mapper.enable(SerializationFeature.INDENT_OUTPUT); + } + + @Override + public void sendRequest(Request request) { + try { + _out.write(_mapper.writeValueAsBytes(request)); + } catch (IOException e) { + e.printStackTrace(); + System.out.println("Error on request send"); + System.exit(1); + } + } + + @Override + public void sendResponse(double response) { + double[] responseArray = new double[1]; + responseArray[0] = response; + try { + _out.write(_mapper.writeValueAsBytes(responseArray)); + } catch (IOException e) { + e.printStackTrace(); + System.out.println("Error on params send"); + System.exit(1); + } + } + + @Override + public Request fetchRequest() { + Request request = null; + try { + byte[] rxbytes = new byte[1024]; + int length = _in.read(rxbytes); + System.out.println("The request: " + getString(rxbytes, length)); + request = _mapper.readValue(rxbytes, Request.class); + } catch (IOException e) { + e.printStackTrace(); + System.out.println("Error on request receive"); + System.exit(1); + } + return request; + } + + @Override + public double fetchResponse() { + try { + double[] responseArray = _mapper.readValue(_in, double[].class); + return responseArray[0]; + } catch (IOException e) { + e.printStackTrace(); + System.out.println("Error on params receive"); + System.exit(1); + } + return 0.0; + } + + public static String getString(byte[] b, int length) { + byte[] bShort = Arrays.copyOf(b, length); + + return new String(bShort); + } +} diff --git a/src/main/java/common/ProcedureID.java b/src/main/java/common/ProcedureID.java new file mode 100644 index 0000000..4126b14 --- /dev/null +++ b/src/main/java/common/ProcedureID.java @@ -0,0 +1,6 @@ +package common; + +public enum ProcedureID { + addID, + subID +} \ No newline at end of file diff --git a/src/main/java/common/Request.java b/src/main/java/common/Request.java new file mode 100644 index 0000000..42316d7 --- /dev/null +++ b/src/main/java/common/Request.java @@ -0,0 +1,47 @@ +package common; + +import java.util.Vector; + +public class Request { + private ProcedureID procedure; + private Vector params; + + public Request() { + + } + + public Request(ProcedureID procedureID) { + procedure = procedureID; + params = new Vector<>(); + } + + public Request(ProcedureID procedureID, double a, double b) { + this(procedureID); + params.add(a); + params.add(b); + } + + public ProcedureID getProcedure() { return procedure; } + + public void setProcedureID(ProcedureID procedureID) { procedure = procedureID; } + + public Vector getParams() { + return params; + } + + public void setParams(Vector params) { + this.params = params; + } + + public void setParam(int i, double x) { + params.set(i, x); + } + + public double getParam(int i) { + return params.get(i); + } + + public int paramsSize() { + return params.size(); + } +} diff --git a/src/main/java/server/Server.java b/src/main/java/server/Server.java new file mode 100644 index 0000000..0ff22d3 --- /dev/null +++ b/src/main/java/server/Server.java @@ -0,0 +1,26 @@ +package server; + +import common.IRPCProcedures; + +import java.io.IOException; + +public class Server implements IRPCProcedures { + @Override + public double add(double a, double b) { + return a + b; + } + + @Override + public double sub(double a, double b) { + return a - b; + } + + public static void main(String[] args) { + Server implementation = new Server(); + try { + new ServerStub(implementation).run(); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/server/ServerStub.java b/src/main/java/server/ServerStub.java new file mode 100644 index 0000000..b5ba28c --- /dev/null +++ b/src/main/java/server/ServerStub.java @@ -0,0 +1,61 @@ +package server; + +import common.*; + +import java.io.IOException; +import java.net.ServerSocket; +import java.net.Socket; + +public class ServerStub { + + private final static int RPC_PORT = 1234; + + private final IRPCProcedures implementation; + private ServerSocket serverSocket; + + public ServerStub(IRPCProcedures implementation) throws IOException { + this.implementation = implementation; + serverSocket = new ServerSocket(RPC_PORT); + } + + public void run() throws IOException { + + while (true) { + Codec codec = null; + Socket socket = serverSocket.accept(); + try { + //codec = new JsonCodec(socket); + codec = new BinaryCodec(socket); + } catch (IOException e) { + e.printStackTrace(); + System.out.println("Codec can not get streams"); + System.exit(1); + } + + Request request = codec.fetchRequest(); + + ProcedureID procedureID = request.getProcedure(); + + double result = 0.0; + + switch (procedureID) { + case addID: + result = implementation.add(request.getParam(0), request.getParam(1)); + break; + + case subID: + result = implementation.sub(request.getParam(0), request.getParam(1)); + break; + + default: + System.out.println("Default"); + break; + } + + codec.sendResponse(result); + + socket.close(); + System.out.println("Client processing finished"); + } + } +} diff --git a/target/classes/client/Client.class b/target/classes/client/Client.class new file mode 100644 index 0000000..11eb0ad Binary files /dev/null and b/target/classes/client/Client.class differ diff --git a/target/classes/client/ClientStub.class b/target/classes/client/ClientStub.class new file mode 100644 index 0000000..6e4f04c Binary files /dev/null and b/target/classes/client/ClientStub.class differ diff --git a/target/classes/common/BinaryCodec.class b/target/classes/common/BinaryCodec.class new file mode 100644 index 0000000..db0a23d Binary files /dev/null and b/target/classes/common/BinaryCodec.class differ diff --git a/target/classes/common/Codec.class b/target/classes/common/Codec.class new file mode 100644 index 0000000..5bd338f Binary files /dev/null and b/target/classes/common/Codec.class differ diff --git a/target/classes/common/IRPCProcedures.class b/target/classes/common/IRPCProcedures.class new file mode 100644 index 0000000..5f3ead3 Binary files /dev/null and b/target/classes/common/IRPCProcedures.class differ diff --git a/target/classes/common/JsonCodec.class b/target/classes/common/JsonCodec.class new file mode 100644 index 0000000..3b0da4f Binary files /dev/null and b/target/classes/common/JsonCodec.class differ diff --git a/target/classes/common/ProcedureID.class b/target/classes/common/ProcedureID.class new file mode 100644 index 0000000..f478e2e Binary files /dev/null and b/target/classes/common/ProcedureID.class differ diff --git a/target/classes/common/Request.class b/target/classes/common/Request.class new file mode 100644 index 0000000..e12dad9 Binary files /dev/null and b/target/classes/common/Request.class differ diff --git a/target/classes/server/Server.class b/target/classes/server/Server.class new file mode 100644 index 0000000..d804545 Binary files /dev/null and b/target/classes/server/Server.class differ diff --git a/target/classes/server/ServerStub$1.class b/target/classes/server/ServerStub$1.class new file mode 100644 index 0000000..00ef4cd Binary files /dev/null and b/target/classes/server/ServerStub$1.class differ diff --git a/target/classes/server/ServerStub.class b/target/classes/server/ServerStub.class new file mode 100644 index 0000000..c4f73a4 Binary files /dev/null and b/target/classes/server/ServerStub.class differ