Go to Content



Library

Home > Library

Inter Application Communication in bada

By bada Master | 5753 views | Aug 18, 2011

Inter-Application Communication (IAC) is a system whereby one application can send a message to another, either to exchange information or to make something happen.

It is possible for applications to pass/extract data object to/from other applications. Channel enables an application to send/receive data to/from other application.

Related article

SDK Version

  • 2.0

In bada, channel communication is made possible via ClientChannel and ServerChannel.

  • Sending requests to other bada applications using the Osp::Io::ClientChannel class
  • Sending responses to other bada applications using the Osp::Io::ServerChannel class

 

 

ClientChannel:

An application can send request to another using the ClientChannel class. This class provides methods to send a request to another application and the channel response listener methods to receive a response for the request sent.

ServerChannel:

An application can receive a request from another using the ServerChannel class. This class provides channel listener methods to receive a request from another application and SendResponse methods to send the response to a client application.

This tutorial covers the mechanism for performing Inter-App Communication. It comes with two apps. A client app that makes the request and a server app that processes the request and sends a response back.

Step 1: Sending a Request:

1. Create and initialize the required class instances needed for the application.

2. Set the Osp::Io::IChannelResponseEventListener interface using the SetChannelResponseEventListener() method:

result
ClientForm::OnInitializing(void)
{
	result r = E_SUCCESS;

	// Sets channel response event listener.
Osp::Io::ClientChannel::GetInstance()->SetChannelResponseEventListener(this);
...
}

3. Send a request to the other bada application using the SendRequest() method.
In the following code snippet, a request is sent for receiving the current system date &time on pressing of respective button "GetSystemDate&Time".

case ID_BUTTON_GETDATE:
	{
		Osp::App::AppId   app = "qik37po9ck"; /*This is the servers App ID*/
		String* pData = new String(L"GetDate");

		//populate the arraylist with input arguments and send the request.
		pList->Construct();
		pList->Add(*pData);
		RequestId reqId = 0;
		r = ClientChannel::GetInstance()->SendRequest(app,  pList, reqId);
...
}

4. Receive the response in the OnChannelResponseReceivedN() event handler:

void
ClientForm::OnChannelResponseReceivedN(RequestId reqId, Osp::Io::ClientChannel& clientChannel, const Osp::App::AppId& serverAppId, Osp::Base::Collection::IList* pArgs)
{
//when response is received, this method is called. 

5. Handle the channel response and populate the result to the output display screen.

  text = ((String*)(pArgs->GetAt(0)))->GetPointer();
	__pLabel->SetText(L"Response received for req id: " + str1 + text);
	RequestRedraw();

	// clean up the arguments
	pArgs->RemoveAll(true);
	delete pArgs;
Step 2: Sending a Response:

1. Create and initialize the required class instances needed for the application.

2. Set the Osp::Io::IChannelRequestEventListener interface using the SetChannelRequestEventListener() method:

result
ServerForm::OnInitializing(void)
{
	result r = E_SUCCESS;

	// Sets channel response event listener.
	Osp::Io::ServerChannel::GetInstance()->SetChannelRequestEventListener(this);

3. Receive the request in the OnChannelRequestReceivedN() event handler:

void
ServerForm::OnChannelRequestReceivedN(RequestId reqId, Osp::Io::ServerChannel& serverChannel, const Osp::App::AppId& clientAppId, Osp::Base::Collection::IList* pArgs)
{
// when a request is received, this method is called.

4. Send the response using the SendResponse() method.
In the following code snippet, the system time retrieved is sent.

ArrayList* pList = new ArrayList();

	String text;
	text =((String*)(pArgs->GetAt(0)))->GetPointer();

	//Check the request and respond accordingly
	if(text.CompareTo(L"GetDate") == 0)
	{
		Osp::Base::DateTime CurrDateTime;

		SystemTime::GetCurrentTime(WALL_TIME, CurrDateTime);
		String str(CurrDateTime.ToString());

		AppLog("current time = %S", str.GetPointer());

		String* pData = new String(str);

		pList->Construct();
		pList->Add(*pData);
		//Sends the response to the client.
		r = serverChannel.SendResponse(clientAppId, reqId, pList);

Below are the screenshots of the application output.

 

Image 1: Client and Server application, request and response.
Image 1: Client and Server application, request and response.

 

Did this document help you? Yes | It's good,but ... | Not Helpful