bool ofxOscReceiver::getNextMessage( ofxOscMessage* message ) { // grab a lock on the queue grabMutex(); // check if there are any to be got if ( messages.size() == 0 ) { // no: release the mutex releaseMutex(); return false; } // copy the message from the queue to message ofxOscMessage* src_message = messages.front(); message->copy( *src_message ); // now delete the src message delete src_message; // and remove it from the queue messages.pop_front(); // release the lock on the queue releaseMutex(); // return success return true; }
ExecProcess *AgentThread::addProcess(ExecProcess *process) { grabMutex(); processPool[processCount] = process; processCount++; freeMutex(); return processPool[processCount-1]; };
void ofxOscReceiver::ProcessMessage( const osc::ReceivedMessage &m, const IpEndpointName& remoteEndpoint ) { // convert the message to an ofxOscMessage ofxOscMessage* ofMessage = new ofxOscMessage(); // set the address ofMessage->setAddress( m.AddressPattern() ); // set the sender ip/host char endpoint_host[ IpEndpointName::ADDRESS_STRING_LENGTH ]; remoteEndpoint.AddressAsString( endpoint_host ); ofMessage->setRemoteEndpoint( endpoint_host, remoteEndpoint.port ); // transfer the arguments for ( osc::ReceivedMessage::const_iterator arg = m.ArgumentsBegin(); arg != m.ArgumentsEnd(); ++arg ) { if ( arg->IsInt32() ) ofMessage->addIntArg( arg->AsInt32Unchecked() ); else if ( arg->IsFloat() ) ofMessage->addFloatArg( arg->AsFloatUnchecked() ); else if ( arg->IsString() ) ofMessage->addStringArg( arg->AsStringUnchecked() ); else if ( arg->IsBlob() ) { osc::Blob blob; arg->AsBlobUnchecked( blob.data, blob.size ); ofMessage->addBlobArg( blob ); } else { assert( false && "message argument is not int, float, string, or blob" ); } } // now add to the queue // at this point we are running inside the thread created by startThread, // so anyone who calls hasWaitingMessages() or getNextMessage() is coming // from a different thread // so we have to practise shared memory management // grab a lock on the queue grabMutex(); // add incoming message on to the queue messages.push_back( ofMessage ); // release the lock releaseMutex(); }
bool ofxOscReceiver::hasWaitingMessages() { // grab a lock on the queue grabMutex(); // check the length of the queue int queue_length = (int)messages.size(); // release the lock releaseMutex(); // return whether we have any messages return queue_length > 0; }
void AgentThread::delProcess(int i) { grabMutex(); ExecProcess *processToDelete = getProcess(i); for (int j = i; j < getProcessCount(); j++) processPool[j] = processPool[j+1]; processPool[processCount] = NULL; processCount--; // remove from our list of test objects if (processToDelete) { ConsoleServer::debugMsg(1,"Garbage collecting the removed process resources for process %d\n",i); delete processToDelete; processToDelete = NULL; } freeMutex(); };