Пример #1
0
bool AbstractCarrier::expectSenderSpecifier(ConnectionState& proto) {
    NetInt32 numberSrc;
    Bytes number((char*)&numberSrc,sizeof(NetInt32));
    int len = 0;
    YARP_SSIZE_T r = proto.is().readFull(number);
    if ((size_t)r!=number.length()) {
        YARP_DEBUG(Logger::get(),"did not get sender name length");
        return false;
    }
    len = NetType::netInt(number);
    if (len>1000) len = 1000;
    if (len<1) len = 1;
    // expect a string -- these days null terminated, but not in YARP1
    ManagedBytes b(len+1);
    r = proto.is().readFull(Bytes(b.get(),len));
    if ((int)r!=len) {
        YARP_DEBUG(Logger::get(),"did not get sender name");
        return false;
    }
    // add null termination for YARP1
    b.get()[len] = '\0';
    String s = b.get();
    proto.setRoute(proto.getRoute().addFromName(s));
    return true;
}
Пример #2
0
bool yarp::os::impl::UdpCarrier::expectReplyToHeader(ConnectionState& proto) {
    // I am the sender
    int myPort = proto.getStreams().getLocalAddress().getPort();
    ConstString myName = proto.getStreams().getLocalAddress().getHost();
    ConstString altName = proto.getStreams().getRemoteAddress().getHost();

    int altPort = readYarpInt(proto);

    if (altPort==-1) {
        return false;
    }

    DgramTwoWayStream *stream = new DgramTwoWayStream();
    yAssert(stream!=YARP_NULLPTR);

    proto.takeStreams(YARP_NULLPTR); // free up port from tcp
    bool ok =
        stream->open(Contact(myName,myPort),Contact(altName,altPort));
    if (!ok) {
        delete stream;
        return false;
    }
    proto.takeStreams(stream);
    return true;
}
Пример #3
0
// This is called when a connection is being established and returns an initialised ConnectionState instance.
ConnectionState *Network::ConnectionAccepted(tcp_pcb *pcb)
{
	ConnectionState *cs = freeConnections;
	if (cs == NULL)
	{
		platform->Message(HOST_MESSAGE, "Network::ConnectionAccepted() - no free ConnectionStates!\n");
		return NULL;
	}

	NetworkTransaction* r = freeTransactions;
	if (r == NULL)
	{
		platform->Message(HOST_MESSAGE, "Network::ConnectionAccepted() - no free transactions!\n");
		return NULL;
	}

	freeConnections = cs->next;
	cs->Init(pcb);

	r->Set(NULL, cs, connected);
	freeTransactions = r->next;
	AppendTransaction(&readyTransactions, r);

	return cs;
}
Пример #4
0
void send_net_data(JOCTET *data, int len, void *client) {
    dbg_printf("Send %d bytes\n", len);
    ConnectionState *p = (ConnectionState *)client;
    char hdr[1000];
    sprintf(hdr,"\n");
    const char *brk = "\n";
    if (hdr[1]=='\0') {
        brk = "\r\n";
    }
    dbg_printf("Using terminator %s\n",(hdr[1]=='\0')?"\\r\\n":"\\n");
    sprintf(hdr,"Content-Type: image/jpeg%s\
Content-Length: %d%s%s", brk, len, brk, brk);
    Bytes hbuf(hdr,strlen(hdr));
    p->os().write(hbuf);
    Bytes buf((char *)data,len);
    /*
      // add corruption now and then, for testing.
    static int ct = 0;
    ct++;
    if (ct==50) {
        printf("Adding corruption\n");
        buf.get()[0] = 'z';
        ct = 0;
    }
    */
    p->os().write(buf);
    sprintf(hdr,"%s--boundarydonotcross%s",brk,brk);
    Bytes hbuf2(hdr,strlen(hdr));
    p->os().write(hbuf2);

}
Пример #5
0
bool AbstractCarrier::expectSenderSpecifier(ConnectionState& proto)
{
    NetInt32 numberSrc;
    Bytes number((char*)&numberSrc, sizeof(NetInt32));
    int len = 0;
    YARP_SSIZE_T r = proto.is().readFull(number);
    if ((size_t)r!=number.length()) {
        YARP_DEBUG(Logger::get(), "did not get sender name length");
        return false;
    }
    len = NetType::netInt(number);
    if (len>1000) {
        len = 1000;
    }
    if (len<1) {
        len = 1;
    }
    ManagedBytes b(len+1);
    r = proto.is().readFull(Bytes(b.get(), len));
    if ((int)r!=len) {
        YARP_DEBUG(Logger::get(), "did not get sender name");
        return false;
    }
    ConstString s = b.get();
    Route route = proto.getRoute();
    route.setFromName(s);
    proto.setRoute(route);
    return true;
}
Пример #6
0
bool XmlRpcCarrier::shouldInterpretRosMessages(ConnectionState& proto) {
    // We need to set the interpretRos flag, which controls
    // whether ROS-style admin messages are treated as
    // admin messages or data messages in YARP.
    // In the future, they should always be data messages.
    // For now, they should be admin messages for all ports
    // except ports tagged as corresponding to ros nodes.

    bool nodelike = false;
    Contactable *port = proto.getContactable();
    Property opt;
    if (port) {
        Property *pport = port->acquireProperties(true);
        if (pport) {
            opt = *pport;
        }
        port->releaseProperties(pport);
    }
    if (opt.check("node_like")) {
        nodelike = true;
    }

    Name n(proto.getRoute().getCarrierName() + "://test");
    ConstString rospass = n.getCarrierModifier("ros");
    interpretRos = !nodelike;
    if (rospass=="1"||rospass=="on") {
        interpretRos = true;
    }
    if (rospass=="0"||rospass=="off") {
        interpretRos = false;
    }
    return interpretRos;
}
Пример #7
0
bool yarp::os::impl::TextCarrier::expectSenderSpecifier(ConnectionState& proto)
{
    YARP_SPRINTF0(Logger::get(), debug, "TextCarrier::expectSenderSpecifier");
    Route route = proto.getRoute();
    route.setFromName(proto.is().readLine());
    proto.setRoute(route);
    return true;
}
Пример #8
0
bool AbstractCarrier::write(ConnectionState& proto, SizedWriter& writer) {
    bool ok = sendIndex(proto,writer);
    if (!ok) {
        return false;
    }
    writer.write(proto.os());
    proto.os().flush();
    return proto.os().isOk();
}
Пример #9
0
bool AbstractCarrier::sendConnectionStateSpecifier(ConnectionState& proto) {
    char buf[8];
    Bytes header((char*)&buf[0],sizeof(buf));
    OutputStream& os = proto.os();
    proto.getConnection().getHeader(header);
    os.write(header);
    os.flush();
    return os.isOk();
}
Пример #10
0
bool yarp::os::impl::McastCarrier::becomeMcast(ConnectionState& proto, bool sender) {
#ifndef YARP_HAS_ACE
    return false;
#else
    YARP_UNUSED(sender);
    DgramTwoWayStream *stream = new DgramTwoWayStream();
    yAssert(stream!=YARP_NULLPTR);
    Contact remote = proto.getStreams().getRemoteAddress();
    Contact local;
    local = proto.getStreams().getLocalAddress();
    bool test = true;
    //(yarp::NameConfig::getEnv("YARP_MCAST_TEST")!="");
    /*
    if (test) {
        printf("  MULTICAST is being extended; some temporary status messages added\n");
        printf("  Local: %s\n", local.toString().c_str());
        printf("  Remote: %s\n", remote.toString().c_str());
    }
    */
    proto.takeStreams(YARP_NULLPTR); // free up port from tcp

    if (sender) {
        /*
            Multicast behavior seems a bit variable.
            We assume here that if packages need to be broadcast
            to targets via different network interfaces, that
            we'll need to send independently on those two
            interfaces.  This may or may not always be the case,
            the author doesn't know, so is being cautious.
        */
        key = proto.getRoute().getFromName();
        if (test) {
            key += "/net=";
            key += local.getHost();
        }
        YARP_DEBUG(Logger::get(),
                    ConstString("multicast key: ") + key);
        addSender(key);
    }

    bool ok = true;
    if (isElect()||!sender) {
        if (test) {
            ok = stream->join(mcastAddress,sender,local);
        } else {
            ok = stream->join(mcastAddress,sender);
        }
    }

    if (!ok) {
        delete stream;
        return false;
    }
    proto.takeStreams(stream);
    return true;
#endif
}
Пример #11
0
bool yarp::os::impl::HttpCarrier::respondToHeader(ConnectionState& proto) {
    stream = new HttpTwoWayStream(proto.giveStreams(),
                                  input.c_str(),
                                  prefix.c_str(),
                                  prop,
                                  false);
    proto.takeStreams(stream);
    return true;
}
Пример #12
0
bool yarp::os::impl::TextCarrier::respondToHeader(ConnectionState& proto)
{
    std::string from = "Welcome ";
    from += proto.getRoute().getFromName();
    from += "\r\n";
    yarp::os::Bytes b2((char*)from.c_str(), from.length());
    proto.os().write(b2);
    proto.os().flush();
    return proto.os().isOk();
}
Пример #13
0
bool yarp::os::impl::TextCarrier::sendAck(ConnectionState& proto)
{
    if (ackVariant) {
        std::string from = "<ACK>\r\n";
        Bytes b2((char*)from.c_str(), from.length());
        proto.os().write(b2);
        proto.os().flush();
    }
    return proto.os().isOk();
}
Пример #14
0
bool XmlRpcCarrier::respondToHeader(ConnectionState& proto) {
    shouldInterpretRosMessages(proto);
    sender = false;
    XmlRpcStream *stream = new XmlRpcStream(proto.giveStreams(),
                                            sender,
                                            interpretRos);
    if (stream==NULL) { return false; }
    proto.takeStreams(stream);
    return true;
}
Пример #15
0
int AbstractCarrier::readYarpInt(ConnectionState& proto) {
    char buf[8];
    Bytes header((char*)&buf[0],sizeof(buf));
    YARP_SSIZE_T len = proto.is().readFull(header);
    if ((size_t)len!=header.length()) {
        YARP_DEBUG(proto.getLog(),"data stream died");
        return -1;
    }
    return interpretYarpNumber(header);
}
Пример #16
0
bool MjpegCarrier::sendHeader(ConnectionState& proto) {
    Name n(proto.getRoute().getCarrierName() + "://test");
    ConstString pathValue = n.getCarrierModifier("path");
    ConstString target = "GET /?action=stream\n\n";
    if (pathValue!="") {
        target = "GET /";
        target += pathValue;
    }
    target += " HTTP/1.1\n\n";
    Bytes b((char*)target.c_str(),target.length());
    proto.os().write(b);
    return true;
}
Пример #17
0
bool yarp::os::impl::LocalCarrier::expectExtraHeader(ConnectionState& proto) {
    portName = proto.getRoute().getToName();
    // switch over to some local structure to communicate
    peerMutex.wait();
    peer = manager.getSender(this);
    //printf("receiver %ld (%s) sees sender %ld (%s)\n",
    //       (long int) this, portName.c_str(),
    //       (long int) peer, peer->portName.c_str());
    proto.setRoute(proto.getRoute().addFromName(peer->portName));
    peerMutex.post();

    return true;
}
Пример #18
0
bool AbstractCarrier::sendSenderSpecifier(ConnectionState& proto) {
    NetInt32 numberSrc;
    Bytes number((char*)&numberSrc,sizeof(NetInt32));
    const String senderName = proto.getSenderSpecifier();
    //const String& senderName = getRoute().getFromName();
    NetType::netInt((int)senderName.length()+1,number);
    OutputStream& os = proto.os();
    os.write(number);
    Bytes b((char*)senderName.c_str(),senderName.length()+1);
    os.write(b);
    os.flush();
    return os.isOk();
}
Пример #19
0
 bool MpiCarrier::expectReplyToHeader(ConnectionState& proto) {
    // SWITCH TO NEW STREAM TYPE
    if (!comm->accept()) {
        delete stream;
        return false;
    }
    proto.takeStreams(stream);

    #ifdef MPI_DEBUG
    printf("[MpiCarrier @ %s] MpiStream successfully setup \n", route.c_str() );
    #endif

    return proto.os().isOk();
}
Пример #20
0
bool AbstractCarrier::defaultSendAck(ConnectionState& proto) {
    YARP_DEBUG(Logger::get(),"sending an acknowledgment");
    if (proto.getConnection().requireAck()) {
        writeYarpInt(0,proto);
    }
    return true;
}
Пример #21
0
bool TcpRosCarrier::reply(ConnectionState& proto, SizedWriter& writer) {
    char twiddle[1];
    twiddle[0] = 1;
    Bytes twiddle_buf(twiddle,1);
    proto.os().write(twiddle_buf);
    return write(proto,writer);
}
Пример #22
0
bool yarp::os::impl::McastCarrier::expectExtraHeader(ConnectionState& proto) {
    YARP_DEBUG(Logger::get(),"Expecting extra mcast header");
    ManagedBytes block(6);
    YARP_SSIZE_T len = proto.is().readFull(block.bytes());
    if ((size_t)len!=block.length()) {
        YARP_ERROR(Logger::get(),"problem with MCAST header");
        return false;
    }

    int ip[] = { 0, 0, 0, 0 };
    int port = -1;

    unsigned char *base = (unsigned char *)block.get();
    ConstString add;
    for (int i=0; i<4; i++) {
        ip[i] = base[i];
        if (i!=0) { add += "."; }
        char buf[100];
        ACE_OS::sprintf(buf,"%d",ip[i]);
        add += buf;
    }
    port = 256*base[4]+base[5];
    Contact addr("mcast", add, port);
    YARP_DEBUG(Logger::get(),ConstString("got mcast header ") + addr.toURI());
    mcastAddress = addr;

    return true;
}
Пример #23
0
void AbstractCarrier::writeYarpInt(int n, ConnectionState& proto)
{
    char buf[8];
    Bytes header(&(buf[0]), sizeof(buf));
    createYarpNumber(n, header);
    proto.os().write(header);
}
Пример #24
0
bool yarp::os::impl::TextCarrier::expectReplyToHeader(ConnectionState& proto)
{
    if (ackVariant) {
        // expect and ignore welcome line
        std::string result = proto.is().readLine();
    }
    return true;
}
Пример #25
0
bool XmlRpcCarrier::sendHeader(ConnectionState& proto) {
    shouldInterpretRosMessages(proto);
    ConstString target = "POST /RPC2";
    Name n(proto.getRoute().getCarrierName() + "://test");
    ConstString pathValue = n.getCarrierModifier("path");
    if (pathValue!="") {
        target = "POST /";
        target += pathValue;
        // on the wider web, we should provide real host names
        host = NetworkBase::queryName(proto.getRoute().getToName());
    }
    target += " HTTP/1.1\n";
    http = target;
    Bytes b((char*)target.c_str(),target.length());
    proto.os().write(b);
    return true;
}
Пример #26
0
bool yarp::os::impl::LocalCarrier::expectIndex(ConnectionState& proto) {

    YARP_DEBUG(Logger::get(), "local recv: wait send");
    sent.wait();
    YARP_DEBUG(Logger::get(), "local recv: got send");
    proto.setReference(ref);
    received.post();
    if (ref != nullptr) {
        YARP_DEBUG(Logger::get(), "local recv: received");
    } else {
        YARP_DEBUG(Logger::get(), "local recv: shutdown");
        proto.is().interrupt();
        return false;
    }

    return true;
}
Пример #27
0
bool yarp::os::impl::TextCarrier::expectAck(ConnectionState& proto)
{
    if (ackVariant) {
        // expect and ignore acknowledgement
        std::string result = proto.is().readLine();
    }
    return true;
}
Пример #28
0
 bool MpiCarrier::expectSenderSpecifier(ConnectionState& proto) {
    // interpret everything that sendHeader wrote
    name = proto.getRoute().getToName();

    #ifdef MPI_DEBUG
    printf("[MpiCarrier @ %s] Waiting for header\n", route.c_str());
    #endif

    other = proto.is().readLine();
    proto.setRoute(proto.getRoute().addFromName(other));
    // Receiver
    route = name + "<-" + other;

    createStream(false);
    if (!MpiControl) return false;
    if (! MpiControl->isRunning())
        return false;

    ConstString other_id = proto.is().readLine();
    bool notLocal = comm->notLocal(other_id);

    port = proto.is().readLine();

    #ifdef MPI_DEBUG
    printf("[MpiCarrier @ %s] Header received\n", route.c_str());
    #endif

    return notLocal && proto.is().isOk();
}
Пример #29
0
 bool MpiCarrier::respondToHeader(ConnectionState& proto) {
    // SWITCH TO NEW STREAM TYPE
    #ifdef MPI_DEBUG
    printf("[MpiCarrier @ %s] trying to connect to MpiPort '%s'\n", route.c_str(), port.c_str());
    #endif

    if (!comm->connect(port)) {
        delete stream;
        return false;
    }
    proto.takeStreams(stream);

    #ifdef MPI_DEBUG
    printf("[MpiCarrier @ %s] MpiStream successfully setup \n", route.c_str() );
    #endif

    return proto.is().isOk();
}
Пример #30
0
bool yarp::os::impl::HttpCarrier::sendHeader(ConnectionState& proto) {
    ConstString target = "GET / HTTP/1.0\r\n";
    ConstString path = proto.getRoute().getToName();
    if (path.size()>=2) {
        target = "GET " + path + " HTTP/1.0\r\n";
    }
    Contact host = proto.getRoute().getToContact();
    if (host.getHost()!="") {
        target += "Host: ";
        target += host.getHost();
        target += "\r\n";
    }
    target += "\r\n";
    Bytes b((char*)target.c_str(),target.length());
    proto.os().write(b);
    return true;

}