From e058788ad09fab1effa721840b5f014b89cce7f2 Mon Sep 17 00:00:00 2001 From: jenz Date: Tue, 21 Feb 2023 21:35:05 +0100 Subject: [PATCH] reduced amount of strings and removed overuse of threads --- .../src/main/java/DataLayer/DataMapper.java | 2 +- .../src/main/java/DataLayer/ThreadClient.java | 43 ++++++++++++++----- .../main/java/FunctionLayer/Datahandler.java | 7 ++- .../FunctionLayer/PipelineJMWESingleton.java | 2 +- .../PresentationLayer/DiscordHandler.java | 12 +----- 5 files changed, 38 insertions(+), 28 deletions(-) diff --git a/ArtificialAutism/src/main/java/DataLayer/DataMapper.java b/ArtificialAutism/src/main/java/DataLayer/DataMapper.java index fcd7717a..6453ceec 100644 --- a/ArtificialAutism/src/main/java/DataLayer/DataMapper.java +++ b/ArtificialAutism/src/main/java/DataLayer/DataMapper.java @@ -93,7 +93,7 @@ public class DataMapper { ResultSet resultSet = l_pStatement.executeQuery(); if (resultSet.next()) { int count = resultSet.getInt(1); - if (count > 13000) { + if (count > 8000) { //System.out.println("cleaning strings: " + l_sSQL); l_pStatement = l_cCon.prepareStatement(l_sSQL); l_pStatement.executeUpdate(); diff --git a/ArtificialAutism/src/main/java/DataLayer/ThreadClient.java b/ArtificialAutism/src/main/java/DataLayer/ThreadClient.java index a5888ffb..7233d304 100644 --- a/ArtificialAutism/src/main/java/DataLayer/ThreadClient.java +++ b/ArtificialAutism/src/main/java/DataLayer/ThreadClient.java @@ -8,10 +8,17 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.*; +import java.util.ArrayList; import java.util.Properties; public class ThreadClient { - public ThreadClient(int port, Datahandler datahandler, StanfordCoreNLP stanfordCoreNLP, StanfordCoreNLP stanfordCoreNLPSentiment) { + public ThreadClient(Datahandler datahandler, StanfordCoreNLP stanfordCoreNLP, StanfordCoreNLP stanfordCoreNLPSentiment) { + ArrayList ports = new ArrayList(); + ports.add(48475); + ports.add(48476); + ports.add(48477); + ports.add(48478); + Properties prop = new Properties(); String fileName = "app.config"; try (FileInputStream fis = new FileInputStream(fileName)) { @@ -21,33 +28,46 @@ public class ThreadClient { } String hostIP = prop.getProperty("app.hostip"); - if (port == Integer.valueOf(prop.getProperty("app.hostport")) - || port == Integer.valueOf(prop.getProperty("app.hostport2"))) { - hostIP = prop.getProperty("app.hostip2"); - } + String hostIP2 = prop.getProperty("app.hostip2"); try { InetAddress ipAddress = InetAddress.getByName(hostIP);//used ip's - try (DatagramSocket serverSocket = new DatagramSocket(port)) { - while (true) { - receiveAndSendPacket(serverSocket, ipAddress, port, datahandler, stanfordCoreNLP, stanfordCoreNLPSentiment); + InetAddress ipAddress2 = InetAddress.getByName(hostIP2);//used ip's + try (DatagramSocket serverSocket = new DatagramSocket(ports.get(0))) { + try (DatagramSocket serverSocket1 = new DatagramSocket(ports.get(1))) { + try (DatagramSocket serverSocket2 = new DatagramSocket(ports.get(2))) { + try (DatagramSocket serverSocket3 = new DatagramSocket(ports.get(3))) { + while (true) { + try { + receiveAndSendPacket(serverSocket, ipAddress, ports.get(0), datahandler, stanfordCoreNLP, stanfordCoreNLPSentiment); + receiveAndSendPacket(serverSocket1, ipAddress, ports.get(1), datahandler, stanfordCoreNLP, stanfordCoreNLPSentiment); + receiveAndSendPacket(serverSocket2, ipAddress2, ports.get(2), datahandler, stanfordCoreNLP, stanfordCoreNLPSentiment); + receiveAndSendPacket(serverSocket3, ipAddress2, ports.get(3), datahandler, stanfordCoreNLP, stanfordCoreNLPSentiment); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } + } } } catch (SocketException e) { e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); } } catch (UnknownHostException e) { e.printStackTrace(); } } - private static void receiveAndSendPacket(DatagramSocket serverSocket, InetAddress ipAddress, int port, Datahandler datahandler, StanfordCoreNLP stanfordCoreNLP, StanfordCoreNLP stanfordCoreNLPSentiment) throws IOException { byte[] receiveData = new byte[4096]; DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); try { + /* + Only one DatagramSocket can call receive at a time since its a blocking call. yet somehow + the other DatagramSockets still get their UDP packets from receive() even if the call is made + many minutes after the actual UDP packet was sent. Maybe Security manager context? + */ serverSocket.receive(receivePacket); } catch (IOException e) { e.printStackTrace(); @@ -57,6 +77,7 @@ public class ThreadClient { sentence = sentence.replace("clientmessage:", ""); String ResponseMsg = datahandler.getResponseMsg(sentence, "", stanfordCoreNLP, stanfordCoreNLPSentiment, true); + System.out.println("port: " + port + ". ResponseMsg ingame: " + ResponseMsg); byte[] sendData = new byte[0]; try { sendData = ResponseMsg.getBytes("UTF-8"); diff --git a/ArtificialAutism/src/main/java/FunctionLayer/Datahandler.java b/ArtificialAutism/src/main/java/FunctionLayer/Datahandler.java index 0d53dff2..c1364d81 100644 --- a/ArtificialAutism/src/main/java/FunctionLayer/Datahandler.java +++ b/ArtificialAutism/src/main/java/FunctionLayer/Datahandler.java @@ -27,7 +27,7 @@ import java.util.regex.Pattern; public class Datahandler { - private ExecutorService pool = Executors.newFixedThreadPool(4); + private ExecutorService pool = Executors.newFixedThreadPool(6); private CompletionService completionService = new ExecutorCompletionService(pool); private HashMap pipelineAnnotationCache; private HashMap pipelineSentimentAnnotationCache; @@ -139,7 +139,7 @@ public class Datahandler { props.setProperty("parse.model", shiftReduceParserPath); props.setProperty("parse.maxlen", "90"); props.setProperty("parse.binaryTrees", "true"); - props.setProperty("threads", "5"); + props.setProperty("threads", "1"); props.setProperty("pos.maxlen", "90"); props.setProperty("tokenize.maxlen", "90"); props.setProperty("ssplit.maxlen", "90"); @@ -166,7 +166,7 @@ public class Datahandler { propsSentiment.setProperty("parse.model", lexParserEnglishPCFG); propsSentiment.setProperty("sentiment.model", sentimentModel); propsSentiment.setProperty("parse.maxlen", "90"); - propsSentiment.setProperty("threads", "5"); + propsSentiment.setProperty("threads", "1"); propsSentiment.setProperty("pos.maxlen", "90"); propsSentiment.setProperty("tokenize.maxlen", "90"); propsSentiment.setProperty("ssplit.maxlen", "90"); @@ -585,7 +585,6 @@ public class Datahandler { for (String str1 : ues_copy) { if (strF != str1) { - //critical section Future submit = completionService.submit(new get_res(strF, str1, stanfordCoreNLP, stanfordCoreNLPSentiment, coreMaps1, strAnno, strAnnoSentiment, coreDocument, tokenizeCountingF, taggedWordListF diff --git a/ArtificialAutism/src/main/java/FunctionLayer/PipelineJMWESingleton.java b/ArtificialAutism/src/main/java/FunctionLayer/PipelineJMWESingleton.java index 1e24f79a..d338cd9f 100644 --- a/ArtificialAutism/src/main/java/FunctionLayer/PipelineJMWESingleton.java +++ b/ArtificialAutism/src/main/java/FunctionLayer/PipelineJMWESingleton.java @@ -86,7 +86,7 @@ public class PipelineJMWESingleton { propsJMWE = new Properties(); propsJMWE.setProperty("annotators", "tokenize,ssplit,pos,lemma"); propsJMWE.setProperty("tokenize.options", "untokenizable=firstKeep"); - propsJMWE.setProperty("threads", "5"); + propsJMWE.setProperty("threads", "1"); propsJMWE.setProperty("pos.maxlen", "90"); propsJMWE.setProperty("tokenize.maxlen", "90"); propsJMWE.setProperty("ssplit.maxlen", "90"); diff --git a/ArtificialAutism/src/main/java/PresentationLayer/DiscordHandler.java b/ArtificialAutism/src/main/java/PresentationLayer/DiscordHandler.java index 51b838e1..314a2a6b 100644 --- a/ArtificialAutism/src/main/java/PresentationLayer/DiscordHandler.java +++ b/ArtificialAutism/src/main/java/PresentationLayer/DiscordHandler.java @@ -66,17 +66,7 @@ public class DiscordHandler extends ListenerAdapter { } catch (LoginException e) { throw new RuntimeException(e); } - - ArrayList ports = new ArrayList(); - ports.add(48475); - ports.add(48476); - ports.add(48477); - ports.add(48478); - for (Integer port : ports) { - new Thread(() -> { - new ThreadClient(port, datahandler, stanfordCoreNLP, stanfordCoreNLPSentiment); - }).start(); - } + new ThreadClient(datahandler, stanfordCoreNLP, stanfordCoreNLPSentiment); }