0

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:

https://help.anylogic.com/index.jsp?topic=%2Fcom.anylogic.help%2Fhtml%2Fjavadoc%2Fcom%2Fanylogic%2Fengine%2FEngine.html

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

Razil
  • 5
  • 4
  • Can you also share the code that you use at server side? – Florian Jun 21 '19 at 08:56
  • And where exactly is your problem? Can you receive your sent String? Does getEngine().pause() work at all in the AnyLogic model? – Florian Jun 21 '19 at 11:40
  • Hi Florian, sorry for the delayed answer. I added the code for the server in the post above. The socket is working fine. I can send strings from the server to the client and the other way around. My problem is that as soon as I type "start" or "pause", which are supposed to be the trigger words for the engine to start or to stop Anylogic does not perform it. I'm guessing I implemented the engine.run(); wrong – Razil Jun 21 '19 at 23:45

0 Answers0