Exemple #1
0
bool test_accept(LocalNode &node)
        throw (EpiException)
{

    node.publishPort();

    std::auto_ptr<Connection> connection(node.accept());
    std::auto_ptr<MailBox> mailbox(node.createMailBox(connection.get()));
    connection->start();

    std::auto_ptr<ErlangMessage> msg;
    ErlTerm* received_term;
    OutputBuffer *buffer = mailbox->newOutputBuffer();

    bool loop = true;
    while (loop) {
        msg.reset(mailbox->receiveMsg());

        if (!(msg->messageType() == ERL_MSG_SEND ||
              msg->messageType() == ERL_MSG_REG_SEND)) {
            std::cout << "Message is not SEND type\n";
            return false;
        }

        received_term = msg->getMsg();

        if (msg->messageType() == ERL_MSG_REG_SEND) {
            std::cout << "Received to " << ((RegSendMessage *) msg.get())->getRecipientName() <<
                    ": " << received_term->toString() << "\n";
        } else {
            std::cout << "Received to " << ((SendMessage *) msg.get())->getRecipientPid()->toString() <<
                    ": " << received_term->toString() << "\n";
        }

        if (received_term->instanceOf(ERL_TUPLE)) {
            ErlTuple *tuple = (ErlTuple *) received_term;
            if (tuple->arity() == 2) {
                ErlTerm* term1 = tuple->elementAt(0);
                ErlTerm* term2 = tuple->elementAt(1);
                if (term1->instanceOf(ERL_PID)) {

                    std::cout << "Sending " << term2->toString() <<
                            " to " << term1->toString() << "\n";

                    buffer->reset();
                    buffer->writeTerm(term2);
                    mailbox->sendBuf((ErlPid *) term1, buffer);
                    std::cout << "Sent.\n";
                }
            }
        }

        if (received_term->instanceOf(ERL_ATOM)) {
            if (((ErlAtom *) received_term)->atomValue() == "exit") {
                loop = false;
            }
        }
    }
    return true;
}
Exemple #2
0
// Test send and receive with a reply server.
bool send_reply_server(ErlTerm *t, MailBox *mailbox)
        throw(EpiException)
{

    std::cout << "Sending term: " << t->toString() << "\n";

    OutputBuffer *buffer = mailbox->newOutputBuffer();

    // Create tuple with pid
    ErlPid *pid = mailbox->self();
    ErlTermPtr<ErlTuple> tuple(new ErlTuple(pid, t));

    buffer->writeTerm(tuple.get());

    mailbox->sendBuf("reply_server", buffer);

    std::cout << "Sent. Receiving response:\n";

    std::auto_ptr<EpiMessage> msg(mailbox->receiveMsg());

    if (msg->messageType() != ERL_MSG_SEND) {
        std::cout << "Message is not SEND type\n";
        return false;
    }

    ErlTerm* t2 = ((ErlangMessage *) msg.get())->getMsg();

    std::cout << "Got term: " << t2->toString() << "\n";

    bool equals = t->equals(*t2);
    if (equals) {
        std::cout << "Equals, ok" << "\n";
    } else {
        std::cout << "Not equals, error!" << "\n";
    }
    return equals;
}