Merge pull request #2303 from jwillemsen/jwi-803
[ACE_TAO.git] / TAO / examples / Advanced / ch_21 / server.h
blobed8bba2774e8f0e0199530bdc28c97b52ffe5cf1
1 //=============================================================================
2 /**
3 * @file server.h
5 * @author Source code used in TAO has been modified and adapted from the codeprovided in the book
6 * @author "Advanced CORBA Programming with C++" by MichiHenning and Steve Vinoski. Copyright 1999. Addison-Wesley
7 * @author Reading
8 * @author MA.Modified for TAO by Mike Moran <mm4@cs.wustl.edu>
9 */
10 //=============================================================================
15 #ifndef server_HH_
16 #define server_HH_
18 #include "CCSS.h"
19 #include "icp.h"
20 #include <set>
21 #include <list>
22 #include <map>
23 #include <assert.h>
24 #include "tao/PortableServer/PortableServer.h"
25 #include "tao/PortableServer/ServantLocatorC.h"
27 #include "CCSS.h"
29 using namespace std;
31 class Controller_impl;
33 class Thermometer_impl : public virtual POA_CCS::Thermometer {
34 public:
35 // CORBA attributes
36 virtual CCS::ModelType model();
37 virtual CCS::AssetType asset_num();
38 virtual CCS::TempType temperature();
39 virtual CCS::LocType location();
40 virtual void location(const char *loc);
41 virtual void remove();
43 // Constructor & destructor
44 Thermometer_impl(CCS::AssetType anum);
45 virtual ~Thermometer_impl();
47 static Controller_impl * m_ctrl; // My controller
48 const CCS::AssetType m_anum; // My asset number
50 void _add_ref();
51 void _remove_ref();
53 private:
54 // Helper functions
55 CCS::ModelType get_model();
56 CCS::TempType get_temp();
57 CCS::LocType get_loc();
58 void set_loc(const char * new_loc);
60 ACE_Mutex m_count_mutex;
61 CORBA::ULong m_ref_count;
62 bool m_removed;
64 // Copy and assignment not supported
65 Thermometer_impl(const Thermometer_impl &);
66 void operator=(const Thermometer_impl &);
69 class Thermostat_impl :
70 public virtual POA_CCS::Thermostat,
71 public virtual Thermometer_impl {
72 public:
73 // CORBA operations
74 virtual CCS::TempType get_nominal();
75 virtual CCS::TempType set_nominal(CCS::TempType new_temp);
77 // Constructor and destructor
78 Thermostat_impl(CCS::AssetType anum);
79 virtual ~Thermostat_impl();
81 private:
82 // Helper functions
83 CCS::TempType get_nominal_temp();
84 CCS::TempType set_nominal_temp(CCS::TempType new_temp);
86 // Copy and assignment not supported
87 Thermostat_impl(const Thermostat_impl &);
88 void operator=(const Thermostat_impl &);
91 class Controller_impl : public virtual POA_CCS::Controller {
92 public:
93 // CORBA operations
94 virtual CCS::Controller::ThermometerSeq* list();
95 virtual void find(CCS::Controller::SearchSeq & slist);
96 virtual void change(const CCS::Controller::ThermostatSeq & tlist,
97 CORBA::Short delta);
99 virtual CCS::Thermometer_ptr create_thermometer(CCS::AssetType anum,
100 const char* loc);
102 virtual CCS::Thermostat_ptr create_thermostat(CCS::AssetType anum,
103 const char* loc,
104 CCS::TempType temp);
106 // Constructor and destructor
107 Controller_impl(PortableServer::POA_ptr poa,
108 const char * asset_file);
110 virtual ~Controller_impl();
112 // Helper functions to allow access to the object map
113 void add_impl(CCS::AssetType anum);
114 void remove_impl(CCS::AssetType anum);
115 bool exists(CCS::AssetType anum);
117 ACE_Mutex m_assets_mutex;
119 private:
120 typedef set<CCS::AssetType> AssetSet;
121 AssetSet m_assets;
123 // POA for thermometers and thermostats
124 PortableServer::POA_var m_poa;
126 // Name of asset number file
127 CORBA::String_var m_asset_file;
129 // Copy and assignment not supported
130 Controller_impl(const Controller_impl &);
131 void operator=(const Controller_impl &);
133 // Function object for the find_if algorithm to search for
134 // devices by location and model string.
135 class StrFinder {
136 public:
137 StrFinder(
138 CCS::Controller::SearchCriterion sc,
139 const char * str
140 ) : m_sc(sc), m_str(str) {}
141 bool operator()(
142 CCS::AssetType anum) const
144 char buf[32];
145 switch (m_sc) {
146 case CCS::Controller::LOCATION:
147 ICP_get(anum, "location", buf, sizeof(buf));
148 break;
149 case CCS::Controller::MODEL:
150 ICP_get(anum, "model", buf, sizeof(buf));
151 break;
152 default:
153 assert(0); // Precondition violation
155 return strcmp(buf, m_str) == 0;
157 private:
158 CCS::Controller::SearchCriterion m_sc;
159 const char * m_str;
163 class DeviceLocator_impl :
164 public virtual PortableServer::ServantLocator {
165 public:
166 DeviceLocator_impl(Controller_impl * ctrl);
168 virtual PortableServer::Servant
169 preinvoke(
170 const PortableServer::ObjectId & oid,
171 PortableServer::POA_ptr /* poa */ ,
172 const char * operation,
173 void * & /* cookie */
176 virtual void
177 postinvoke(
178 const PortableServer::ObjectId & /* oid */ ,
179 PortableServer::POA_ptr /* poa */ ,
180 const char * /* operation */ ,
181 void * /* cookie */ ,
182 PortableServer::Servant /* servant */
183 ) {}
184 private:
185 Controller_impl * m_ctrl;
187 typedef list<Thermometer_impl *> EvictorQueue;
188 typedef map<CCS::AssetType, EvictorQueue::iterator>
189 ActiveObjectMap;
191 static const unsigned int MAX_EQ_SIZE;// = 100;
192 EvictorQueue m_eq;
193 ActiveObjectMap m_aom;
195 // Copy and assignment not supported
196 DeviceLocator_impl(const DeviceLocator_impl &);
197 void operator=(const DeviceLocator_impl &);
200 #endif