Add a retry logic in IPC
authorSunwook Bae <sunwook45.bae@samsung.com>
Tue, 14 May 2013 01:53:31 +0000 (10:53 +0900)
committerSunwook Bae <sunwook45.bae@samsung.com>
Tue, 14 May 2013 07:35:39 +0000 (16:35 +0900)
Change-Id: Icc8faa277438588180f99ec49ce98205f202f87c
Signed-off-by: Sunwook Bae <sunwook45.bae@samsung.com>
src/io/FIo_IpcClient.cpp
src/server/inc/FIo_IpcServer.h
src/server/io/FIo_IpcServer.cpp

index 1fd53cd..2e389cb 100644 (file)
@@ -134,8 +134,7 @@ struct HelloMessage
 {
        int pid;
        bool reverse;
-       char pkgId[256];
-       char appExecutableName[256];
+       char appId[256];
 };
 
 result
@@ -176,30 +175,17 @@ _IpcClient::MakeConnection(bool forReverse)
 
        if (__fdCount == 0)
        {
-               // Set an pkgId
-               String pkgId = _AppInfo::GetPackageId();
-               int length = (pkgId.GetLength() + 1) * sizeof(wchar_t);
+               // Set an appId
+               String appId = _AppInfo::GetApplicationId();
+               int length = (appId.GetLength() + 1) * sizeof(wchar_t);
                if (length > 255)
                {
                        length = 255;
                }
 
-               SysTryReturnResult(NID_IO, pkgId.GetLength() > 0, E_SYSTEM, "AppId dose not exist.");
+               SysTryReturnResult(NID_IO, appId.GetLength() > 0, E_SYSTEM, "AppId dose not exist.");
 
-               memcpy(helloMessage.pkgId, pkgId.GetPointer(), length);
-
-               // Set an executableName
-               String appExecutableName = _AppInfo::GetAppExecutableName();
-               length = (appExecutableName.GetLength() + 1) * sizeof(wchar_t);
-               if (length > 255)
-               {
-                       length = 255;
-               }
-
-               if (appExecutableName.GetLength() != 0)
-               {
-                       memcpy(helloMessage.appExecutableName, appExecutableName.GetPointer(), length);
-               }
+               memcpy(helloMessage.appId, appId.GetPointer(), length);
        }
 
        client = socket(AF_UNIX, SOCK_STREAM, 0);
@@ -215,8 +201,26 @@ _IpcClient::MakeConnection(bool forReverse)
        strncpy(server.sun_path, socketName.c_str(), socketNameLength);
        serverLen = sizeof(server);
 
-       ret = connect(client, (struct sockaddr*) &server, serverLen);
-       if (ret != 0)
+       // Retry if the server is not ready
+       int retry = 5;
+       while (retry > 0)
+       {
+               ret = connect(client, (struct sockaddr*) &server, serverLen);
+               if (ret < 0 && errno == ENOENT)
+               {
+                       SysLog(NID_IO, "The server is not ready. %d", retry);
+
+                       usleep(1000 * 1000);
+
+                       --retry;
+               }
+               else
+               {
+                       break;
+               }
+       }
+
+       if (ret < 0)
        {
                SysTryCatch(NID_IO, errno == EINPROGRESS, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to connect to server(%s) : %s",
                                   socketName.c_str(), strerror(errno));
@@ -512,8 +516,6 @@ _IpcClient::SendSync(IPC::Message* pMessage)
 
        std::string message;
 
-       struct pollfd pfd;
-
        IPC::Message* pReply = null;
        MessageReplyDeserializer* pReplyDeserializer = null;
        IPC::SyncMessage* pSyncMessage = dynamic_cast <IPC::SyncMessage*>(pMessage);
@@ -530,11 +532,21 @@ _IpcClient::SendSync(IPC::Message* pMessage)
        while (remain > 0)
        {
                written = write(fd, (char*) pData, remain);
+               if (written < 0)
+               {
+                       SysLogException(NID_IO, E_SYSTEM, "[E_SYSTEM] Failed to send a request: %d, %s", errno, strerror(errno));
+
+                       ReleaseFd(fd);
+                       return E_SYSTEM;
+               }
+
                remain -= written;
                pData += written;
        }
 
        // Wait reply
+       struct pollfd pfd;
+
        pfd.fd = fd;
        pfd.events = POLLIN | POLLRDHUP;
        pfd.revents = 0;
@@ -558,6 +570,8 @@ _IpcClient::SendSync(IPC::Message* pMessage)
                if (pfd.revents & POLLRDHUP)
                {
                        SysLogException(NID_IO, E_SYSTEM, "[E_SYSTEM] POLLRDHUP");
+
+                       ReleaseFd(fd);
                        return E_SYSTEM;
                }
 
index 2c30c10..bd90760 100644 (file)
@@ -183,8 +183,7 @@ private:
                _IpcServer* pIpcServer;                    /**< the pointer to an _ IpcServer */
                GIOChannel* pReverseChannel;               /**< the channel for sending reverse message */
                std::vector <struct _ChannelInfo*> channels;   /**< the set of channels associated with a client */
-               Tizen::Base::String pkgId;
-               Tizen::Base::String appExecutableName;
+               Tizen::Base::String appId;
        };
 
        Tizen::Base::String __name;
index d0453c0..62b8476 100644 (file)
@@ -277,8 +277,7 @@ struct HelloMessage
 {
        int pid;
        bool reverse;  // true if the connection is for reverse message
-       char pkgId[256];
-       char appExecutableName[256];
+       char appId[256];
 };
 
 gboolean
@@ -306,8 +305,7 @@ _IpcServer::OnConnectionRequest(GIOChannel* source, GIOCondition condition, gpoi
        SysTryCatch(NID_IO, client != -1, , E_SYSTEM, "[E_SYSTEM] Accept failed.");
 
        read(client, &helloMessage, sizeof(helloMessage));
-       helloMessage.pkgId[255] = '\0';
-       helloMessage.appExecutableName[255] = '\0';
+       helloMessage.appId[255] = '\0';
 
        pChannel = g_io_channel_unix_new(client);
        SysTryCatch(NID_IO, pChannel != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Not enough memory.");
@@ -326,8 +324,7 @@ _IpcServer::OnConnectionRequest(GIOChannel* source, GIOCondition condition, gpoi
 
                pClientInfo->pIpcServer = pIpcServer;
                pClientInfo->clientId = helloMessage.pid;
-               pClientInfo->pkgId.Append((wchar_t*) helloMessage.pkgId);
-               pClientInfo->appExecutableName.Append((wchar_t*) helloMessage.appExecutableName);
+               pClientInfo->appId.Append((wchar_t*) helloMessage.appId);
                pClientInfo->pReverseChannel = null;
 
                pIpcServer->__clients[helloMessage.pid] = pClientInfo;
@@ -391,7 +388,10 @@ _IpcServer::GetClientPackageId(void) const
 
        if (__pCurrentClientInfo)
        {
-               return __pCurrentClientInfo->pkgId;
+               String pkgId;
+               __pCurrentClientInfo->appId.SubString(0, 10, pkgId);
+
+               return pkgId;
        }
 
        return nullString;
@@ -404,11 +404,7 @@ _IpcServer::GetClientApplicationId(void) const
 
        if (__pCurrentClientInfo)
        {
-               String appId(__pCurrentClientInfo->pkgId);
-               appId.Append(L'.');
-               appId.Append(__pCurrentClientInfo->appExecutableName);
-
-               return appId;
+               return __pCurrentClientInfo->appId;
        }
 
        return nullString;