Esempio n. 1
0
 public static OtpNode createOtpNode(final String cookie, final boolean longName)
     throws IOException {
   OtpNode node;
   final String hostName = HostnameUtils.getErlangHostName(longName);
   if (Strings.isNullOrEmpty(cookie)) {
     node = new OtpNode(createJavaNodeName(hostName));
   } else {
     node = new OtpNode(createJavaNodeName(hostName), cookie);
   }
   debugPrintCookie(node.cookie());
   return node;
 }
 private boolean canConnect(final String hostName) {
   if (hostName == null) {
     return false;
   }
   try {
     final OtpNode node = new OtpNode("jtest", "erlide");
     ErlLogger.debug("Ping: " + nodeName + "@" + hostName);
     final boolean result = node.ping(nodeName + "@" + hostName, 1000);
     node.close();
     return result;
   } catch (final IOException e) {
     ErlLogger.error(e);
   }
   return false;
 }
Esempio n. 3
0
 private boolean connectRetry() {
   int tries = MAX_RETRIES;
   boolean ok = false;
   while (!ok && tries > 0) {
     ErlLogger.debug("# ping..." + getNodeName() + " " + Thread.currentThread().getName());
     ok = localNode.ping(getNodeName(), RETRY_DELAY + (MAX_RETRIES - tries) * RETRY_DELAY % 3);
     tries--;
   }
   return ok;
 }
Esempio n. 4
0
 public void startLocalNode() {
   boolean nodeCreated = false;
   synchronized (localNodeLock) {
     int i = 0;
     do {
       try {
         i++;
         localNode = ErlRuntime.createOtpNode(data.getCookie(), data.hasLongName());
         localNode.registerStatusHandler(statusWatcher);
         nodeCreated = true;
       } catch (final IOException e) {
         ErlLogger.error("ErlRuntime could not be created (%s), retrying %d", e.getMessage(), i);
         try {
           localNodeLock.wait(300);
         } catch (final InterruptedException e1) {
         }
       }
     } while (!nodeCreated && i < 10);
   }
 }
  public static void main(String argv[]) {

    // String cookie = argv[0];
    String erlNode = argv[1];
    OtpErlangObject expected = null;
    boolean waiting = false;

    try { //
      OtpNode node = new OtpNode("javanode");
      OtpMbox mainMbox = node.createMbox();

      try {
        // Initiate and set up connection to erlang process
        OtpMbox mbox = node.createMbox();
        OtpMbox mbox2;

        OtpErlangObject[] msg = {mainMbox.self(), mbox.self()};
        mbox.send("erl_link_server", erlNode, new OtpErlangTuple(msg));
        OtpErlangObject o = mbox.receive(1000);
        if (o == null) {
          System.exit(1);
          return;
        }
        OtpErlangTuple tuple = (OtpErlangTuple) o;
        int tag = (int) ((OtpErlangLong) tuple.elementAt(0)).longValue();

        switch (tag) {
          case java_exit_with_reason_any_term:
          case java_link_and_exit:
            dbg("Java got \"java_link_and_exit\" or " + "\"java_exit_with_reason_any_term\"");
            mbox.link((OtpErlangPid) tuple.elementAt(1));
            mbox.send((OtpErlangPid) tuple.elementAt(1), new OtpErlangAtom("ok"));
            mbox.exit(tuple.elementAt(2));
            break;
          case erl_exit_with_reason_any_term:
          case erl_link_and_exit:
            dbg("Java got \"erl_link_and_exit\" or " + "\"erl_exit_with_reason_any_term\"");
            mbox.send((OtpErlangPid) tuple.elementAt(1), new OtpErlangAtom("ok"));
            waiting = true;
            expected = tuple.elementAt(2);
            mbox.receive(1000);
            System.exit(2);
            break;
          case erl_link_java_exit:
            dbg("Java got \"erl_link_java_exit\"");
            mbox.exit(tuple.elementAt(2));
            break;
          case java_link_erl_exit:
            dbg("Java got \"java_link_erl_exit\"");
            mbox.link((OtpErlangPid) tuple.elementAt(1));
            mbox.send((OtpErlangPid) tuple.elementAt(1), new OtpErlangAtom("ok"));
            waiting = true;
            expected = tuple.elementAt(2);
            mbox.receive(1000);
            System.exit(3);
            break;
          case internal_link_linking_exits:
            dbg("Java got \"internal_link_linking_exits\"");
            mbox2 = node.createMbox();
            mbox.link(mbox2.self());
            mbox.exit(tuple.elementAt(2));
            waiting = true;
            expected = tuple.elementAt(2);
            mbox2.receive(1000); // hanging waiting for exit
            System.exit(4); // got someting other than exit
            break;
          case internal_link_linked_exits:
            dbg("Java got \"internal_link_linked_exits\"");
            mbox2 = node.createMbox();
            mbox.link(mbox2.self());
            mbox2.exit(tuple.elementAt(2));
            waiting = true;
            expected = tuple.elementAt(2);
            mbox.receive(1000); // hanging waiting for exit
            System.exit(5); // got someting other than exit
            break;
          case internal_unlink_linking_exits:
            dbg("Java got \"internal_unlink_linking_exits\"");
            mbox2 = node.createMbox();
            mbox.link(mbox2.self());
            mbox.unlink(mbox2.self());
            mbox.link(mbox2.self());
            mbox2.unlink(mbox.self());
            mbox2.exit(tuple.elementAt(2));
            if (mbox.receive(500) != null) System.exit(6);
            break;
          case internal_unlink_linked_exits:
            dbg("Java got \"internal_unlink_linked_exits\"");
            mbox2 = node.createMbox();
            mbox.link(mbox2.self());
            mbox.unlink(mbox2.self());
            mbox.link(mbox2.self());
            mbox2.unlink(mbox.self());
            mbox.exit(tuple.elementAt(2));
            if (mbox2.receive(500) != null) System.exit(7);
            break;
          case normal_exit:
            dbg("Java got \"normal_exit\"");
            mbox.close();
            break;
          case kill_mbox:
            dbg("Java got \"kill_mbox\"");
            mbox.exit("kill");
            break;
          case kill_erl_proc_from_java:
            dbg("Java got \"kill_erl_proc_from_java\"");
            mbox.exit((OtpErlangPid) tuple.elementAt(1), "kill");
            break;
          case kill_mbox_from_erlang:
            dbg("Java got \"kill_mbox_from_erlang\"");
            /* This will make the testcase successful, but it is
                  not the correct way to do it...
                  Mbox should probably just die when the kill signal is
                  received from erlang (or other mbox).

               try {
            mbox.receive(1000);
            System.exit(8);
               }
               catch (OtpErlangExit exit) {
            if(!(exit.reason().equals(new OtpErlangAtom("kill"))))
                System.exit(9);
            mbox.exit("killed");
               }
               */
            break;
        }
      } catch (OtpErlangExit exit) {
        dbg("Java got exit: " + exit.reason());
        if (!(waiting && exit.reason().equals(expected))) System.exit(10);
      }

      OtpErlangAtom done = new OtpErlangAtom("done");
      mainMbox.send("erl_link_server", erlNode, done);
      OtpErlangObject o = mainMbox.receive(1000);
      if (o == null) System.exit(11);
      else if (!((OtpErlangAtom) o).equals(done)) System.exit(12);

    } catch (Exception e) {
      System.out.println("EXCEPTION: " + e);
      System.exit(13);
    }
  }
Esempio n. 6
0
 @Override
 public void stop() {
   // close peer too?
   stopped = true;
   localNode.close();
 }
Esempio n. 7
0
 @Override
 public OtpMbox createMbox() {
   return localNode.createMbox();
 }
Esempio n. 8
0
 @Override
 public OtpMbox createMbox(final String name) {
   return localNode.createMbox(name);
 }