At the moment I'm trying to find a way to tell Anylogic programmatically to start or stop the simulation.
I tried to apply the code from the Anylogic Help-Website here:
Unfortunately, it didn't work out for me, because the engine does not respond to my code. In general I'm trying to create a socket, using Anylogic as my client and Eclipse as my server, which is supposed to give Anylogic the commands to either start or stop the simulation.
In general the socket connection is fine - you chat between the programs and the simulation in Anylogic stops automatically as soon as they connect to each other. But if you type "start" nothing happens.
I already got my answer on how to connect Anylogic to Eclipse here: How to fix: Anylogic does not connect to Eclipse over Socket
public class Client implements Serializable {
Engine engine;
public Client(Engine e) {
System.out.println("Engine Name: "+e);
engine=e;
}
public void activate() {
try {
Socket sock = new Socket("127.0.0.1", 3000); // reading from keyboard
BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));// sending to client (pwrite object)
OutputStream ostream = sock.getOutputStream();
PrintWriter pwrite = new PrintWriter(ostream, true);
// receiving from server ( receiveRead object)
InputStream istream = sock.getInputStream();
BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));
System.out.println("Start the chat, type and press Enter key");
String receiveMessage, sendMessage;
while(true)
{
sendMessage = keyRead.readLine(); // keyboard reading
pwrite.println(sendMessage); // sending to server
pwrite.flush(); // flush the data
if((receiveMessage = receiveRead.readLine()) == "start")
//receive from server
{
engine.run();
System.out.println("Start activated");
}
if((receiveMessage = receiveRead.readLine()) == "pause")
//receive from server
{
engine.pause();
System.out.println("Pause activated");
}
if ((receiveMessage = receiveRead.readLine()) != null) //receive from server
{
System.out.println(receiveMessage); // displaying at DOS prompt
}
}
}
catch(IOException ex) {
System.out.println(ex);
}
}
/**
* This number is here for model snapshot storing purpose<br>
* It needs to be changed when this class gets changed
*/
private static final long serialVersionUID = 1L;
}
// Instead of the if clause i have also tried:
if (receiveMessage.contains("start")){
System.out.println("receiveMessage "+ engine);
engine.run();}
// The code for the server
import java.io.*;
import java.net.*;
public class Server3
{
public static void main(String[] args) throws Exception
{
ServerSocket sersock = new ServerSocket(3000);
System.out.println("Server ready for chatting");
Socket sock = sersock.accept( );
// reading from keyboard (keyRead object)
BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
// sending to client (pwrite object)
OutputStream ostream = sock.getOutputStream();
PrintWriter pwrite = new PrintWriter(ostream, true);
// receiving from server ( receiveRead object)
InputStream istream = sock.getInputStream();
BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));
String receiveMessage, sendMessage;
while(true)
{
if((receiveMessage = receiveRead.readLine()) != null)
{
System.out.println(receiveMessage);
}
sendMessage = keyRead.readLine();
pwrite.println(sendMessage);
pwrite.flush();
}
}
}