void InvocationManager::Run() { for (;;) { Wait(); OpenHome::Net::Invocation* invocation = NULL; try { invocation = iWaitingInvocations.Read(); if (invocation->Interrupt()) { // the service associated with this invocation is being deleted // complete it with an error immediately and process the next waiting invocation->SetError(Error::eAsync, Error::eCodeInterrupted, Error::kDescriptionAsyncInterrupted); invocation->SignalCompleted(); } else { Invoker* invoker = iFreeInvokers.Read(); invoker->Invoke(invocation); } } catch (FifoReadError&) { if (invocation != NULL) { invocation->SetError(Error::eAsync, Error::eCodeShutdown, Error::kDescriptionAsyncShutdown); invocation->SignalCompleted(); } break; } } }
int main() { //invoker Invoker invoker; //command receiver--->the real object to execute command Light *light = new Light(); TV *tv = new TV(); //command LightCommand *lightCom = new LightCommand(); TVCommand *tvCom = new TVCommand(); lightCom->setReceiver(light); tvCom->setReceiver(tv); invoker.addCommand(lightCom); invoker.addCommand(tvCom); invoker.execute(); invoker.undo(); return 0; }
int LUAClient::Item(lua_State *pState) { // 1 => clients table // 2 => parameter if (!lua_isstring(pState, 2)) return 0; Invoker* pInvoker = Script::GetCollector<Invoker>(pState,1); if (!pInvoker) return 0; Client* pClient(NULL); UInt32 size = lua_objlen(pState, 2); const char* id = lua_tostring(pState, 2); if (size == ID_SIZE) pClient = pInvoker->clients(id); else if (size == (ID_SIZE * 2)) pClient = pInvoker->clients(Util::UnformatHex((UInt8*)id, size)); if (!pClient) { string name(id, size); pClient = pInvoker->clients(name); // try by name! } SCRIPT_BEGIN(pState) if (pClient) SCRIPT_ADD_OBJECT(Client, LUAClient,*pClient) SCRIPT_END return pClient ? 1 : 0; }
int main(int argc, char *argv[]) { Receiver *pRev = new Receiver(); Command *pCom = new ConcreteCommand(pRev); Invoker* pInv = new Invoker(pCom); pInv->Invoke(); return 0; }
int main(int argc, char* argv[]) { Reciever* rev = new Reciever(); Command* cmd = new ConcreteCommand(rev); Invoker* inv = new Invoker(cmd); inv->Invoke(); //printf("Hello World!\n"); return 0; }
int main() { Receiver *rec = new Receiver(); Command *cmd = new ConcreteCommand(rec); Invoker *inv = new Invoker(); inv->SetCommand(cmd); inv->RunCommand(); }
int main() { Invoker a; ConcreteCommand b, c, d; a.store(&b); a.store(&c); a.store(&d); a.execute(); return EXIT_SUCCESS; }
void test_command() { Receiver* rev = new Receiver(); ConcreteCommand* concmd = new ConcreteCommand(rev); Invoker* inv = new Invoker(concmd); inv->Invoke(); delete rev; delete concmd; delete inv; }
void LUAPublication::Delete(lua_State* pState, Publication& publication) { if (!publication.running()) return; lua_getmetatable(pState, -1); lua_getfield(pState, -1, "|invoker"); lua_replace(pState, -2); Invoker* pInvoker = (Invoker*)lua_touserdata(pState, -1); if (pInvoker) pInvoker->unpublish(publication.name()); lua_pop(pState, 1); }
int main(int argc, char* argv[]) { Receiver* pReceiver = new Receiver(); ConcreteCommand* pCommand = new ConcreteCommand(pReceiver); Invoker* pInvoker = new Invoker(pCommand); pInvoker->call(); delete pReceiver; delete pCommand; delete pInvoker; return 0; }
int main(){ Receiver *pReceiver = new Receiver(); Command *pCommand = new ConcreteCommand(pReceiver); Invoker *pInvoker = new Invoker(pCommand); pInvoker->Invoke(); SAFE_DELETE(pInvoker); SAFE_DELETE(pCommand); SAFE_DELETE(pReceiver); return 0; }
int main() { Receiver* pReceiver = new Receiver(); Command* pCommand = new ConcreateComand(pReceiver); Invoker* pInvoker = new Invoker(pCommand); pInvoker->Invoke(); delete pInvoker; system("pause"); return 0; }
int main() { Receiver *receiver = new Receiver(); ConcreteCommand *command = new ConcreteCommand(receiver); Invoker* invoker =new Invoker(); invoker->SetCommand(command); invoker->Notify(); delete receiver; delete command; delete invoker; // system("pause"); return 0; }
FlowStream::FlowStream(UInt64 id,const string& signature,Peer& peer,Invoker& invoker,BandWriter& band) : Flow(id,signature,_Name,peer,invoker,band),_pPublication(NULL),_state(IDLE),_numberLostFragments(0),_pListener(NULL) { PacketReader reader((const UInt8*)signature.c_str(),signature.length()); reader.next(4); _index = reader.read7BitValue(); Publications::Iterator it = invoker.publications(_index); if(it!=invoker.publications.end()) _pPublication = it->second; }
int LUAPublication::Close(lua_State *pState) { SCRIPT_CALLBACK(Publication, publication) lua_getmetatable(pState, 1); lua_getfield(pState, -1, "|invoker"); lua_replace(pState, -2); Script::DetachDestructor(pState,1); Invoker* pInvoker = (Invoker*)lua_touserdata(pState, -1); if (!pInvoker) { SCRIPT_BEGIN(pState) SCRIPT_ERROR("You have not the handle on publication ", publication.name(), ", you can't close it") SCRIPT_END } else if (publication.running()) pInvoker->unpublish(publication.name()); // call LUAPublication::Clear (because no destructor) lua_pop(pState, 1); SCRIPT_CALLBACK_RETURN }
int LUAGroup::Item(lua_State *pState) { // 1 => groups table // 2 => parameter if (!lua_isstring(pState, 2)) return 0; Invoker* pInvoker = Script::GetCollector<Invoker>(pState,1); if (!pInvoker) return 0; Group* pGroup(NULL); UInt32 size = lua_objlen(pState, 2); const UInt8* id = (const UInt8*)lua_tostring(pState, 2); if (size == ID_SIZE) pGroup = pInvoker->groups(id); else if (size == (ID_SIZE * 2)) pGroup = pInvoker->groups(Util::UnformatHex((UInt8*)id, size)); SCRIPT_BEGIN(pState) if (pGroup) SCRIPT_ADD_OBJECT(Group, LUAGroup, *pGroup) SCRIPT_END return pGroup ? 1 : 0; }
RTMFPFlow::RTMFPFlow(UInt64 id,const string& signature,Peer& peer,Invoker& invoker,BandWriter& band) : _poolBuffers(invoker.poolBuffers),_numberLostFragments(0),id(id),_stage(0),_completed(false),_pPacket(NULL),_pStream(NULL),_band(band) { RTMFPWriter* pWriter = new RTMFPWriter(peer.connected ? Writer::OPENED : Writer::OPENING,signature, band, _pWriter); if(signature.empty()) return; if (pWriter->flowId == 0) ((UInt64&)pWriter->flowId) = id; // create code prefix for a possible response // get flash stream process engine if(signature.size()>4 && signature.compare(0,5,"\x00\x54\x43\x04\x00",5)==0) (bool&)pWriter->critical = true; // NetConnection else if(signature.size()>3 && signature.compare(0,4,"\x00\x54\x43\x04",4)==0) invoker.flashStream(BinaryReader((const UInt8*)signature.c_str()+4,signature.length()-4).read7BitValue(),peer,_pStream); // NetStream else if(signature.size()>2 && signature.compare(0,3,"\x00\x47\x43",3)==0) (UInt64&)pWriter->flowId = id; // NetGroup if(!_pStream) _pStream.reset(new FlashMainStream(invoker,peer)); }
int main(int argc, char *argv[]) { Data data(10); AddCommand add(&data); SubCommand sub(&data); Invoker invoker; cout << "value = " << data.getValue() << endl; invoker.invoke(&add); cout << "value = " << data.getValue() << endl; invoker.invoke(&sub); cout << "value = " << data.getValue() << endl; invoker.undo(); cout << "value = " << data.getValue() << endl; invoker.undo(); invoker.undo(); invoker.undo(); cout << "value = " << data.getValue() << endl; return 0; }
void main() { IReceiver* reciever = new IReceiver(2); IncrementCommand* increCmd = new IncrementCommand(reciever); DecrementCommand* decreCmd = new DecrementCommand(reciever); SquareCommand* squreCmd = new SquareCommand(reciever); SqrtCommand* sqrtCmd = new SqrtCommand(reciever); Invoker* invoker = new Invoker(); invoker->addCmd(increCmd); invoker->addCmd(decreCmd); invoker->addCmd(squreCmd); invoker->addCmd(sqrtCmd); invoker->notify(); invoker->undo(); delete increCmd; delete decreCmd; delete squreCmd; delete sqrtCmd; delete invoker; delete reciever; }
bool Host::Invoke(IPC::Message* msg) { #pragma TODO("Unpack the invoker and interface name from the message data") const char* invokerName = NULL; const char* interfaceName = NULL; // find the interface Interface* interface = GetInterface(interfaceName); if (interface == NULL) { printf("RPC::Unable to find interface '%s'\n", interfaceName); delete msg; return true; } // find the invoker Invoker* invoker = interface->GetInvoker(invokerName); if (invoker == NULL) { printf("RPC::Unable to find invoker '%s' in interface '%s'\n", invokerName, interfaceName); delete msg; return true; } // get our frame from the top of the stack Frame* frame = m_Stack.Top(); HELIUM_ASSERT(frame->m_Message == NULL); frame->m_Message = msg; // call the function frame->m_MessageTaken = false; invoker->Invoke(msg->GetData(), msg->GetSize()); HELIUM_ASSERT(frame->m_Message != NULL); frame->m_Message = NULL; Args* args = (Args*)msg->GetData(); if (args->m_Flags & RPC::Flags::NonBlocking) { if (!frame->m_MessageTaken) { delete msg; } return true; // async call, we are done } // our reply IPC::Message* reply = NULL; // if we have data, and a reference args or payload if (msg->GetSize() > 0 && args->m_Flags & (RPC::Flags::ReplyWithArgs | RPC::Flags::ReplyWithPayload)) { // total size of reply uint32_t size = 0; // size of args section uint32_t argSize = invoker->GetArgsSize(); // size of payload section uint32_t payload_size = msg->GetSize() - argSize; // if we have a ref args if (args->m_Flags & RPC::Flags::ReplyWithArgs) { // alloc for args block size += argSize; } // if we have a ref payload if (args->m_Flags & RPC::Flags::ReplyWithPayload) { // alloc for payload block size += payload_size; } // create message reply = Create(invoker, size, msg->GetTransaction()); // where to write uint8_t* ptr = reply->GetData(); // if we have a ref args if (args->m_Flags & RPC::Flags::ReplyWithArgs) { if (Swizzle()) { invoker->Swizzle( msg->GetData() ); } // write to ptr memcpy(ptr, msg->GetData(), argSize); // incr ptr by amount written ptr += argSize; } // if we have a ref payload if (args->m_Flags & RPC::Flags::ReplyWithPayload) { // write to ptr memcpy(ptr, msg->GetData() + argSize, payload_size); // incr ptr by amount written ptr += payload_size; } // assert we did not overrun message size HELIUM_ASSERT((uint32_t)(ptr - reply->GetData()) == size); } else // no data, or no ref args or payload { // just create an empty reply, the other side is blocking reply = Create(invoker, 0, msg->GetTransaction()); } if (m_Connection->Send(reply)!= IPC::ConnectionStates::Active) { delete reply; } #ifdef RPC_DEBUG_MSG printf("RPC::Put message id 0x%08x, size %d, transaction %d\n", reply->GetID(), reply->GetSize(), reply->GetTransaction()); #endif if (!frame->m_MessageTaken) { delete msg; } return true; }
///Command 模式在实现的实现和思想都很简单,其关键就是将一个请求封装到一个类中 (Command),再提供处理对象(Receiver),最后 Command 命令由 Invoker 激活。另外,我 们可以将请求接收者的处理抽象出来作为参数传给 Command 对象,实际也就是回调的机制 (Callback)来实现这一点,也就是说将处理操作方法地址(在对象内部)通过参数传递给 Command 对象,Command 对象在适当的时候(Invoke 激活的时候)再调用该函数 ///Command 模式的思想非常简单,但是 Command 模式也十分常见,并且威力不小。实 际上,Command 模式关键就是提供一个抽象的 Command 类,并将执行操作封装到 Command 类接口中,Command 类中一般就是只是一些接口的集合,并不包含任何的数据属性 void CommandTest() { Reciever* rev = new Reciever(); Command* cmd = new ConcreteCommand(rev); Invoker* inv = new Invoker(cmd); inv->Invoke(); }