Initialize codecInterface

This commit is contained in:
github-classroom[bot] 2023-11-08 07:08:46 +00:00 committed by GitHub
commit 28e7c72980
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
30 changed files with 642 additions and 0 deletions

8
.idea/.gitignore vendored Normal file
View File

@ -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/

13
.idea/compiler.xml Normal file
View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="Rpc20" />
</profile>
</annotationProcessing>
</component>
</project>

20
.idea/jarRepositories.xml Normal file
View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://repo.maven.apache.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
</component>
</project>

14
.idea/misc.xml Normal file
View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_15" default="true" project-jdk-name="openjdk-15" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RunConfigurationProducerService">
<option name="ignoredProducers">
<set>
<option value="com.android.tools.idea.compose.preview.runconfiguration.ComposePreviewRunConfigurationProducer" />
</set>
</option>
</component>
</project>

124
.idea/uiDesigner.xml Normal file
View File

@ -0,0 +1,124 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

BIN
Rpc20.zip Normal file

Binary file not shown.

33
pom.xml Normal file
View File

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ch.hevs.synd.sdi</groupId>
<artifactId>Rpc20</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>15</maven.compiler.source>
<maven.compiler.target>15</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.13.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.0</version>
</dependency>
</dependencies>
</project>

View File

@ -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));
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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();
}

View File

@ -0,0 +1,6 @@
package common;
public interface IRPCProcedures {
double add(double a, double b);
double sub(double a, double b);
}

View File

@ -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);
}
}

View File

@ -0,0 +1,6 @@
package common;
public enum ProcedureID {
addID,
subID
}

View File

@ -0,0 +1,47 @@
package common;
import java.util.Vector;
public class Request {
private ProcedureID procedure;
private Vector<Double> 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<Double> getParams() {
return params;
}
public void setParams(Vector<Double> 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();
}
}

View File

@ -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();
}
}
}

View File

@ -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");
}
}
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.