LunaService

Example client usage:

bool retVal;
LSError lserror;
LSErrorInit(&lserror);

LSHandle *serviceHandle;
retVal = LSRegister(NULL, &serviceHandle, &lserror);
if (!retVal) goto error;

retVal = LSCall(serviceHandle, "luna://com.palm.contacts/category/listContacts",
        "{ \"json payload\" }", listContactsHandler, user_data, &token, &lserror);
if (!retVal) goto error;

LSGmainAttach(serviceHandle, gmainLoop, &lserror); 
g_main_loop_run(gmainLoop);

Example service usage.

// callback
static bool
listContacts(LSHandle *sh, LSMessage *message)
{
     LSMessage *reply = NULL;

     bool retVal;
     LSError lserror;
     LSErrorInit(&lserror);

     retVal = LSMessageReply(sh, message, "{ JSON REPLY PAYLOAD }", &lserror);
     if (!retVal)
     {
         LSErrorPrint(&lserror, stderr);
         LSErrorFree(&lserror);
     }

     return retVal;
}

static LSMethod ipcMethods[] = {
   { "listContacts", listContacts },
   { },
};
...

// Service registration thread
bool retVal;
LSError lserror;
LSErrorInit(&lserror);

LSHandle *serviceHandle;
retVal = LSRegister("com.palm.contacts", &serviceHandle, &lserror);
if (!retVal) goto error;

retVal = LSRegisterCategory(serviceHandle, "/category",  ipcMethods, NULL, NULL, &lserror);
if (!retVal) goto error;

retVal = LSGmainAttach(serviceHandle, gmainLoop, &lserror); 
if (!retVal) goto error;

g_main_loop_run(gmainLoop);

Storing a message for replying in another thread.

Queue messageQueue;
...

static bool
listContacts(LSHandle *sh, LSMessage *message)
{
     bool retVal;

     LSError lserror;
     LSErrorInit(&lserror);

     LSMessageRef(message);

     queue(messageQueue, message);
}

...

void
SomeOtherThread()
{
    LSError lserror;
    LSErrorInit(&lserror);

    LSMessage *message = dequeue(messageQueue);
    ...
    if (!LSMessageReply(sh, message, "{PAYLOAD IN JSON}", lserror))
    {
        LSErrorPrint(&lserror);
        LSErrorFree(&lserror);
    }

    ....
}

Example run loop via select. See LSCustomSelectExample() for latest example.

bool retVal;

do
{
    int nfd = -1;
    fd_set rdfds, wrfds, exfds;

    FD_ZERO(&rdfds);
    FD_ZERO(&wrfds);
    FD_ZERO(&exfds);

    retVal = LSCustomGetFds(sh, &nfd, &rdfds, &wrfds, &exfds, lserror);
    if (!retVal) return -1;

    int ret = select(nfd, &rdfds, &wrfds, &exfds, NULL);
    if (ret < 0) 
    {
        perror("select");
        break;
    }

    // Pull incoming bytes off socket and push outgoing bytes onto it.
    retVal = LSCustomSendRecvBytes(sh, &rdfds, &wrfds, &exfds, lserror);
    if (!retVal)
    {
        break;
    }

    // Transmit byte and Dispatch incomming at most 1 message
    retVal = LSCustomDispatchMessage(sh, NULL, lserror); 
    if (!retVal)
    {
        break;
    }

} while (true);

Example run loop via select if you want to handle messages directly.

while (serviceRunning)
{
    fd_set rdfds, wrfds, exfds;
    FD_ZERO(&rdfds);
    FD_ZERO(&wrfds);
    FD_ZERO(&exfds);

    LSGetFd(serviceHandle, &maxfd, &rdfds, &wrfds, &exfds, &lserror);

    ret = select(maxfd, &rdfds, &wrfds, &exfds, NULL);
    if (ret > 0)
    {
        LSMessage *message;
        char *reply = NULL;

        LSMessageFetch(serviceHandle, &message, &lserror);

        if (strcmp(LSMessageGetName(message), "listContacts"))
        {
            char *payload;
            payload = LSMessageGetPayload(message);
            ...
            reply = "{ JSON PAYLOAD }";
            LSMessageReply(serviceHandle, message, reply, &lserror);
        }
    }
}

Generated on Wed Sep 16 12:26:46 2009 by  doxygen 1.5.5