Exemplo n.º 1
0
    void testStandard() {
        report(0,"testing standard compliance...");

        // in theory, bottles should comply with a standard binary format
        // we check that here

        Bottle bot("10 20 30 40");
        //bot.setNested(true);
        //bot.specialize(bot.get(0).getCode());
        BufferedConnectionWriter writer;
        bot.write(writer);
        String s = writer.toString();
        checkEqual((int)s.length(),sizeof(NetInt32)*(1+1+(int)bot.size()),
                   "exact number of integers, plus type/count");

        Bottle bot2("[go] (10 20 30 40)");
        writer.clear();
        bot2.write(writer);
        s = writer.toString();
        // 1 for (outer) list code
        // 1 for list length
        // 1 for vocab code
        // 1 for vocab code value
        // 1 for (inner) list code
        // 1 for (inner) list length
        // 4 for integers in list
        checkEqual((int)s.length(),sizeof(NetInt32)*(10),
                   "nested example");
    }
Exemplo n.º 2
0
    void checkFormat() {
        report(0,"check matrix format conforms to network standard...");

        Matrix m;
        size_t rr = 10;
        size_t cc = 5;
        makeTestMatrix(m,rr,cc);

        BufferedConnectionWriter writer;
        m.write(writer);
        std::string s = writer.toString();
        Bottle bot;
        bot.fromBinary(s.c_str(),s.length());
        checkEqual((size_t) bot.get(0).asInt32(),rr,"row count matches");
        checkEqual((size_t) bot.get(1).asInt32(),cc,"column count matches");
        Bottle *lst = bot.get(2).asList();
        checkTrue(lst!=nullptr,"have data");
        if (!lst) return;
        checkEqual(lst->size(),(rr*cc),"data length matches");
        if (lst->size()!=(rr*cc)) return;
        bool ok = true;
        for (int i=0; i<(int)(rr*cc); i++) {
            double v = lst->get(i).asFloat64();
            if (fabs(v-i)>0.01) {
                ok = false;
                checkEqualish(v,i,"cell matches");
                break;
            }
        }
        checkTrue(ok,"data matches");
    }
Exemplo n.º 3
0
void BottleImpl::synch() {
    if (dirty) {
        if (!nested) {
            subCode();
            YMSG(("bottle code %d\n",StoreList::code + subCode()));
        }
        data.clear();
        BufferedConnectionWriter writer;
        if (!nested) {
            writer.appendInt(StoreList::code + speciality);
            YMSG(("wrote bottle code %d\n",StoreList::code + speciality));
        }
        YMSG(("bottle length %d\n",size()));
        writer.appendInt((int)size());
        for (unsigned int i=0; i<content.size(); i++) {
            Storable *s = content[i];
            if (speciality==0) {
                YMSG(("subcode %d\n",s->getCode()));
                writer.appendInt(s->getCode());
            } else {
                YMSG(("skipped subcode %d\n",s->getCode()));
                YARP_ASSERT(speciality==s->getCode());
            }
            if (s->isList()) {
                s->asList()->setNested(true);
            }
            s->writeRaw(writer);
        }
        String str = writer.toString();
        data.resize(str.length(),' ');
        memcpy(&data[0],str.c_str(),str.length());
        dirty = false;
    }
}
Exemplo n.º 4
0
 ConnectionReader& getReader()
 {
     writer.stopWrite();
     String s = writer.toString();
     sis.reset();
     sis.add(s);
     Route r;
     reader.reset(sis, NULL, r, s.length(), textMode);
     return reader;
 }
Exemplo n.º 5
0
 void checkStandard() {
     PortablePair<Bottle,Bottle> pp;
     pp.head.fromString("1 2 3");
     pp.body.fromString("yes no");
     BufferedConnectionWriter writer;
     pp.write(writer);
     std::string s = writer.toString();
     Bottle bot;
     bot.fromBinary(s.c_str(),s.length());
     checkEqual(bot.size(),(size_t) 2,"it is a pair");
     checkEqual(bot.get(0).asList()->size(),(size_t) 3,"head len is right");
     checkEqual(bot.get(1).asList()->size(),(size_t) 2,"body len is right");
 }
Exemplo n.º 6
0
 void testStandard() {
     report(0,"checking standard compliance of description...");
     ImageOf<PixelRgb> img;
     img.resize(8,4);
     img.zero();
     BufferedConnectionWriter writer;
     img.write(writer);
     ConstString s = writer.toString();
     Bottle bot;
     bot.fromBinary(s.c_str(),s.length());
     checkEqual(bot.size(),4,"plausible bottle out");
     checkEqual(bot.get(0).toString().c_str(),"mat","good tag");
     YARP_DEBUG(Logger::get(),"an example image:");
     YARP_DEBUG(Logger::get(),bot.toString().c_str());
 }
Exemplo n.º 7
0
bool StreamConnectionReader::convertTextMode() {
    if (isTextMode()) {
        if (!convertedTextMode) {
            Bottle bot;
            bot.read(*this);
            BufferedConnectionWriter writer;
            bot.write(writer);
            String s = writer.toString();
            altStream.reset(s);
            in = &altStream;
            convertedTextMode = true;
        }
    }

    return true;
}
Exemplo n.º 8
0
    void testBottle() {
        report(0,"trying to send a bottle across a fake stream");

        // set up a fake sender/receiver pair
        FakeTwoWayStream *fake1 = new FakeTwoWayStream();
        FakeTwoWayStream *fake2 = new FakeTwoWayStream();
        fake1->setTarget(fake2->getStringInputStream());
        fake2->setTarget(fake1->getStringInputStream());
        
        // hand streams over to protocol managers
        Protocol p1(fake1);
        Protocol p2(fake2);
        
        p1.open(Route("/out","/in","text"));
        
        checkEqual(fake1->getOutputText(),"CONNECT /out\r\n",
                   "text carrier header");
        
        p2.open("/in");
        
        checkEqual(fake2->getOutputText(),"Welcome /out\r\n",
                   "text carrier response");
        
        BufferedConnectionWriter writer;
        writer.appendLine("d");
        writer.appendLine("0 \"Hello\"");
        p1.write(writer);
        
        const char *expect = "CONNECT /out\r\nd\r\n0 \"Hello\"\r\n";
        checkEqual(fake1->getOutputText(),expect,
                   "added a bottle");
        
        ConnectionReader& reader = p2.beginRead();
        String str1 = reader.expectText().c_str();
        String str2 = reader.expectText().c_str();
        p2.endRead();
        
        checkEqual(str1,String("d"),"data tag");
        const char *expect2 = "0 \"Hello\"";
        checkEqual(str2,String(expect2),"bottle representation");
    }
Exemplo n.º 9
0
bool ConnectionWriter::writeToStream(PortWriter& portable, OutputStream& os) {
    BufferedConnectionWriter writer;
    if (!portable.write(writer)) return false;
    writer.write(os);
    return os.isOk();
}
Exemplo n.º 10
0
 void reset()
 {
     writer.reset(textMode);
 }
Exemplo n.º 11
0
 ConnectionWriter& getCleanWriter()
 {
     writer.reset(textMode);
     return writer;
 }
Exemplo n.º 12
0
 void setTextMode(bool textmode)
 {
     textMode = textmode;
     writer.reset(textMode);
     reader.tmode = textMode;
 }
Exemplo n.º 13
0
 BufferedConnectionWriter *getWriter() {
     altWriter->reset(tmode);
     return altWriter;
 }