views:

130

answers:

1

Hi,

I'm working on a application manager for embeded platform based on the CVM PhoneME VM. The VM is started by a C++ app which configures the CVM and then triggers the VM itself. This C++ app is called form the command line passing the main class name and the classpath of a java application. There is a main java app (lets call it Manager) which loads the app using classloaders. I want this manager to be a single instance application so it could track all running apps.

In other words: The first time I start an app (app1 for instance), the VM will launch and the Manager will load the app1. In further calls to load other apps (app2, app3 and so on), the same instance of the Manager would load those apps.

The manager is working fine, except for the fact that this is not a single instance.

Is it possible to do what I want?

I found this: http://www.knowledgesutra.com/forums/topic/59760-how-to-implement-single-instance-application-on-java/

This is almost the same I want, except for the app loading part. However, the necessary packages are not available in the CVM implementation.

Thanks very much.

+1  A: 

There are various ways to enforce a single instance of an application. Firefox uses a lock file, for example.

One of the most common ways I've seen is to launch the "manager" regardless, but at launch, try to open a ServerSocket on some port listening for connections.

If you fail to open the socket, you know you're not the singleton instance. So, instead of launching the app, connect to the listening socket and send it the program arguments instead.

If you succeed in opening the socket, do what was asked to launch app1 while on another thread listen for incoming connections from those managers launched as duplicate instances.

Edit: This describes the process in a non-ME environment. Not being an ME developer, I can't comment on its feasibility on a phone.

Mark Peters
Thanks for your answer.This solution looks fine. I'll manage to try it.Do you think it is possible to do a JNI call from the C++ to the running instance of the app in the JVM? I believe it would solve the problem so well, however I'm not that used to JNI.Thanks.
Marcus
@Marcus: Others can comment on that; I've never had the opportunity to use JNI to date. But my general understanding of it is that JNI calls go the other direction: so Java can call native code; not typically the other way around.
Mark Peters
It is possible to make JNI calls from C++ to Java. I studied that in the last few days. The major problem is that it is impossible to attach a different proccess to the previously created VM (at least I couldn't find a way. Do you know if it is possible?).My current solution is to make the VM keeper a daemon proccess. I'm going to use a IPC (Inter Proccess Comunication) call to notify the daemon of new apps. This one will manage to make a JNI call inside the running VM. I just have do define which option of IPC I'm going to use. My choice is DBUS, but it may change.
Marcus
Just to let you know: It worked perfectly. I used DBUS to exchange messages between proccesses. The server uses a JNI call to attach to the running VM and call the necessary code.
Marcus
So accept the answer :)
Luís Guilherme