Пример #1
0
bool DeviceResponder::read(ConnectionReader& connection) {
    Bottle cmd, response;
    if (!cmd.read(connection)) { return false; }
    //printf("command received: %s\n", cmd.toString().c_str());
    respond(cmd,response);
    if (response.size()>=1) {
        ConnectionWriter *writer = connection.getWriter();
        if (writer!=NULL) {
            if (response.get(0).toString()=="many"&&writer->isTextMode()) {
                for (int i=1; i<response.size(); i++) {
                    Value& v = response.get(i);
                    if (v.isList()) {
                        v.asList()->write(*writer);
                    } else {
                        Bottle b;
                        b.add(v);
                        b.write(*writer);
                    }
                }
            } else {
                response.write(*writer);
            }

            //printf("response sent: %s\n", response.toString().c_str());
        }
    } else {
        ConnectionWriter *writer = connection.getWriter();
        if (writer!=NULL) {
            response.clear();
            response.addVocab(Vocab::encode("nak"));
            response.write(*writer);
        }
    }
    return true;
}
Пример #2
0
bool RFModuleHelper::read(ConnectionReader& connection) {
    Bottle cmd, response;
    if (!cmd.read(connection)) { return false; }
    printf("command received: %s\n", cmd.toString().c_str());

    bool result = owner.safeRespond(cmd,response);
    if (response.size()>=1) {
        ConnectionWriter *writer = connection.getWriter();
        if (writer!=0) {
            if (response.get(0).toString()=="many" && writer->isTextMode()) {
                for (int i=1; i<response.size(); i++) {
                    Value& v = response.get(i);
                    if (v.isList()) {
                        v.asList()->write(*writer);
                    } else {
                        Bottle b;
                        b.add(v);
                        b.write(*writer);
                    }
                }
            } else {
                response.write(*writer);
            }

            //printf("response sent: %s\n", response.toString().c_str());
        }
    }
    return result;
}
Пример #3
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();
}
Пример #4
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();
}