Real-Time Systems & RT CORBA
by William Nagel

Example 1: 

void processData(const data& packet)
{
  // First, let's run the data through the analyzer to determine 
  // the priority it should be running at.
  int priority = _analyzer->analyzePriority(data);
  // Get the Current object, and set the priority
  CORBA::Object_var object = _orb->resolve_initial_references("RTCurrent");
  RTCORBA::Current_var current = RTCORBA::Current::_narrow(object);
  current->the_priority(priority);
  // Send the data off to the router
  sensorRouter->routeData(data);
}


Example 2: 

// Create a POA for the router and assign the priority propagation model
CORBA::PolicyList policy(1);
policy.length(1);
policy[0] = _rtorb->create_priority_model_policy(RTCORBA::CLIENT_PROPAGATED, 
                                       MEDIUM_PRIORITY);
PortableServer::POA_var routerPOA =
 _rootPOA->create_POA("RouterPOA",PortableServer::POAManager::_nil(),policy);


Example 3: 

// Set up a lane
RTCORBA::ThreadpoolLane lane;
lane.lane_priority = MEDIUM_PRIORITY
lane.static_threads = 5;
lane.dynamic_threads = 10;
// Create a list of all the lanes to use
RTCORBA::ThreadpoolLanes laneList(1);
laneList.length(1);
// Create the threadpool, with the RTORB
RTCORBA::ThreadpoolId tpoolId =
  rtorb->create_threadpool_with_lanes(0, laneList, false, false, 0, 0);

Example 4: 

// Create a policy, from the threadpool created in example 3
RTCORBA::ThreadpoolPolicy_var tpoolPolicy =
      rtorb->create_threadpool_policy(tpoolId);
// Create a list to hold the policy
CORBA::PolicyList policyList(1);
policyList.length(1);
policyList[0] = tpoolPolicy;
// Set the policy for an already existing object reference
CORBA::Object_var object = 
     sensorRouter->create_policy_overrides(policyList, SET_OVERRIDE);
sensorRouter = SensorRouter::_narrow(object);

Example 5: 

// Load the policy manager
CORBA::Object_var object=orb->resolve_initial_references("ORBPolicyManager");
CORBA::PolicyManager_var policyManager=CORBA::PolicyManager::_narrow(object);
// Create the timeout policy, setting policy to 1 ms, in 100's of nanoseconds
CORBA::PolicyList timeoutPolicy(1);
timeoutPolicy.length(1);
timeoutPolicy[0] = orb->createPolicy(RELATIVE_RT_TIMEOUT_POLICY_TYPE, 10000)
// Set the timeout policy with the ORB
policyManager->set_policy_overrides(timeoutPolicy, SET_OVERRIDE);


Example 6: 

void SensorRouter::routeData(Data d) {
  // Attempt to acquire a lock on the Mutex, but give up 
  // if the invocation timeout threshold is passed
  if(_mutex->try_lock(10000)) {
    // Send the data out to the various data sinks for processing
    // ....
    _mutex->unlock();  // Release the Mutex lock
  }
}


Listing One

class SensorPriorityMapping : public RTCORBA::PriorityMapping
{
  public:
    virtual
    CORBA::Boolean to_native(RTCORBA::Priority corba_priority,
                             RTCORBA::NativePriority& native_priority);
    virtual
    CORBA::Boolean to_CORBA(RTCORBA::NativePriority native_priority,
                            RTCORBA::Priority& corba_priority);
};
CORBA::Boolean
SensorPriorityMapping::to_native(RTCORBA::Priority corba_priority,
                                 RTCORBA::NativePriority& native_priority)
{
  if(corba_priority <= 1024) {
    native_priority = 32;
  }
  else if(corba_priority <= 2048) {
    native_priority = 64;
  }
  else if(corba_priority <= 4096) {
    native_priority = 128;
  }
  else {
    native_priority = 192;
  }
  return true;
}
CORBA::Boolean
SensorPriorityMapping::to_corba(RTCORBA::NativePriority native_priority,
                                RTCORBA::Priority& corba_priority)
{
  if(native_priority <= 32) {
    corba_priority = 1024;
  }
  else if(native_priority <= 64) {
    corba_priority = 2048;
  }
  else if(native_priority <= 128) {
    corba_priority = 4096;
  }
  else {
    corba_priority = 8192;
  }
  return true;
}

Listing Two

#define LOW_PRIORITY       1024
#define MEDIUM_PRIORITY    2048
#define HIGH_PRIORITY      4096
#define EMERGENCY_PRIORITY 8192

CORBA::ORB_var orb;
RTCORBA::RTORB_var rtorb;

SensorRouterImpl* sensorRtrServant;
SensorRouter_var sensorRouter;

void initCORBA(int argc, char** argv)
{
  // Initialize the ORB, and assign it to the CORBA::ORB reference orb
  orb = CORBA::ORB_init(argc, argv);
  
  // Resolve the real-time ORB, and assign it to the
  // RTCORBA::RTORB reference rtorb
  CORBA::Object_var object = orb->resolve_initial_references("RTORB");
  rtorb = RTCORBA::RTORB::_narrow(object);
}
void activateRouter(void)
{
  // Create a list of policies to assign to the POA
  CORBA::PolicyList policies(2);
  policies.length(2);
  // Create the propagation policy
  policies[0] = 
    _rtorb->create_priority_model_policy(RTCORBA::CLIENT_PROPAGATED, 
                                         MEDIUM_PRIORITY);
  // Create threadpool lanes for each priority level
  // Values are (in order): 
  //   Default priority, # of static threads, # of dynamic threads
  RTCORBA::ThreadpoolLane lowPriorityLane = {LOW_PRIORITY, 20, 20};
  RTCORBA::ThreadpoolLane mediumPriorityLane = {MEDIUM_PRIORITY, 20, 30};
  RTCORBA::ThreadpoolLane highPriorityLane = {HIGH_PRIORITY, 10, 10};
  RTCORBA::ThreadpoolLane emergencyPriorityLane = 
    {EMERGENCY_PRIORITY,  2, 5};
  RTCORBA::ThreadpoolLanes lanes(4);
  lanes.length(4);
  lanes[0] = lowPriorityLane;
  lanes[1] = mediumPriorityLane;
  lanes[2] = highPriorityLane;
  lanes[3] = emergencyPriorityLane;
  // Create the threadpool, with the no default stacksize, 
  // thread borrowing, or request buffering
  RTCORBA::ThreadpoolId tpoolId =
    rtorb->create_threadpool_with_lanes(0, lanes, false, false, 0, 0);
  // Create the threadpool policy to associate with the POA
  RTCORBA::ThreadpoolPolicy_var tpoolPolicy =
    rtorb->create_threadpool_policy(tpoolId);
  policies[1] = tpoolPolicy;
  // Create POA to use for creating router, using the above policies
  PortableServer::POA_var routerPOA = _rootPOA->create_POA("RouterPOA", 
                            PortableServer::POAManager::_nil(), policies);
  // Create an instance of the servant class
  sensorRtrServant = new SensorRouterImpl;
  // Activate the servant with the POA
  CORBA::Object_var object = routerPOA->activate_object(sensorRtrServant);
  sensorRouter = SensorRouter::_narrow(object);
}







4


