Esempio n. 1
0
void Protocol::setCarrier(const std::string& carrierNameBase)
{
    // Set up the carrier for this connection.  The carrier
    // has all the protocol-specific behavior.
    std::string carrierName = carrierNameBase;
    if (carrierNameBase == "")
        carrierName = "tcp";
    Route route = getRoute();
    route.setCarrierName(carrierName);
    setRoute(route);
    if (delegate == nullptr) {
        delegate = Carriers::chooseCarrier(carrierName);
        if (delegate != nullptr) {
            if (delegate->modifiesIncomingData()) {
                if (active) {
                    fprintf(stderr, "Carrier \"%s\" cannot be used this way, try \"tcp+recv.%s\" instead.\n", carrierName.c_str(), carrierName.c_str());
                }
                close();
                return;
            }
            // Configure the carrier.
            if (!delegate->configure(*this)) {
                fprintf(stderr, "Carrier \"%s\" could not be configured.\n", carrierName.c_str());
                close();
                return;
            }
            delegate->prepareSend(*this);
        }
    }
}
Esempio n. 2
0
void Protocol::setRoute(const Route& route)
{
    Route r = route;

    // We reorganize the route to reduce variation in naming.
    // If there are qualifiers in the source port name, propagate
    // those qualifiers to the carrier.
    std::string from = r.getFromName();
    std::string carrier = r.getCarrierName();
    if (from.find(' ') != std::string::npos) {
        Bottle b(from);
        if (b.size() > 1) {
            r.setFromName(b.get(0).toString());
            for (size_t i = 1; i < b.size(); i++) {
                Value& v = b.get(i);
                Bottle* lst = v.asList();
                if (lst != nullptr) {
                    carrier.append("+").append(lst->get(0).toString()).append(".").append(lst->get(1).toString());
                } else {
                    carrier.append("+").append(v.toString());
                }
            }
            r.setCarrierName(carrier);
        }
    }

    // Record canonicalized route.
    this->route = r;

    // Check if we have a receiver modifier.
    if (!recv_delegate) {
        Bottle b(getSenderSpecifier());
        if (b.check("recv")) {
            need_recv_delegate = true;
        }
    }

    // Check if we have a sender modifier.
    if (!send_delegate) {
        Bottle b(getSenderSpecifier());
        if (b.check("send")) {
            need_send_delegate = true;
        }
    }
}
Esempio n. 3
0
bool Protocol::expectProtocolSpecifier()
{
    // Historically YARP has used the first 8 bytes of
    // every connection as a way to identify it.  This
    // assumption is showing its age, and should really
    // be generalized.
    char buf[8];
    yarp::os::Bytes header((char*)&buf[0], sizeof(buf));
    yarp::conf::ssize_t len = is().readFull(header);
    if (len == -1) {
        YARP_DEBUG(log, "no connection");
        return false;
    }
    if ((size_t)len != header.length()) {
        YARP_DEBUG(log, "data stream died");
        return false;
    }
    bool already = false;
    if (delegate != nullptr) {
        if (delegate->checkHeader(header)) {
            already = true;
        }
    }
    if (!already) {
        delegate = Carriers::chooseCarrier(header);
        if (delegate == nullptr) {
            // Carrier not found; send a human-readable message.
            std::string msg = "* Error. Protocol not found.\r\n* Hello. You appear to be trying to communicate with a YARP Port.\r\n* The first 8 bytes sent to a YARP Port are critical for identifying the\r\n* protocol you wish to speak.\r\n* The first 8 bytes you sent were not associated with any particular protocol.\r\n* If you are a human, try typing \"CONNECT foo\" followed by a <RETURN>.\r\n* The 8 bytes \"CONNECT \" correspond to a simple text-mode protocol.\r\n* Goodbye.\r\n";
            yarp::os::Bytes b((char*)msg.c_str(), msg.length());
            os().write(b);
            os().flush();
        }
    }
    if (delegate == nullptr) {
        YARP_DEBUG(log, "unrecognized protocol");
        return false;
    }
    Route r = getRoute();
    r.setCarrierName(delegate->getName());
    setRoute(r);
    delegate->setParameters(header);
    return true;
}