Package com.ericsson.otp.erlang

Examples of com.ericsson.otp.erlang.OtpMbox


        stopped = true;
    }

    @Override
    protected void run() throws Exception {
        final OtpMbox eventBox = eventMBox;
        do {
            receiveEventMessage(eventBox);
        } while (!stopped);
    }
View Full Code Here


            throws RpcException {
        final OtpErlangAtom gleader = USER_ATOM;
        try {
            final Object[] args1 = new Object[args.length + 1];
            System.arraycopy(args, 0, args1, 1, args.length);
            final OtpMbox mbox = localNode.createMbox();
            args1[0] = mbox.self();
            new RpcResultReceiver(mbox, cb);
            rpcCast(localNode, nodeName, false, gleader, m, f, signature, args1);
        } catch (final SignatureException e) {
            throw new RpcException(e);
        }
View Full Code Here

    }

    @Override
    public void send(final OtpErlangPid pid, final Object msg) {
        try {
            final OtpMbox mbox = localNode.createMbox();
            try {
                if (mbox != null) {
                    if (CHECK_RPC) {
                        ErlLogger.debug("SEND " + pid + "-> " + msg);
                    }
                    mbox.send(pid, TypeConverter.java2erlang(msg, "x"));
                }
            } finally {
                localNode.closeMbox(mbox);
            }
        } catch (final Exception e) {
View Full Code Here

        }
    }

    private void send(final OtpNode node, final String peer, final String name,
            final Object msg) {
        final OtpMbox mbox = node.createMbox();
        try {
            if (mbox != null) {
                if (CHECK_RPC) {
                    ErlLogger.debug("SEND " + name + "-> " + msg);
                }
                try {
                    mbox.send(name, peer, TypeConverter.java2erlang(msg, "x"));
                } catch (final SignatureException e) {
                    // ignore
                }
            }
        } finally {
View Full Code Here

        }

        final OtpErlangObject[] args = convertArgs(signature, args0);

        OtpErlangObject res = null;
        final OtpMbox mbox = node.createMbox();
        res = buildRpcCall(mbox.self(), gleader, module, fun, args);
        if (logCalls) {
            final Object[] args01 = { module, fun, argString(args) };
            ErlLogger.debug("call -> %s:%s(%s)", args01);
        }
        //
        final OtpErlangRef ref = RpcMonitor.recordRequest(node, peer, module, fun, args,
                OtpErlang.sizeOf(res));
        //
        mbox.send("rex", peer, res);
        if (CHECK_RPC) {
            ErlLogger.debug("RPC " + mbox.hashCode() + "=> " + res);
        }
        return new RpcFuture(ref, mbox, module + ":" + fun + "/" + args0.length,
                logCalls, this);
    }
View Full Code Here

    return "_connector_to_" + binding.getNode()
        + System.currentTimeMillis();
  }

  private Message sendMessage(Message msg) {
    OtpMbox tmpMbox = null;
    OtpNode node = null;
    try {
      node = new OtpNode(getClientNodeName());
      if (binding.hasCookie()) {
        node.setCookie(binding.getCookie());
      }
      tmpMbox = node.createMbox();
      // obtain args, make sure they aren't null
      // NOTE: sending message with no content (but only with senders PID)
      // is possible
      Object[] args = (Object[]) (msg.getBody() != null ? msg.getBody()
          : new Object[0]);
      Method jmethod = ((JavaOperation) msg.getOperation())
          .getJavaMethod();
      // create and send msg with self pid in the beginning
      OtpErlangObject[] argsArray = {
          tmpMbox.self(),
          TypeHelpersProxy.toErlang(args, jmethod
              .getParameterAnnotations()) };
      OtpErlangObject otpArgs = new OtpErlangTuple(argsArray);
      tmpMbox.send(msg.getOperation().getName(), binding.getNode(),
          otpArgs);
      if (msg.getOperation().getOutputType() != null) {
        OtpMsg resultMsg = null;
        if (binding.hasTimeout()) {
          resultMsg = tmpMbox.receiveMsg(binding.getTimeout());
        } else {
          resultMsg = tmpMbox.receiveMsg();
        }
        OtpErlangObject result = resultMsg.getMsg();
        msg.setBody(TypeHelpersProxy.toJava(result, msg.getOperation()
            .getOutputType().getPhysical(), jmethod
            .getAnnotations()));
      }
    } catch (InterruptedException e) {
      // TODO: externalize message?
      ErlangException ee = new ErlangException(
          "Timeout while receiving message reply", e);
      msg.setBody(null);
      reportProblem(msg, ee);
    } catch (Exception e) {
      reportProblem(msg, e);
    } finally {
      if (tmpMbox != null) {
        tmpMbox.close();
      }
      if (node != null) {
        OtpEpmd.unPublishPort(node);
        node.close();
      }
View Full Code Here

    public static void main(String argv[]) {

  try {
      OtpNode node = new OtpNode("javanode");
      OtpMbox mbox1 = node.createMbox();
      mbox1.registerName("mbox1");
      node.createMbox("mbox2");
      OtpMbox mbox3 = node.createMbox();
      node.registerName("mbox3",mbox3);

        ArrayList<String> existing_names = new ArrayList<String>();
      existing_names.add("mbox3");
      existing_names.add("mbox2");
View Full Code Here

  OtpErlangTuple msg = null;

  try {
      // Initiate: create javanode and mboxes
      OtpNode node = new OtpNode("javanode",cookie);
      OtpMbox mbox = node.createMbox();
      OtpMbox mbox2 = node.createMbox("java_echo_server2");

      // Send the pid of mbox to erlang and wait for test case
      // instruction: {TestCaseTag, Pid}
      mbox.send("erl_send_receive_server", erlNode, mbox.self());
      OtpErlangObject o = mbox.receive(recTime);
      if (o == null) System.exit(1);
      OtpErlangTuple testCase = (OtpErlangTuple)o;
      dbg("mbox received " + testCase);
      int tag = (int)((OtpErlangLong)testCase.elementAt(0)).longValue();
      OtpErlangPid erlangPid = (OtpErlangPid)testCase.elementAt(1);

      switch (tag) {

      case java_erlang_send_receive:

    // Test1 (happened during initiation):
    // Send mbox pid to erlang process with registered name.
    // Erlang process sent back its pid to the mbox pid.

    // Test2: Register name and sent it to the erlang pid. Erlang
    // process shall send message back to my registered name.

    mbox.registerName("java_echo_server");
    msgArray[0] = getNameNode("java_echo_server",node);
    msg = new OtpErlangTuple(msgArray);

    dbg("java_echo_server sending " + msg);
    mbox.send(erlangPid,msg);

    o = mbox.receive(recTime);
    dbg("java_echo_server received " + o);
    if (o == null) System.exit(2);
    if (!((OtpErlangAtom)o).equals(msgArray[1])) System.exit(3);

    // Test3: Same as Test2, but using a new mbox2 which
    // got its name already when it is created - i.e. not
    // using mbox.registerName
    msgArray[0] = getNameNode("java_echo_server2",node);
    msg = new OtpErlangTuple(msgArray);

    dbg("java_echo_server2 sending " + msg);
    mbox2.send(erlangPid,msg);

    o = mbox2.receive(recTime);
    dbg("java_echo_server received " + o);
    if (o == null) System.exit(4);
    if (!((OtpErlangAtom)o).equals(msgArray[1])) System.exit(5);

    break;

      case java_internal_send_receive_same_node:

    // Test1: Sending message between mboxes on same node
    // given registered name and node without host.
    mbox.send("java_echo_server2","javanode",msgArray[1]);
    o = mbox2.receive(recTime);
    dbg("Mbox at same node: " + o);
    if (o == null) System.exit(6);
    if(!((OtpErlangAtom)o).equals(msgArray[1])) System.exit(7);

    // Test2: Sending message between mboxes on same node
    // given registered name and node with host.
    mbox.send("java_echo_server2",mbox2.self().node(),msgArray[1]);
    o = mbox2.receive(recTime);
    dbg("Mbox at same node: " + o);
    if (o == null) System.exit(8);
    if(!((OtpErlangAtom)o).equals(msgArray[1])) System.exit(9);

    // Test3: Sending message between mboxes on same node
    // given registered name but not node.
    mbox.send("java_echo_server2",msgArray[1]);
    o = mbox2.receive(recTime);
    dbg("Mbox at same node: " + o);
    if (o == null) System.exit(10);
    if(!((OtpErlangAtom)o).equals(msgArray[1])) System.exit(11);

    // Test4: Sending message between mboxes on same node
    // given pid.
    mbox.send(mbox2.self(),msgArray[1]);
    o = mbox2.receive(recTime);
    dbg("Mbox at same node: " + o);
    if (o == null) System.exit(12);
    if(!((OtpErlangAtom)o).equals(msgArray[1])) System.exit(13);

    break;

      case java_internal_send_receive_different_nodes:

    OtpNode node2 = new OtpNode("javanode2", cookie);
    OtpMbox mboxOtherNode = node2.createMbox("mboxOtherNode");

    // Test1: Sending message between mboxes on different
    // nodes given registered name and node without host.
    mbox.send("mboxOtherNode","javanode2",msgArray[1]);
    o = mboxOtherNode.receive(recTime);
    dbg("Mbox at same node: " + o);
    if (o == null) System.exit(14);
    if(!((OtpErlangAtom)o).equals(msgArray[1])) System.exit(15);

    // Test2: Sending message between mboxes on different
    // nodes given registered name and node with host.
    mbox.send("mboxOtherNode",mboxOtherNode.self().node(),
        msgArray[1]);
    o = mboxOtherNode.receive(recTime);
    dbg("Mbox at same node: " + o);
    if (o == null) System.exit(16);
    if(!((OtpErlangAtom)o).equals(msgArray[1])) System.exit(17);

    // Test3: Sending message between mboxes on different
    // nodes given pid.
    mbox.send(mboxOtherNode.self(),msgArray[1]);
    o = mboxOtherNode.receive(recTime);
    dbg("Mbox at same node: " + o);
    if (o == null) System.exit(18);
    if(!((OtpErlangAtom)o).equals(msgArray[1])) System.exit(19);

    break;
View Full Code Here

      final OtpNode node = new OtpNode(argv[0], argv[1]);
      if (argv.length == 5 && argv[4].equals("unicode")) {
    System.out.println("Setting unicode string mode.");
    node.setFlags(OtpInputStream.DECODE_INT_LISTS_AS_STRINGS);
      }
      final OtpMbox mbox = node.createMbox();

      // Announce our presence
      final OtpErlangObject[] amsg = new OtpErlangObject[3];
      amsg[0] = new OtpErlangAtom("echo_server");
      amsg[1] = new OtpErlangAtom(argv[0]);
      amsg[2] = mbox.self();
      final OtpErlangTuple atuple = new OtpErlangTuple(amsg);
      mbox.send(argv[3], argv[2], atuple);

      // Do echoing...
      while (true) {
    final OtpErlangObject o = mbox.receive();
    if (o == null) {
        continue;
    }
    if (o instanceof OtpErlangTuple) {
        final OtpErlangTuple msg = (OtpErlangTuple) o;
        final int arity = msg.arity();
        if (arity < 2) {
      System.out
        .println("Arity < 2; echo_server aborting...");
      System.exit(2);
        } else if (arity == 2) {
      final OtpErlangPid from = (OtpErlangPid) msg
        .elementAt(0);
      if (debug) System.out.println("Echoing: "
                  + msg.elementAt(1));

      final OtpErlangObject[] rmsg = new OtpErlangObject[2];
      rmsg[0] = mbox.self();
      rmsg[1] = msg.elementAt(1);
      final OtpErlangTuple rtuple = new OtpErlangTuple(rmsg);

      mbox.send(from, rtuple);
      continue;
        } else if (arity == 3) {
      echoTwisted(mbox, msg);
      continue;
        } else {
View Full Code Here

  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;
View Full Code Here

TOP

Related Classes of com.ericsson.otp.erlang.OtpMbox

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.