maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   Development (https://talk.maemo.org/forumdisplay.php?f=13)
-   -   How to create Qt service? (https://talk.maemo.org/showthread.php?t=67039)

smandava 2010-12-15 20:34

How to create Qt service?
 
Hi,

My requirement is to create a Qt based backend service that runs when the device boots and call a function periodically (say every 15 minutes). Very similar to a cron tab.

So far I could find QDBus and QtMobility service framework modules that shows how to implement a service. It is not clear from the documentation weather these services can be run as daemons or the service die after serving the client request.

Can somebody provide a qt way of creating service that runs as daemon and doesn't hurt device battery? Any example projects or code snippets would be much appreciated.

Thanks,
Sateesh.

kif 2010-12-15 22:32

Re: How to create Qt service?
 
Hi

You can use QtService, you can download it from qt.nokia.com.

I'm using it in one of my projects, and it works great with QtMobility and QDBus, only problem I had was when trying to use anything from user space (calendar in my case), as this is a service, it does not have readily access to user space.

Kim

uvatbc 2010-12-15 23:53

Re: How to create Qt service?
 
Quote:

Originally Posted by smandava (Post 897611)
Hi,

My requirement is to create a Qt based backend service that runs when the device boots and call a function periodically (say every 15 minutes). Very similar to a cron tab.

So far I could find QDBus and QtMobility service framework modules that shows how to implement a service. It is not clear from the documentation weather these services can be run as daemons or the service die after serving the client request.

Can somebody provide a qt way of creating service that runs as daemon and doesn't hurt device battery? Any example projects or code snippets would be much appreciated.

Thanks,
Sateesh.

It depends what you want to achieve. A service is fundamentally an application that has no user interaction and thus no need for a UI.
It may be instructed to perform certain functionality by "poking" it using an IPC mechanism like DBus.

If you intend your service to be cross platform to Windows or Mac, then you should use QtService.
Installing and starting the service is heavily dependent on the target OS:
eg. Most Linux variants have upstart / init.d / event.d, Windows would require you to setup registry entries or invoke SCM.

smandava 2010-12-16 00:47

Re: How to create Qt service?
 
Thanks for the leads about QTSevice. If QTService doesn't work in user space, I am afraid I cant use it. I will look at the QTService api closely.

To clarify what exactly what I am trying to achieve, I have a qt application that can sync Google contacts with N900 device contacts. I am trying to set it up as a service so that the service can call the sync function every 15 minutes without user interaction. Cross platform functionality is not my primary concern at this time.

This service needs to run under the user space and be started every time the device is rebooted. I am not sure if init.d/event.d scripts always run as root or if they can be configured to run under user space.

I will have another UI client that will poke the service to modify the update frequency etc..

Thanks,
Sateesh.

uvatbc 2010-12-16 01:17

Re: How to create Qt service?
 
All of QT is user space. Do not even think of using it in kernel mode.

Also: I have a google contacts API parser ready in case you're interested.
I was supposed to work on a google contacts sync tool, but I've pushed it back until my other project is in better shape.

MohammadAG 2010-12-16 01:42

Re: How to create Qt service?
 
Why not just use a QTimer instance?

smandava 2010-12-16 01:49

Re: How to create Qt service?
 
Quote:

Originally Posted by uvatbc (Post 897788)
All of QT is user space. Do not even think of using it in kernel mode.

Also: I have a google contacts API parser ready in case you're interested.
I was supposed to work on a google contacts sync tool, but I've pushed it back until my other project is in better shape.

I guess what Kim mentioned above is QtMobilty Contacts Api opens the address book for the user who started the process. For instance QContactManager.contacts() calls gives different results for user account and developer account even tho its the same binary is making the call.

If the service is started (by init.d or upstart) as root user, the service will see the address book of root account instead of the intended default user account. Do you know if init.d or event.d scripts can start a process as default user?

I am noob to the Qt Programming. I would love to get the GData parser if you have available. Please PM me or post the link to this thread.

Thanks,
Sateesh.

smandava 2010-12-16 01:59

Re: How to create Qt service?
 
Quote:

Originally Posted by MohammadAG (Post 897796)
Why not just use a QTimer instance?

I am new to Qt and not sure if If this a common way to creating server in Qt. Not sure if the program consumes any resources (effecting the battery life) while sitting waiting for QTimer signal.

If this not CPU intensive, I will proceed with this option.

Thanks,
Sateesh.

javispedro 2010-12-16 02:13

Re: How to create Qt service?
 
There's a cron-like daemon running on the device:

http://wiki.maemo.org/Documentation/...larm_Framework

smandava 2010-12-16 02:32

Re: How to create Qt service?
 
Quote:

Originally Posted by javispedro (Post 897807)
There's a cron-like daemon running on the device:

http://wiki.maemo.org/Documentation/...larm_Framework

Great. I wish there is a Qt API for alarmd.

uvatbc 2010-12-16 02:35

Re: How to create Qt service?
 
Quote:

Originally Posted by smandava (Post 897800)
I am noob to the Qt Programming. I would love to get the GData parser if you have available.

This file has the code that logs into the contacts API and sends the requests to fetch the contacts - either all of them or from a specific date.
The function onGotContacts is the one which begins the parsing of the XML data returned by the Google Contacts API.

This file has the XML parser for the contacts data that you get from the previous file.

uvatbc 2010-12-16 02:41

Re: How to create Qt service?
 
Quote:

Originally Posted by smandava (Post 897813)
Great. I wish there is a Qt API for alarmd.

Almost all QT classes that might be long running have signals to indicate completion. So if you use signals intelligently, there should be no CPU usage by your app while "waiting" for a QTimer to fire.

tl;dr: Use a QTimer and quickly get to the more interesting bits :p

javispedro 2010-12-16 02:46

Re: How to create Qt service?
 
Quote:

Originally Posted by smandava (Post 897813)
Great. I wish there is a Qt API for alarmd.

You do not need it, the C API will work.

Note thought that it is Maemo-specific.

MohammadAG 2010-12-16 11:04

Re: How to create Qt service?
 
A QTimer of 1000ms didn't affect my CPU much, so I guess it's light on processing/battery power :P

kif 2010-12-16 21:11

Re: How to create Qt service?
 
Quote:

Originally Posted by smandava (Post 897800)
I guess what Kim mentioned above is QtMobilty Contacts Api opens the address book for the user who started the process. For instance QContactManager.contacts() calls gives different results for user account and developer account even tho its the same binary is making the call.

You are mostly right, but it was very late ;)

What I meant is that a daemon (QtService application) started from event.d (used in maemo) while booting will try to access for instance QContactManager.contacts() as root and not as user, resulting in many interesting errors.

The solution is to start the daemon as user (what I in the sleep deprived state I was in, called for user space), this is done by starting the daemon with the following command in event.d :

Code:

su -c '<path and name of your daemon>' -l user
After that everything should work as usual, with the exception of the missing GUI.

Kim

smandava 2010-12-17 04:11

Re: How to create Qt service?
 
Code:

su -c '<path and name of your daemon>' -l user
Thanks for the clarification and the event.d info.


All times are GMT. The time now is 15:12.

vBulletin® Version 3.8.8