refactoring the flow, still missing some caching for sentimentAnalyzerTest
This commit is contained in:
@@ -15,21 +15,18 @@ screen -X -S (number1) quit
|
||||
package PresentationLayer;
|
||||
|
||||
import DataLayer.settings;
|
||||
import FunctionLayer.CustomError;
|
||||
import FunctionLayer.Datahandler;
|
||||
import FunctionLayer.PipelineJMWESingleton;
|
||||
import com.sun.tools.javac.util.List;
|
||||
import discord4j.core.DiscordClient;
|
||||
import discord4j.core.GatewayDiscordClient;
|
||||
import discord4j.core.event.domain.message.MessageCreateEvent;
|
||||
import edu.stanford.nlp.pipeline.StanfordCoreNLP;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.*;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
|
||||
/**
|
||||
@@ -37,7 +34,8 @@ import java.util.logging.Logger;
|
||||
*/
|
||||
public class DiscordHandler {
|
||||
|
||||
private static void receiveAndSendPacket(DatagramSocket serverSocket, InetAddress ipAddress, int port) throws IOException, CustomError {
|
||||
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 {
|
||||
@@ -48,8 +46,9 @@ public class DiscordHandler {
|
||||
String sentence = new String(receivePacket.getData(), 0,
|
||||
receivePacket.getLength());
|
||||
sentence = sentence.replace("clientmessage:", "");
|
||||
String getResponseMsg = Datahandler.instance.getResponseMsg(sentence);
|
||||
byte[] sendData = getResponseMsg.getBytes("UTF-8");
|
||||
String ResponseMsg = datahandler.getResponseMsg(sentence, "", stanfordCoreNLP, stanfordCoreNLPSentiment,
|
||||
true);
|
||||
byte[] sendData = ResponseMsg.getBytes("UTF-8");
|
||||
int deliver_port = 0;
|
||||
switch (port) {
|
||||
case 48470:
|
||||
@@ -67,10 +66,10 @@ public class DiscordHandler {
|
||||
}
|
||||
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, ipAddress, deliver_port);
|
||||
serverSocket.send(sendPacket);
|
||||
//System.out.println("receiveAndSendPacket send message to port: " + deliver_port);
|
||||
}
|
||||
|
||||
public static void handleUDPTraffic(int port) {
|
||||
public static void handleUDPTraffic(int port, Datahandler datahandler,
|
||||
StanfordCoreNLP stanfordCoreNLP, StanfordCoreNLP stanfordCoreNLPSentiment) {
|
||||
try (DatagramSocket serverSocket = new DatagramSocket(port)) {
|
||||
String hostIP = "";
|
||||
if (port == 48473 || port == 48471) {
|
||||
@@ -80,35 +79,27 @@ public class DiscordHandler {
|
||||
}
|
||||
InetAddress ipAddress = InetAddress.getByName(hostIP);//used ip'
|
||||
while (true) {
|
||||
receiveAndSendPacket(serverSocket, ipAddress, port);
|
||||
receiveAndSendPacket(serverSocket, ipAddress, port, datahandler, stanfordCoreNLP, stanfordCoreNLPSentiment);
|
||||
}
|
||||
} catch (SocketException | UnknownHostException e) {
|
||||
e.printStackTrace();
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
} catch (CustomError customError) {
|
||||
customError.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
Datahandler.instance.initiateMYSQL();
|
||||
//nohup screen -d -m -S nonroot java -Xmx6900M -jar /home/javatests/ArtificialAutism-1.0.jar
|
||||
//uncomment db fetch when ready, just keep the comment for future reference
|
||||
System.out.println("finished initiating MYSQL");
|
||||
} catch (SQLException | IOException ex) {
|
||||
Logger.getLogger(DiscordHandler.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
public static void main(String[] args) throws IOException, SQLException {
|
||||
Datahandler datahandler = new Datahandler();
|
||||
datahandler.initiateMYSQL();
|
||||
|
||||
PipelineJMWESingleton.getINSTANCE();
|
||||
Datahandler.instance.instantiateAnnotationMapJMWE();
|
||||
Datahandler.instance.shiftReduceParserInitiate();
|
||||
Datahandler.instance.instantiateAnnotationMap();
|
||||
StanfordCoreNLP stanfordCoreNLP = datahandler.pipeLineSetUp();
|
||||
StanfordCoreNLP stanfordCoreNLPSentiment = datahandler.shiftReduceParserInitiate();
|
||||
System.out.println("FINISHED ALL ANNOTATIONS");
|
||||
Datahandler.instance.updateStringCache();
|
||||
//String token = "NTI5NzAxNTk5NjAyMjc4NDAx.Dw0vDg.7-aMjVWdQMYPl8qVNyvTCPS5F_A";
|
||||
datahandler.updateStringCache();
|
||||
System.out.println("updatedstring cache");
|
||||
String token = new settings().getDiscordToken();
|
||||
final DiscordClient client = DiscordClient.create(token);
|
||||
final GatewayDiscordClient gateway = client.login().block();
|
||||
@@ -119,13 +110,11 @@ public class DiscordHandler {
|
||||
final int j = i;
|
||||
new Thread(() -> {
|
||||
List<Integer> ports = List.of(48470, 48471, 48472, 48473);
|
||||
handleUDPTraffic(ports.get(j));
|
||||
handleUDPTraffic(ports.get(j), datahandler, stanfordCoreNLP, stanfordCoreNLPSentiment);
|
||||
}).start();
|
||||
}
|
||||
gateway.on(MessageCreateEvent.class).subscribe(event -> {
|
||||
if (!FunctionLayer.DoStuff.isOccupied()) {
|
||||
FunctionLayer.DoStuff.doStuff(event, usernameBot);
|
||||
}
|
||||
FunctionLayer.DoStuff.doStuff(event, usernameBot, datahandler, stanfordCoreNLP, stanfordCoreNLPSentiment);
|
||||
});
|
||||
gateway.onDisconnect().block();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user