示例#1
0
  private static void configureCoSimulation(JSONObject jargs) throws IOException {
    CoSimulationMode mode = null;
    List<String> externalProcesses = new Vector<String>();
    String host = null;
    int port = 0;

    String processName = (String) jargs.get(CmlInterpreterArguments.PROCESS_NAME.toString());

    if (jargs.containsKey(CmlInterpreterArguments.COSIM_MODE.key)) {
      mode =
          CoSimulationMode.fromString((String) jargs.get(CmlInterpreterArguments.COSIM_MODE.key));
    } else {
      return;
    }

    if (mode == CoSimulationMode.CoSimCoordinator) {
      if (jargs.containsKey(CmlInterpreterArguments.COSIM_EXTERNAL_PROCESSES.key)) {
        String tmp = (String) jargs.get(CmlInterpreterArguments.COSIM_EXTERNAL_PROCESSES.key);
        externalProcesses.addAll(Arrays.asList(tmp.split(",")));
      } else {
        Console.err.println("Missing required argument for external processes");
      }
    }

    if (jargs.containsKey(CmlInterpreterArguments.COSIM_HOST.key)) {
      String tmp = (String) jargs.get(CmlInterpreterArguments.COSIM_HOST.key);

      String[] decodedTmp = tmp.split("\\:");
      host = decodedTmp[0];
      port = Integer.valueOf(decodedTmp[1]);
    }

    // configure
    Console.out.println("Starting co-simulation with " + host + ":" + port);

    switch (mode) {
      case CoSimCoordinator:
        {
          server = new CoSimulationServer(externalProcesses, port);
          server.listen();
          server.waitForClients();
          factory = new CoSimCoordinatorInterpreterFactory(server);

          break;
        }
      case CoSimClient:
        {
          client = new CoSimulationClient(host, port);
          client.connect();
          client.start();

          client.registerImplementation(processName);
          factory = new CoSimClientInterpreterFactory(client);

          break;
        }
    }
  }
示例#2
0
  private static void shutdownCoSimulation() throws InterruptedException {
    if (server != null) {
      server.close();
    }

    if (client != null) {
      Console.out.println("Waiting for client to recieve disconnect instructions...");
      try {
        client.join();
      } catch (InterruptedException e) {
        // ignore
      }
      try {
        Console.out.println("Client instructed to disconnect so disconnecting now");
        client.disconnect();
      } catch (Exception e) {
        // don't care
      }
    }
  }