Beispiel #1
0
bool Port::open(const Contact& contact, bool registerName,
                const char *fakeName) {
    Contact contact2 = contact;

    if (!NetworkBase::initialized()) {
        YARP_ERROR(Logger::get(), "YARP not initialized; create a yarp::os::Network object before using ports");
        return false;
    }

    ConstString n = contact2.getName();
    if (n!="..." && n!="" && n[0]!='/') {
        if (fakeName==NULL) {
            YARP_SPRINTF1(Logger::get(),error,
                          "Port name '%s' needs to start with a '/' character",
                          n.c_str());
            return false;
        }
    }
    if (n!="..." && n!="") {
        if (fakeName==NULL) {
            ConstString prefix = NetworkBase::getEnvironment("YARP_PORT_PREFIX");
            if (prefix!="") {
                n = prefix + n;
                contact2 = contact2.addName(n);
            }
        }
    }

    // Allow for open() to be called safely many times on the same Port
    PortCoreAdapter *currentCore = &(HELPER(implementation));
    if (currentCore->isOpened()) {
        PortCoreAdapter *newCore = new PortCoreAdapter(*this);
        YARP_ASSERT(newCore!=NULL);
        // copy state that should survive in a new open()
        if (currentCore->checkPortReader()!=NULL) {
            newCore->configReader(*(currentCore->checkPortReader()));
        }
        if (currentCore->checkReadCreator()!=NULL) {
            newCore->configReadCreator(*(currentCore->checkReadCreator()));
        }
        if (currentCore->checkWaitAfterSend()>=0) {
            newCore->configWaitAfterSend(currentCore->checkWaitAfterSend());
        }
        close();
        delete ((PortCoreAdapter*)implementation);
        implementation = newCore;
    }

    PortCoreAdapter& core = HELPER(implementation);

    core.openable();

    bool local = false;
    if (NetworkBase::localNetworkAllocation()&&contact2.getPort()<=0) {
        YARP_DEBUG(Logger::get(),"local network allocation needed");
        local = true;
    }

    bool success = true;
    Address caddress(contact2.getHost().c_str(),
                     contact2.getPort(),
                     contact2.getCarrier().c_str(),
                     contact2.getName().c_str());
    Address address = caddress;

    core.setReadHandler(core);
    if (contact2.getPort()>0 && contact2.getHost()!="") {
        registerName = false;
    }
    if (registerName&&!local) {
        Contact contactFull = NetworkBase::registerContact(contact2);
        address = Address::fromContact(contactFull);
    }

    core.setControlRegistration(registerName);
    success = (address.isValid()||local)&&(fakeName==NULL);

    ConstString blame = "invalid address";
    if (success) {
        success = core.listen(address,registerName);
        blame = "address conflict";
        if (success) {
            success = core.start();
            blame = "manager did not start";
        }
    }
    if (success) {
        address = core.getAddress();
        if (registerName&&local) {
            contact2 = contact2.addSocket(address.getCarrierName().c_str(),
                                          address.getName().c_str(),
                                          address.getPort());
            contact2 = contact2.addName(address.getRegName().c_str());
            Contact newName = NetworkBase::registerContact(contact2);
            core.resetPortName(newName.getName());
            address = core.getAddress();
        }

        if (core.getVerbosity()>=1) {
            YARP_INFO(Logger::get(),
                      String("Port ") +
                      address.getRegName() +
                      " active at " +
                      address.toString());
        }
    }

    if (fakeName!=NULL) {
        success = core.manualStart(fakeName);
        blame = "unmanaged port failed to start";
    }

    if (!success) {
        YARP_ERROR(Logger::get(),
                   String("Port ") +
                   (address.isValid()?(address.getRegName().c_str()):(contact2.getName().c_str())) +
                   " failed to activate" +
                   (address.isValid()?" at ":"") +
                   (address.isValid()?address.toString():String("")) +
                   " (" +
                   blame.c_str() +
                   ")");
    }
    return success;
}
Beispiel #2
0
void yarp::os::impl::HttpTwoWayStream::apply(char ch) {
    if (ch=='\r') { return; }
    if (ch == '\n') {
        proc = "";
        Address addr = yarp::os::impl::NameClient::extractAddress(part);
        if (addr.isValid()) {
            if (addr.getCarrierName()=="tcp"&&
                (YARP_STRSTR(addr.getRegName(),"/quit")==String::npos)) {
                proc += "<a href=\"http://";
                proc += addr.getName();
                proc += ":";
                proc += NetType::toString(addr.getPort());
                proc += "\">";
                proc += addr.getRegName();
                proc += "</A> ";
                size_t len = addr.getRegName().length();
                size_t target = 30;
                if (len<target) {
                    for (size_t i=0; i<target-len; i++) {
                        proc += " ";
                    }
                }
                proc += "(";
                proc += addr.toString().c_str();
                proc += ")";
                proc += "\n";
            } else {
                // Don't show non tcp connections
                //proc += part;
                //proc += "\n";
            }
        } else {
            if ((part[0]=='\"'&&part[1]=='[')||(part[0]=='+')) {
                // translate this to a form
                if (part[0]=='+') { part[0] = ' '; }
                String org = part;
                part = "<p><form method=post action='/form'>";
                size_t i=0;
                for (i=0; i<org.length(); i++) {
                    if (org[i]=='"') {
                        org[i] = ' ';
                    }
                }
                part += "<input type=hidden name=data value=\"";
                part += org;
                part += "\">";
                part += org;
                org += " ";
                bool arg = false;
                String var = "";
                for (i=0; i<org.length(); i++) {
                    char ch = org[i];
                    if (arg) {
                        if ((ch>='A'&&ch<='Z')||
                            (ch>='a'&&ch<='z')||
                            (ch>='0'&&ch<='9')||
                            (ch=='_')) {
                            var += ch;
                        } else {
                            arg = false;
                            part += "\n    ";
                            part += var;
                            part += " ";
                            part += "<input type=text name=";
                            part += var;
                            part += " size=5 value=\"\">";
                            var = "";
                        }
                    }
                    if (ch=='$') {
                        arg = true;
                    }
                }
                part += "<input type=submit value=\"go\">";
                part += "</form></p>";
            }
            proc += part;
            proc += "\n";
        }
        if (data||!filterData) {
            Bytes tmp((char*)proc.c_str(),proc.length());
            delegate->getOutputStream().write(tmp);
            delegate->getOutputStream().flush();
        }
        data = false;
        if (proc[0] == 'd' || proc[0] == 'D') {
            data = true;
        }
        part = "";
    } else {
        part += ch;
    }
}