Chair for Computer Aided Medical Procedures & Augmented Reality
Lehrstuhl für Informatikanwendungen in der Medizin & Augmented Reality

THIS WEBPAGE IS DEPRECATED - please visit our new website

Up to DwarfMiddlewareTutorial, forward to DwarfMiddlewareTutorialLesson7, back to DwarfMiddlewareTutorialLesson5

Lesson 6: Session Handling

In this lesson, you will learn how one service can handle multiple sessions to other services.

Reconfigure the time client and server

Use the installed binaries TimeService and TimeClient of DwarfMiddlewareTutorialLesson2, and start up the service manager.

Run make all in (top build directory)/src/tutorials/middleware/lesson6. This will install two new binaries, TimeService and TimeClient, overwriting those of DwarfMiddlewareTutorialLesson2. It will also install two new service descriptions:

TimeService TimeClient
<service name="TimeService" isTemplate="true">
   <ability name="giveTime" type="TimeOfDay">
      <connector protocol="ObjrefExporter"/>
<service name="TimeClient" isTemplate="true">
   <need name="time" type="TimeOfDay"
    minInstances="1" maxInstances="3">
      <connector protocol="ObjrefImporter"/>

There are two differences to the previous lesson:

  • This time, both the TimeService and the TimeClient are template services.
  • The need time of the TimeClient has two new parameters, minInstances and maxInstances. These specify how many communication partners should be connected to this need:
    • The middleware will not connect the TimeClient to anything else until at least minInstances partners have been found for the need.
    • The middleware will connect up to maxInstances partners at any time.

Try it out

In three terminals, run three TimeServices. In three other terminals, run three TimeClients. All three clients will be able to run and connect to all three TimeServices, showing this DIVE image:

Three Time Services and Three Clients

The TimeClient will get the time from each partner:

Getting time from 3 partners...
current time from service:TimeOfDay.ObjrefExporter://atbruegge11/TimeService5/giveTime/ObjrefExporter is: 16:44:44
current time from service:TimeOfDay.ObjrefExporter://atbruegge11/TimeService6/giveTime/ObjrefExporter is: 16:44:44
current time from service:TimeOfDay.ObjrefExporter://atbruegge11/TimeService7/giveTime/ObjrefExporter is: 16:44:44

How it works

If a service implements the SvcSession interface, the service manager calls the newSession and endSession methods when a need or ability is about to be connected to other services.

For example, TimeClient_impl::newSession() is called for each TimeService:

16:44:35.270 00003 10 TimeClient.cpp       148 TimeClient_impl :
16:44:35.313 00003 10 TimeClient.cpp       150 TimeClient_impl :
   partner's attributes are: (abilityName=giveTime),(abilityState=Registered),(hostname=atbruegge11),
   (serviceName=TimeService5),(serviceState=Started),(started=true),(status=test status),(username=macwilli)

In the newSession method, the TimeService creates a new object of class TimeContact to handle the connection to the TimeService. It returns a reference to that object to the service manager. The service manager then calls importObjref on that newly created object.

Exercise 6.1 How many TimeServices does one TimeClient connect to? What happens if you change maxInstances?

Exercise 6.2 How many TimeClients can you connect to one TimeService? What happens if you change maxInstances?

Exercise 6.3 Set the startOnDemand and startCommand parameters for the TimeService, as in DwarfMiddlewareTutorialLesson3. Then, start a TimeClient. How many TimeServices are started? What happens if you start a second TimeClient?

Exercise 6.4 Take a close look at the implementation of newSession and endSession. How do they achieve thread safety? How does endSession ensure that the TimePartner object is deleted?

Exercise 6.5 Extend the TimeService to give a different time to each TimeClient that connects to it: the first client receives the correct time, the second receives the current time one hour ago, and so on.

Exercise 6.6 Give the TimeService three different abilities which differ by an attribute timezone, with values of Munich, NewYork and Tokyo. Make the TimeService give clients the correct time for each time zone. Also, make the TimeClient print e.g. "the current time in Munich is 16:59". (Hint: the TimeClient should use the getAttribute() method at some point, and the TimeServer should call serviceDesc->getAbility(needabilityname) in newSession().)

Exercise 6.7 Now do it the other way around: have the TimeService only have one ability, but give the TimeClient an attribute timezoneRelativeToGMT, with a value of +1, -6, etc., specified in the XML file. Have the TimeService give each TimeClient its correct time.

Up to DwarfMiddlewareTutorial, forward to DwarfMiddlewareTutorialLesson7, back to DwarfMiddlewareTutorialLesson5

-- AsaMacWilliams - 11 Nov 2003

Edit | Attach | Refresh | Diffs | More | Revision r1.6 - 17 Mar 2005 - 21:11 - NikolasDoerfler

Lehrstuhl für Computer Aided Medical Procedures & Augmented Reality    rss.gif