Example #1
0
    bool read(PortReader& reader, bool willReply = false) {
        // called by user

        // user claimed they would reply to last read, but then
        // decided not to.
        if (replyDue) {
            Bottle emptyMessage;
            reply(emptyMessage,false,false);
            replyDue = false;
            dropDue = false;
        }
        if (willReply) {
            replyDue = true;
        }

        stateMutex.wait();
        readActive = true;
        readDelegate = &reader;
        writeDelegate = NULL;
        this->willReply = willReply;
        consume.post(); // happy consumer
        stateMutex.post();

        produce.wait();
        stateMutex.wait();
        if (!readBackground) {
            readDelegate = NULL;
        }
        stateMutex.post();
        bool result = readResult;
        return result;
    }
Example #2
0
 virtual ~Private()
 {
     Port *closePort = nullptr;
     stateSema.wait();
     if (port!=nullptr) {
         closePort = port;
     }
     stateSema.post();
     if (closePort!=nullptr) {
         closePort->close();
     }
     stateSema.wait();
     clear();
     //stateSema.post();  // never give back mutex
 }
Example #3
0
 void configReader(PortReader& reader) {
     stateMutex.wait();
     readActive = true;
     readBackground = true;
     readDelegate = &reader;
     consume.post(); // just do this once
     stateMutex.post();
 }
Example #4
0
 void finishReading() {
     if (!readBackground) {
         stateMutex.wait();
         closed = true;
         consume.post();
         consume.post();
         stateMutex.post();
     }
 }
Example #5
0
 bool reply(PortWriter& writer, bool drop, bool /*interrupted*/) {
     // send reply even if interrupt has happened in interim
     if (!replyDue) return false;
     replyDue = false;
     dropDue = drop;
     writeDelegate = &writer;
     consume.post();
     produce.wait();
     bool result = readResult;
     return result;
 }
Example #6
0
    virtual bool read(ConnectionReader& reader) {
        // called by comms code
        readBlock.wait();

        if (!reader.isValid()) {
            // termination
            stateMutex.wait();
            if (readDelegate!=NULL) {
                readResult = readDelegate->read(reader);
            }
            stateMutex.post();
            produce.post();
            readBlock.post();
            return false;
        }

        // wait for happy consumer - don't want to miss a packet
        if (!readBackground) {
            consume.wait();
        }

        if (closed) {
            YARP_DEBUG(Logger::get(),"Port::read shutting down");
            readBlock.post();
            return false;
        }

        stateMutex.wait();
        readResult = false;
        if (readDelegate!=NULL) {
            readResult = readDelegate->read(reader);
        } else {
            // read and ignore
            YARP_DEBUG(Logger::get(),"data received in Port, no reader for it");
            Bottle b;
            b.read(reader);
        }
        if (!readBackground) {
            readDelegate = NULL;
            writeDelegate = NULL;
        }
        bool result = readResult;
        stateMutex.post();
        if (!readBackground) {
            produce.post();
        }
        if (result&&willReply) {
            consume.wait();
            if (closed) {
                YARP_DEBUG(Logger::get(),"Port::read shutting down");
                readBlock.post();
                return false;
            }
            if (writeDelegate!=NULL) {
                stateMutex.wait();
                ConnectionWriter *writer = reader.getWriter();
                if (writer!=NULL) {
                    result = readResult = writeDelegate->write(*writer);
                }
                stateMutex.post();
            }
            if (dropDue) {
                reader.requestDrop();
            }
            produce.post();
        }
        readBlock.post();
        return result;
    }
Example #7
0
 void openable() {
     stateMutex.wait();
     closed = false;
     opened = true;
     stateMutex.post();
 }
Example #8
0
 void promiseType(const Type& typ) {
     stateMutex.wait();
     this->typ = typ;
     stateMutex.post();
 }
Example #9
0
 Type getType() {
     stateMutex.wait();
     Type t = typ;
     stateMutex.post();
     return t;
 }