add multi thread

This commit is contained in:
Rémi Heredero 2023-01-13 10:16:59 +01:00
parent 5e002b3e7e
commit 4c81d931f2
5 changed files with 98 additions and 5 deletions

41
.idea/workspace.xml generated
View File

@ -7,10 +7,12 @@
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="028e861c-982b-48af-abd9-036e0fbebabe" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/src/main/java/ch/hevs/synd/sin/stream/UIClientTCP.java" afterDir="false" />
<list default="true" id="028e861c-982b-48af-abd9-036e0fbebabe" name="Default Changelist" comment="ex1-2">
<change afterPath="$PROJECT_DIR$/src/main/java/ch/hevs/synd/sin/stream/Server.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/ch/hevs/synd/sin/stream/ServerThread.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/ch/hevs/synd/sin/network/server/Transaction.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/ch/hevs/synd/sin/network/server/Transaction.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/ch/hevs/synd/sin/UIConstants.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/ch/hevs/synd/sin/UIConstants.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/ch/hevs/utils/Utility.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/ch/hevs/utils/Utility.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -61,7 +63,7 @@
&quot;settings.editor.selected.configurable&quot;: &quot;discord-project&quot;
}
}</component>
<component name="RunManager" selected="Application.Task1">
<component name="RunManager" selected="Application.Server">
<configuration default="true" type="ArquillianTestNG" factoryName="" nameIsGenerated="true">
<option name="arquillianRunConfiguration">
<value>
@ -75,6 +77,19 @@
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="Server" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="ch.hevs.synd.sin.stream.Server" />
<module name="UIServerStudents" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="ch.hevs.synd.sin.stream.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="Task1" type="Application" factoryName="Application">
<option name="MAIN_CLASS_NAME" value="ch.hevs.synd.sin.stream.UIClientTCP" />
<module name="UIServerStudents" />
@ -92,7 +107,13 @@
<list>
<item itemvalue="Application.Task2" />
<item itemvalue="Application.Task1" />
<item itemvalue="Application.Server" />
</list>
<recent_temporary>
<list>
<item itemvalue="Application.Server" />
</list>
</recent_temporary>
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
@ -107,6 +128,14 @@
<workItem from="1639476893616" duration="41000" />
<workItem from="1639648924592" duration="247000" />
</task>
<task id="LOCAL-00001" summary="ex1-2">
<created>1673337087163</created>
<option name="number" value="00001" />
<option name="presentableId" value="LOCAL-00001" />
<option name="project" value="LOCAL" />
<updated>1673337087163</updated>
</task>
<option name="localTasksCounter" value="2" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
@ -123,4 +152,8 @@
</map>
</option>
</component>
<component name="VcsManagerConfiguration">
<MESSAGE value="ex1-2" />
<option name="LAST_COMMIT_MESSAGE" value="ex1-2" />
</component>
</project>

View File

@ -7,7 +7,7 @@ package ch.hevs.synd.sin;
*/
public class UIConstants {
/** Defining the default port number to be used. */
public static final int UI_SERVER_PORT = 1502;
public static final int UI_SERVER_PORT = 1515; //1502;
public static final int UI_BUFFER_MAX_SIZE = 256;
/** Messages for DatagramSocket examples */

View File

@ -0,0 +1,26 @@
package ch.hevs.synd.sin.stream;
import ch.hevs.synd.sin.UICommands;
import ch.hevs.synd.sin.UIConstants;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
try (ServerSocket serverSocket = new ServerSocket(UIConstants.UI_SERVER_PORT)){
System.out.println("Server is listening on port " + UIConstants.UI_SERVER_PORT);
while (true) {
Socket socket = serverSocket.accept();
System.out.println("New client connected");
new ServerThread(socket).start();
}
} catch (IOException ex) {
System.out.println("Server exception: " + ex.getMessage());
ex.printStackTrace();
}
}
}

View File

@ -0,0 +1,31 @@
package ch.hevs.synd.sin.stream;
import ch.hevs.synd.sin.network.server.Transaction;
import ch.hevs.synd.sin.sensor.MeasurementType;
import ch.hevs.synd.sin.sensor.Sensor;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class ServerThread extends Thread {
private Socket socket;
static private Sensor uS = new Sensor(MeasurementType.Voltage,0);
static private Sensor iS = new Sensor(MeasurementType.Current, 90);
public ServerThread(Socket socket){
this.socket = socket;
}
public void run(){
try {
InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();
Transaction t = new Transaction(is, os, uS, iS);
if(!t.processTransaction()) socket.close();
} catch (IOException ex) {
System.out.println("Server exception: " + ex.getMessage());
ex.printStackTrace();
}
}
}

View File

@ -10,6 +10,9 @@ import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Random;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
/**
* This class contains some useful Java methods to manipulate data.