Exemplo n.º 1
0
CCS::Controller::ThermometerSeq *
Controller_impl::list ()
{
    // Create a new thermometer sequence. Because we know
    // the number of elements we will put onto the sequence,
    // we use the maximum constructor.
    CCS::Controller::ThermometerSeq_var listv
        = new CCS::Controller::ThermometerSeq (m_assets.size ());
    listv->length (m_assets.size ());

    // Loop over the m_assets set and create a
    // reference for each device.
    CORBA::ULong count = 0;
    AssetMap::iterator i;
    for (i = m_assets.begin (); i != m_assets.end (); i++)
        listv[count++] = make_dref (m_poa.in (), i->first);
    return listv._retn ();
}
Exemplo n.º 2
0
int
ACE_TMAIN(int argc, ACE_TCHAR *argv[])
{
    CORBA::ULong i = 0;
    try {
        // Initialize the ORB
        CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);

        // Check arguments
        if (argc != 2) {
            std::cerr << "Usage: client IOR_string" << std::endl;
            throw 0;
        }

        // Get controller reference from argv
        // and convert to object.
        CORBA::Object_var obj = orb->string_to_object(argv[1]);
        if (CORBA::is_nil(obj.in())) {
            std::cerr << "Nil controller reference" << std::endl;
            throw 0;
        }

        // Try to narrow to CCS::Controller.
        CCS::Controller_var ctrl;
        try {
            ctrl = CCS::Controller::_narrow(obj.in());
        } catch (const CORBA::SystemException &se) {
            std::cerr << "Cannot narrow controller reference: "
                      << se
                      << std::endl;
            throw 0;
        }
        if (CORBA::is_nil(ctrl.in())) {
            std::cerr << "Wrong type for controller ref." << std::endl;
            throw 0;
        }

        // Get list of devices
        CCS::Controller::ThermometerSeq_var list = ctrl->list();

        // Show number of devices.
        CORBA::ULong len = list->length();
        std::cout << "Controller has " << len << " device";
        if (len != 1)
            std::cout << "s";
        std::cout << "." << std::endl;

        CCS::Thermometer_var t = ctrl->create_thermometer(27, "Room 1");
        CCS::Thermostat_var ts = ctrl->create_thermostat(28, "Room 2", 48);
        CCS::Thermostat_var ts2 = ctrl->create_thermostat(30, "Room 3", 48);
        CCS::Thermostat_var ts3 = ctrl->create_thermostat(32, "Room 3", 68);
        CCS::Thermostat_var ts4 = ctrl->create_thermostat(34, "Room 3", 68);
        CCS::Thermostat_var ts5 = ctrl->create_thermostat(36, "Room 3", 48);
        std::cout << t->location() << std::endl;
        std::cout << ts->location() << std::endl;
        std::cout << ts2->location() << std::endl;
        t->remove();

        list = ctrl->list();
        // Show details for each device.
        for ( i = 0; i < list->length(); i++)
        {
            CCS::Thermometer_ptr ti = list[i];
            std::cout << ti;
        }
        std::cout << std::endl;

        // Change the location of first device in the list
        CCS::AssetType anum = list[0U]->asset_num();
        std::cout << "Changing location of device "
                  << anum << "." << std::endl;
        list[0U]->location("Earth");
        // Check that the location was updated
        std::cout << "New details for device "
                  << anum << " are:" << std::endl;
        CCS::Thermometer_ptr tx = list[0u];
        std::cout << tx << std::endl;

        // Find first thermostat in list.
        CCS::Thermostat_var tmstat;
        for ( i = 0;
                i < list->length() && CORBA::is_nil(tmstat.in());
                i++) {
            tmstat = CCS::Thermostat::_narrow(list[i]);
        }

        // Check that we found a thermostat on the list.
        if (CORBA::is_nil(tmstat.in())) {
            std::cout << "No thermostat devices in list." << std::endl;
        } else {
            // Set temperature of thermostat to
            // 50 degrees (should work).
            set_temp(tmstat.inout(), 50);
            std::cout << std::endl;

            // Set temperature of thermostat to
            // -10 degrees (should fail).
            set_temp(tmstat.inout(), -10);
        }

        // Look for device in Rooms Earth and HAL. This must
        // locate at least one device because we earlier changed
        // the location of the first device to Room Earth.
        std::cout << "Looking for devices in Earth and HAL." << std::endl;
        CCS::Controller::SearchSeq ss;
        ss.length(2);
        ss[0].key.loc(CORBA::string_dup("Earth"));
        ss[1].key.loc(CORBA::string_dup("HAL"));
        ctrl->find(ss);

        // Show the devices found in that room.
        for ( i = 0; i < ss.length(); i++)
            std::cout << ss[i].device.in();          // Overloaded <<
        std::cout << std::endl;

        // Increase the temperature of all thermostats
        // by 40 degrees. First, make a new list (tss)
        // containing only thermostats.
        std::cout << "Increasing thermostats by 40 degrees." << std::endl;
        CCS::Controller::ThermostatSeq tss;
        for ( i = 0; i < list->length(); i++) {
            tmstat = CCS::Thermostat::_narrow(list[i]);
            if (CORBA::is_nil(tmstat.in()))
                continue;                   // Skip thermometers
            len = tss.length();
            tss.length(len + 1);
            tss[len] = tmstat;
        }

        // Try to change all thermostats.
        try {
            ctrl->change(tss, 40);
        } catch (const CCS::Controller::EChange &ec) {
            std::cerr << ec;                     // Overloaded <<
        }
    } catch (const CORBA::Exception & e) {
        std::cerr << "Uncaught CORBA exception: "
                  << e
                  << std::endl;
        return 1;
    } catch (...) {
        return 1;
    }
    return 0;
}