⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 companion.cpp

📁 一个语言识别引擎
💻 CPP
📖 第 1 页 / 共 2 页
字号:

class CompanionCheckHelper : public Readable {
public:
    BottleImpl bot;
    bool got;
    virtual bool read(yarp::os::ConnectionReader& reader) {
        bot.read(reader);
        got = true;
        return true;
    }
    BottleImpl *get() {
        if (got) {
            return ⊥
        }
        return NULL;
    }
};
#endif /*DOXYGEN_SHOULD_SKIP_THIS*/


int Companion::cmdCheck(int argc, char *argv[]) {
    Logger& log = Logger::get();
    NameClient& nic = NameClient::getNameClient();

    YARP_INFO(log,"==================================================================");
    YARP_INFO(log,"=== Trying to register some ports");

    CompanionCheckHelper check;
    PortCore in;
    Address address = nic.registerName("...");
    bool faking = false;
    if (!address.isValid()) {
        YARP_INFO(log,"=== NO NAME SERVER!  Switching to local, fake mode");
        Network::setLocalMode(true);
        address = nic.registerName("...");
        faking = true;
    }
    in.listen(address);
    in.setReadHandler(check);
    in.start();
    PortCore out;
    Address address2 = nic.registerName("...");
    out.listen(address2);
    out.start();

    Time::delay(1);

    YARP_INFO(log,"==================================================================");
    YARP_INFO(log,"=== Trying to connect some ports");

    connect(out.getName().c_str(),in.getName().c_str());

    Time::delay(1);

    YARP_INFO(log,"==================================================================");
    YARP_INFO(log,"=== Trying to write some data");

    BottleImpl bot;
    bot.addInt(42);
    out.send(bot);

    Time::delay(1);

    YARP_INFO(log,"==================================================================");
    bool ok = false;
    for (int i=0; i<3; i++) {
        YARP_INFO(log,"=== Trying to read some data");
        Time::delay(1);
        if (check.get()!=NULL) {
            int x = check.get()->getInt(0);
            char buf[256];
            ACE_OS::sprintf(buf, "*** Read number %d", x);
            YARP_INFO(log,buf);
            if (x==42) {
                ok = true;
                break;
            }
        }
    }
    YARP_INFO(log,"==================================================================");
    YARP_INFO(log,"=== Trying to close some ports");
    in.close();
    out.close();
    Time::delay(1);
    if (!ok) {
        YARP_INFO(log,"*** YARP seems broken.");
        //diagnose();
        return 1;
    } else {
        if (faking) {
            YARP_INFO(log,"*** YARP seems okay, but there is no name server available.");
        } else {
            YARP_INFO(log,"*** YARP seems okay!");
        }
    }
    return 0;
}





int Companion::connect(const char *src, const char *dest, bool silent) {
    PortCommand pc('\0',slashify(dest));
    return sendMessage(src,pc,silent);
}

int Companion::disconnect(const char *src, const char *dest, bool silent) {
    PortCommand pc('\0',String("!")+dest);
    return sendMessage(src,pc,silent);
}

int Companion::disconnectInput(const char *src, const char *dest,
                               bool silent) {
    PortCommand pc('\0',String("~")+dest);
    return sendMessage(src,pc,silent);
}



#ifndef DOXYGEN_SHOULD_SKIP_THIS

// just a temporary implementation until real ports are available
class BottleReader : public Readable {
private:
    PortCore core;
    SemaphoreImpl done;
    bool raw;
    bool env;
public:
    BottleReader(const char *name, bool showEnvelope) : done(0) {
        NameClient& nic = NameClient::getNameClient();
        Address address = nic.registerName(name);
        raw = false;
        env = showEnvelope;
        core.setReadHandler(*this);
        if (address.isValid()) {
            ACE_OS::fprintf(stderr,"Port %s listening at %s\n", 
                            name,
                            address.toString().c_str());
            core.listen(address);
            core.start();
        } else {
            YARP_ERROR(Logger::get(),"could not create port");
            done.post();
        }
    }

    void wait() {
        done.wait();
    }

    void showEnvelope() {
        if (env) {
            Bottle envelope;
            core.getEnvelope(envelope);
            if (envelope.size()>0) {
                ACE_OS::printf("%s ", envelope.toString().c_str());
            }
        }
    }

    virtual bool read(ConnectionReader& reader) {
        BottleImpl bot;
        if (bot.read(reader)) {
            if (bot.size()==2 && bot.isInt(0) && bot.isString(1) && !raw) {
                int code = bot.getInt(0);
                if (code!=1) {
                    showEnvelope();
                    ACE_OS::printf("%s\n", bot.getString(1).c_str());
                    ACE_OS::fflush(stdout);
                }
                if (code==1) {
                    done.post();
                }
            } else {
                // raw = true; // don't make raw mode "sticky"
                showEnvelope();
                ACE_OS::printf("%s\n", bot.toString().c_str());
                ACE_OS::fflush(stdout);
            }
            return true;
        }
        return false;
    }
  
    void close() {
        core.close();
        core.join();
    }
};

#endif /*DOXYGEN_SHOULD_SKIP_THIS*/




int Companion::read(const char *name, const char *src, bool showEnvelope) {
    try {
        BottleReader reader(name,showEnvelope);
        if (src!=NULL) {
            Network::connect(src,name);
        }
        reader.wait();
        reader.close();
        return 0;
    } catch (IOException e) {
        ACE_OS::fprintf(stderr,"read failed: %s\n",e.toString().c_str());    
    }
    return 1;
}




int Companion::write(const char *name, int ntargets, char *targets[]) {
    try {
        PortCore core;
        NameClient& nic = NameClient::getNameClient();
        Address address = nic.registerName(name);
        if (address.isValid()) {
            ACE_OS::fprintf(stderr,"Port %s listening at %s\n", 
                            address.getRegName().c_str(),
                            address.toString().c_str());
            core.listen(address);
            core.start();
        } else {
            YARP_ERROR(Logger::get(),"could not create port");
            return 1;
        }

        bool raw = true;
        for (int i=0; i<ntargets; i++) {
            if (String(targets[i])=="verbatim") {
                raw = false;
            } else {
                connect(address.getRegName().c_str(),targets[i]);
            }
        }


        while (!feof(stdin)) {
            String txt = getStdin();
            if (!feof(stdin)) {
                if (txt[0]<32 && txt[0]!='\n' && 
                    txt[0]!='\r' && txt[0]!='\0') {
                    break;  // for example, horrible windows ^D
                }
                BottleImpl bot;
                if (!raw) {
                    bot.addInt(0);
                    bot.addString(txt.c_str());
                } else {
                    bot.fromString(txt.c_str());
                }
                core.send(bot);
            }
        }

        if (!raw) {
            BottleImpl bot;
            bot.addInt(1);
            bot.addString("<EOF>");
            core.send(bot);
        }

        core.close();
        core.join();

        return 0;
    } catch (IOException e) {
        ACE_OS::fprintf(stderr,"write failed: %s\n",e.toString().c_str());    
    }
    return 1;
}



int Companion::rpc(const char *connectionName, const char *targetName) {
    try {
        NameClient& nic = NameClient::getNameClient();
        Address address = nic.queryName(targetName);
        if (!address.isValid()) {
            YARP_ERROR(Logger::get(),"could not find port");
            return 1;
        }

        OutputProtocol *out = Carriers::connect(address);
        if (out==NULL) {
            throw IOException("cannot connect to port");
        }
        printf("RPC connection to %s at %s (connection name %s)\n", targetName, 
               address.toString().c_str(),
               connectionName);
        Route r(connectionName,targetName,"text_ack");
        out->open(r);
        OutputStream& os = out->getOutputStream();
        InputStream& is = out->getInputStream();
        StreamConnectionReader reader;

        while (!feof(stdin)) {
            String txt = getStdin();

            if (!feof(stdin)) {
                if (txt[0]<32 && txt[0]!='\n' && 
                    txt[0]!='\r' && txt[0]!='\0') {
                    break;  // for example, horrible windows ^D
                }
                Bottle bot;
                bot.fromString(txt.c_str());

                PortCommand pc(0,"d");
                BufferedConnectionWriter bw(out->isTextMode());
                bool ok = pc.write(bw);
                if (!ok) {
                    throw IOException("writer failed");
                }
                ok = bot.write(bw);
                if (!ok) {
                    throw IOException("writer failed");
                }
                bw.write(os);
                Bottle resp;
                reader.reset(is,NULL,r,0,true);
                resp.read(reader);
                if (String(resp.get(0).asString())=="<ACK>") {
                    printf("Acknowledged\n");
                } else {
                    printf("Response: %s\n", resp.toString().c_str());
                    resp.read(reader); // get rid of <ACK>
                }
            }
        }

        return 0;
    } catch (IOException e) {
        ACE_OS::fprintf(stderr,"write failed: %s\n",e.toString().c_str());    
    }
    return 0;
}


String Companion::readString(bool *eof) {
    bool end = false;

    String txt;

    if (!feof(stdin)) {
        txt = getStdin();
    }

    if (feof(stdin)) {
        end = true;
    } else if (txt[0]<32 && txt[0]!='\n' && 
               txt[0]!='\r' && txt[0]!='\0') {
        end = true;
    }
    if (end) {
        txt = "";
    }
    if (eof!=NULL) {
        *eof = end;
    }
    return txt;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -