Project

General

Profile

how to manage the lifecycle of a single user application?

Added by Yang Liu over 6 years ago

Hi, there,

I am porting a MFC application to Wt. I have finished most part of the UI replacement. But I still have a question about the management of the session and the lifecycle of the application.

This application is interacting with some hardware internally, so it could only support one user at same time. If another user try to use this application, then the previous user must be kicked off and the application should be restart (because the hardware need re-initialize).

Is this achievable? Please suggest how to handle this case. Sample code is welcome.

Best Regards,

Yang


Replies (1)

RE: how to manage the lifecycle of a single user application? - Added by Stefan Arndt over 6 years ago

Hi,

I would try the following: Somewhere you should have a function creating your application objects, something like this:

Wt::Application* create_application( Wt::WEnvironment const& env )
{
  return new Your_Custom_App( env );
}

void main(...)
{
  ...
  Wt::WServer server( argv[0] );            
  server.setServerConfiguration( argc, argv, "resources/server_config" );
  server.addEntryPoint( Wt::Application, create_application, "", "" );
  ...
}

Now you can simply add a condition to your create_application-function:

// global flag
bool in_use = false;

Wt::Application* create_application( Wt::WEnvironment const& env )
{
  if( in_use )
  {
    // TODO cleanup old sessions, reset hardware ...
  }

  in_use = true;
  return new Your_Custom_App( env );
}

The hardware stuff seems to be more tricky. To make this more easy, I would make it a resource with a single lock that only one of your application objects can hold.

Of course you need propper init/deinit functions that you can call from Your_Custom_App. If you really need to restart the Wt-Server .. you could do that, but I think you should be fine without it, too.

Finally during the cleanup above, you could post a function to each session (Wt::WServer::post()) which causes the old sessions to release the lock; then you can create your new session.

Hope these ideas help,

Regards, Stefan

    (1-1/1)