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(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; }
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; }
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; }
///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(); }
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; }