## Stock Broker implementation for the Stock Quoter Publisher/Subscriber Real-time CORBA Service

### Implementation of StockBrokerHome interface

This interface is used to register the necessary factories and mappings with the specified orb and create StockBroker objects.

The Stock_StockBrokerHome_i class is generated automatically by the IDL compiler (using the -GI flag), which is a subclass of POA_Stock::StockBrokerHome class.

This class is also a subclass of ACE_Event_Handler, which can be used as an event handler.

#### Implementing the Constructor

The main steps of this function are described as follows:

- Register the necessary factories and mappings with the specified orb.

        Stock::StockName_init *stockname_factory = new Stock::StockName_init;
        orb->register_value_factory (stockname_factory->tao_repository_id (),
                                     stockname_factory);

        Stock::Cookie_init *cookie_factory = new Stock::Cookie_init;
        orb->register_value_factory (cookie_factory->tao_repository_id (),
                                     cookie_factory;

        Stock_PriorityMapping::register_mapping (orb);

- Register this class as an event handler with the ORB to catch ctrl-c from the command line.

        if (orb_->orb_core ()->reactor ()->register_handler (SIGINT, this) == -1)
          ACE_DEBUG ((LM_DEBUG, "ERROR: Failed to register as a signal handler: %p\n",
                      "register_handler\n"));

#### Implementing the create () member function

- If there is no instance of the StockBroker object in the StockBrokerHome object then create one. The "orb_", "dist", and "stock_name" are in the arguments of the create () function. Since the broker has nothing to do with any of the RTCORBA mechanisms, we can activate it under the default_POA, which is the RootPOA.

        Stock_StockBroker_i *broker = new Stock_StockBroker_i (orb_, dist, stock_name);
        PortableServer::ServantBase_var owner_transfer = broker;
        this->broker_ = broker->_this ();

- Return the StockBroker object.

        return Stock::StockBroker::_duplicate (this->broker_.in ());

#### Implementing the handle_signal () member function

- Shutdown the broker object and the orb used by it.

        this->broker_->shutdown ();

        this->orb_->shutdown (false);

### Implementation of StockBroker interface

This interface is used for Stock Broker client.

The Stock_StockBroker_i class is generated automatically by the IDL compiler (using the -GI flag), which is a subclass of POA_Stock::StockBroker class.

#### Implementing the Constructor

        Stock_StockBroker_i (CORBA::ORB_ptr orb,
                             Stock::StockDistributor_ptr dist,
                             const char *stock_name)
          : orb_ (CORBA::ORB::_duplicate (orb)),
            quoter_ (Stock::StockQuoter::_nil()),
            consumer_ (0),
            distributor_ (Stock::StockDistributor::_duplicate (dist))

The "quoter_", "consumer_", and "distributor_" are four private members of the Stock_StockBroker_i class. They stand for the StockQuoter object that is used to get detailed stock information, the StockNameConsumer object that is used to get notification of updates, and the StockDistributor object that the StockBroker object is used to unsubscribe to.

The main steps of this function are described as follows:

- Get a reference to the RTORB.
- Initialize a CORBA::PolicyList object.
- Create a CLIENT_PROPAGATED priority model policy and add it into the former CORBA::PolicyList object.
- Create a child POA using the former CORBA::PolicyList and narrow it to RTPOA.
- Create a new instance of the StockNameConsumer object with the specified name and activate it under the former RTPOA.

        CORBA::Object_var obj = orb->resolve_initial_references ("RTORB");
        RTCORBA::RTORB_var rt_orb = RTCORBA::RTORB::_narrow (obj.in ());

        TAO::Utils::PolicyList_Destroyer consumer_policies (1);
        consumer_policies.length (1);

        consumer_policies[0]
          = rt_orb->create_priority_model_policy (RTCORBA::CLIENT_PROPAGATED,
                                                  Stock::Priority_Mapping::MEDIUM);

        PortableServer::POA_var poa = this->_default_POA ();
        PortableServer::POAManager_var poa_mgr = poa->the_POAManager ();

        PortableServer::POA_var child_poa
          = poa->create_POA ("StockNameConsumer_POA",
                             poa_mgr.in (),
                             consumer_policies);

        RTPortableServer::POA_var rt_poa = RTPortableServer::POA::_narrow (child_poa.in ());

        this->consumer_ = new Stock_StockNameConsumer_i (*this, stock_name);
        PortableServer::ServantBase_var nameconsumer_owner_transfer = this->consumer_;
        rt_poa->activate_object (this->consumer_);

#### Implementing the get_consumer_notifier () member function

- Return the StockNameConsumer object created by the Constructor.

        Stock::StockNameConsumer_var consumer = this->consumer_->_this ();
        return consumer._retn();

#### Implementing the connect_quoter_info () member function

- Duplicate a StockQuoter object using the StockQuoter object reference "c" in the argument.

        quoter_ = Stock::StockQuoter::_duplicate (c);

#### Implementing the disconnect_quoter_info () member function

- Destroy the StockQuoter object and return it.

        Stock::StockQuoter_var old_quoter = this->quoter_;
        this->quoter_ = Stock::StockQuoter::_nil();
        return old_quoter._retn ();

#### Implementing the get_connection_quoter_info () member function

- Return the StockQuoter object.

        return Stock::StockQuoter::_duplicate (this->quoter_);

#### Implementing the shutdown () member function

- Unsubscribe the StockBroker object.

        this->distributor_->unsubscribe_notifier (this->consumer_->cookie ());

- Deactivate the StockBroker object.

        ::Stock::StockBroker_var broker = this->_this ();
        PortableServer::ObjectId_var oid = this->_default_POA ()->reference_to_id (broker.in ());

        this->_default_POA ()->deactivate_object (oid.in ());

Email: shanshan.jiang@vanderbilt.edu