Resources | developer.brewmp.com Resources | developer.brewmp.com

Developer

resources

Example - using a singleton service class

The c_appio example contains two applications: c_appwrite and c_appread. Both applications use the SimpleIOEcho service. The CIF for both applications contains the following declarations for the SimpleIOEcho service:

-- Service declaration that defines the remote "singleton"  
-- service: where it lives, and what interface it uses.  
  
Service {  
   serviceid = AEECLSID_SimpleIOEchoServiceSingle,  
   iid = AEEIID_ISimpleIO,  
   serverid = 0, -- service is in kernel  
   servedclassid = AEECLSID_SimpleIOEchoSingle,  
   required_privs = { 0 }  
};  
  
-- Declare a singleton class using AEECLSID_SimpleIOEcho class declared  
-- in c_service  
Singleton{  
   classid = AEECLSID_SimpleIOEchoSingle,  
   singled_id = AEECLSID_SimpleIOEcho,  
}

The Singleton declaration links AEECLSID_SimpleIOEchoSingle to the ClassID declared in the CIF for SimpleIOEcho. The Service declaration links AEECLSID_SimpleIOEchoServiceSingle to the classid in the Singleton declaration. The serverid in the Service declaration specifies the host process for the service; zero indicates the kernel process.

In the c_appwrite application, the c_appwrite_CtorZ() constructor function instantiates the SimpleIOEcho service as a singleton service, as follows:

ERR_BAIL(IEnv_CreateInstance(me->piEnv,  
                  AEECLSID_SimpleIOEchoServiceSingle,  
                  (void **)&me->piSimpleIO));

Note: Reading data by calling ISimpleIO_Read() consumes the data (it is removed from the buffer and cannot be read again).

When the c_appread application instantiates the SimpleIOEcho service as a singleton service, a pointer to the existing service is returned. c_appread reads the data written by c_appwrite. c_appwrite and c_appread share the SimpleIOEcho service, so the data that is read by c_appread is the same as the data written by c_appwrite.