示例#1
0
bool BottleImpl::write(ConnectionWriter& writer) {
    // could simplify this if knew lengths of blocks up front
    if (writer.isTextMode()) {
        //writer.appendLine(toString());
        writer.appendString(toString().c_str(),'\n');
    } else {
#ifdef USE_YARP1_PREFIX
        if (!nested) {
            String name = "YARP2";
            writer.appendInt(name.length()+1);
            writer.appendString(name.c_str(),'\0');
        }
#endif
        synch();
        /*
          if (!nested) {
          // No byte count any more, to facilitate nesting
          //YMSG(("bottle byte count %d\n",byteCount()));
          //writer.appendInt(byteCount()+sizeof(NetInt32));
          
          writer.appendInt(StoreList::code + speciality);
          }
        */
        //writer.appendBlockCopy(Bytes((char*)getBytes(),byteCount()));
        writer.appendBlock((char*)getBytes(),byteCount());
    }
    return !writer.isError();
}
示例#2
0
bool PortCommand::write(ConnectionWriter& writer) {
    //ACE_DEBUG((LM_DEBUG,"PortCommand::writeBlock"));
    //ACE_OS::printf("Writing port command, text mode %d\n", writer.isTextMode());
    if (!writer.isTextMode()) {
        int len = 0;
        if (ch=='\0') {
            len = (int)str.length()+1;
        }
        yAssert(header.length()==8);
        char *base = header.get();
        Bytes b(base,4);
        NetType::netInt(len,b);
        base[4] = '~';
        base[5] = ch;
        base[6] = 0;
        base[7] = 1;
        writer.appendBlock(header.bytes().get(),header.bytes().length());
        if (ch=='\0') {
            writer.appendBlock(str.c_str(),str.length()+1);
        }
    } else {
        if (ch!='\0') {
            char buf[] = "X";
            buf[0] = ch;
            writer.appendString(ConstString(buf).c_str(),'\n');
        } else {
            writer.appendString(str.c_str(),'\n');
        }
    }
    return !writer.isError();
}
示例#3
0
    virtual bool read(ConnectionReader& reader) {
        YTRACE("NameServer::read start");
        ConstString ref = "NAME_SERVER ";
        bool ok = true;
        ConstString msg = "?";
        bool haveMessage = false;
        if (ok) {
            if (reader.isTextMode()) {
                msg = reader.expectText().c_str();
            } else {
                // migrate to binary mode support, eventually optimize
                Bottle b;
                b.read(reader);
                msg = b.toString().c_str();
            }
            haveMessage = (msg!="");
            msg = ref + msg;
        }
        if (reader.isActive()&&haveMessage) {
            YARP_DEBUG(Logger::get(),ConstString("name server got message ") + msg);
            size_t index = msg.find("NAME_SERVER");
            if (index==0) {
                Contact remote = reader.getRemoteContact();
                YARP_DEBUG(Logger::get(),
                           ConstString("name server receiving from ") +
                           remote.toURI());
                YARP_DEBUG(Logger::get(),
                           ConstString("name server request is ") + msg);
                ConstString result = server->apply(msg,remote);
                ConnectionWriter *os = reader.getWriter();
                if (os!=YARP_NULLPTR) {
                    if (result=="") {
                        result = ns_terminate(ConstString("unknown command ") +
                                              msg + "\n");
                    }
                    // This change is just to make Microsoft Telnet happy
                    ConstString tmp;
                    for (unsigned int i=0; i<result.length(); i++) {
                        if (result[i]=='\n') {
                            tmp += '\r';
                        }
                        tmp += result[i];
                    }
                    tmp += '\r';
                    os->appendString(tmp.c_str(),'\n');

                    YARP_DEBUG(Logger::get(),
                               ConstString("name server reply is ") + result);
                    ConstString resultSparse = result;
                    size_t end = resultSparse.find("\n*** end of message");
                    if (end!=ConstString::npos) {
                        resultSparse[end] = '\0';
                    }
                    YARP_INFO(Logger::get(),resultSparse);
                }
            } else {
                YARP_INFO(Logger::get(),
                          ConstString("Name server ignoring unknown command: ")+msg);
                ConnectionWriter *os = reader.getWriter();
                if (os!=YARP_NULLPTR) {
                    // this result is necessary for YARP1 support
                    os->appendString("???????????????????????????????????????",'\n');
                    //os->flush();
                    //os->close();
                }
            }
        }
        YTRACE("NameServer::read stop");
        return true;
    }