'어플리케이션 간 통신'에 해당되는 글 2건

  1. 2014.01.13 양방향 메세지 포트 통신 처리(Handling Bi-directional Message Port Communication)

Tizen Native 어플리케이션에서는 양방향 메세지 포트 통신(Bi-directional Message Port Communication)을 이용하여 어플리케이션간에 메세지를 보내고 받을 수 있다. 


1. 서버 어플리케이션에서는 Tizen::Io::MessagePortManager클래스의 RequestLocalMessagePort()를 이용하여 Tizen::Io::LocalMessagePort클래스의 인스턴스를 가져온다. AddMessagePortListener() 메소드는 클라이언트로부터 메세지를 받는데 사용된다.

LocalMessagePort* __pLocalPort;
__pLocalPort = MessagePortManager::RequestLocalMessagePort(L"PortA");

__pLocalPort->AddMessagePortListener(*this);


2. OnMessageReceivedN() 이벤트 핸들러는 클라이언트 어플리케이션으로 부터 전달받은 메세지를 처리하고, 클라이언트로 부터 전달받은 

Tizen::Io::RemoteMessagePort클래스 인스턴스를 이용하여 응답한다.

void
OnMessageReceivedN(RemoteMessagePort* pRemoteMessagePort, IMap* pMessage)
{
   // Handle received messages

   String* pValue = static_cast< String* >(pMessage->GetValue(String(L"Request")));

   // Send response messages
   if (*pValue == L"Friend")
   {
      HashMap* pMap = new HashMap(SingleObjectDeleter);
      pMap->Construct();
      pMap->Add(new String(L"Reply"), new String(L"Kim"));

      pRemoteMessagePort->SendMessage(__pLocalPort, pMap);
      delete pMap;
   }

   // Delete messages
   delete pMessage;
}


3. 클라이언트 어플리케이션에서는 Tizen::Io::MessagePortManager클래스를 이용하여 Tizen::Io::LocalMessagePort클래스와 

Tizen::Io::RemoteMessagePort클래스 인스턴스를 가져온다 

LocalMessagePort* pLocalPort;
RemoteMessagePort* pRemotePort;

pLocalPort = MessagePortManager::RequestLocalMessagePort(L"PortB");
pLocalPort->AddMessagePortListener(*this);

pRemotePort = MessagePortManager::RequestRemoteMessagePort(L"123456789.ServerApp", L"PortA");


4. Tizen::Io::RemoteMessagePort클래스의 SendMessage() 메소드를 이용하여 클라이언트 어플리케이션에서 서버 어플리케이션으로 메세지를 전송한다.

void
SendMessage(void)
{
   HashMap* pMap = new HashMap(SingleObjectDeleter);
   pMap->Construct();
   pMap->Add(new String(L"Request"), new String(L"Friend"));

   pRemotePort->SendMessage(__pLocalPort, pMap);
}


5. OnMessageReceivedN() 이벤트 핸들러는 서버 어플리케이션으로부터 메세지를 전달받는데 사용된다.

void
OnMessageReceivedN(RemoteMessagePort* pRemoteMessagePort, IMap* pMessage)
{
   // Response messages received 

   String* pValue = static_cast< String* >(pMessage->GetValue(String(L"Reply")));
   AppLog("My friend's name is %ls", pValue->GetPointer());
   delete pMessage;
}

























Posted by No names
,