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; }
void EIOutputBuffer::writeTerm(ErlTerm *t, const VariableBinding *binding) throw(EpiInvalidTerm, EpiEncodeException, EpiVariableUnbound) { if (t) { Dout_continue(dc::connect, _continue, " failed.", "InputBuffer::WriteTerm(" << t->toString() << "):"); int ei_res; switch(t->termType()) { case ERL_ATOM: ei_res = ei_x_encode_atom(this->getBuffer(), ((ErlAtom*) t)->atomValue()); if (ei_res < 0) { throw EpiEIEncodeException("EI atom encoding failed", ei_res); } break; case ERL_LONG: ei_res = ei_x_encode_longlong(this->getBuffer(), ((ErlLong*) t)->longValue()); if (ei_res < 0) { throw EpiEIEncodeException("EI long encoding failed", ei_res); } break; case ERL_DOUBLE: ei_res = ei_x_encode_double(this->getBuffer(), ((ErlDouble*) t)->doubleValue()); if (ei_res < 0) { throw EpiEIEncodeException("EI double encoding failed", ei_res); } break; case ERL_STRING: ei_res = ei_x_encode_string(this->getBuffer(), ((ErlString*) t)->stringValue()); if (ei_res < 0) { throw EpiEIEncodeException("EI string encoding failed", ei_res); } break; case ERL_REF: if (1==1) { erlang_ref *ref; if ((ref = ErlRef2EI((ErlRef*) t)) == 0) { throw EpiInvalidTerm("Reference not initialized"); } ei_res = ei_x_encode_ref(this->getBuffer(), ref); delete ref; if (ei_res < 0) { throw EpiEIEncodeException("EI ref encoding failed", ei_res); } } break; case ERL_PORT: if (1==1) { erlang_port *port; if ((port = ErlPort2EI((ErlPort*) t)) == 0) { throw EpiInvalidTerm("Reference not initialized"); } ei_res = ei_x_encode_port(this->getBuffer(), port); delete port; if (ei_res < 0) { throw EpiEIEncodeException("EI port encoding failed", ei_res); } } break; case ERL_PID: if (1==1) { erlang_pid *pid; if ((pid = ErlPid2EI((ErlPid*) t)) == 0) { throw EpiInvalidTerm("Reference not initialized"); } ei_res = ei_x_encode_pid(this->getBuffer(), pid); delete pid; if (ei_res < 0) { throw EpiEIEncodeException("EI pid encoding failed", ei_res); } } break; case ERL_BINARY: ei_res = ei_x_encode_binary(this->getBuffer(), ((ErlBinary*) t)->binaryData(), ((ErlBinary*) t)->size()); if (ei_res < 0) { throw EpiEIEncodeException("EI binary encoding failed", ei_res); } break; case ERL_TUPLE: if (1==1) { ErlTuple *tuple = (ErlTuple*) t; ei_res = ei_x_encode_tuple_header(this->getBuffer(), tuple->arity()); if (ei_res < 0) { throw EpiEIEncodeException("EI tuple encoding failed", ei_res); } for (unsigned int i = 0; i < tuple->arity(); i++) { this->writeTerm(tuple->elementAt(i), binding); } } break; case ERL_EMPTY_LIST: ei_res = ei_x_encode_empty_list(this->getBuffer()); if (ei_res < 0) { throw EpiEIEncodeException("EI empty list encoding failed", ei_res); } break; case ERL_LIST: case ERL_CONS_LIST: if (1==1) { ErlConsList* list = (ErlConsList*) t; ei_x_encode_list_header(this->getBuffer(), list->arity()); // Encode all elements and last tail for(unsigned int i=0; i<list->arity(); i++) { this->writeTerm(list->elementAt(i), binding); } this->writeTerm(list->tail(list->arity()-1), binding); } break; case ERL_VARIABLE: if (1==1) { ErlVariable* variable = (ErlVariable*) t; if (variable->getName() == "_") { throw EpiInvalidTerm("You can't encode an anonymous variable"); } ErlTerm* term = binding? binding->search(variable->getName()): 0; if (term) { this->writeTerm(term, binding); } else { throw EpiVariableUnbound(variable->getName()); } } break; } Dout_finish(_continue, " encoded."); } else { throw EpiInvalidTerm("trying to encode a null pointer"); } }