File: subdev_diag.cpp

package info (click to toggle)
tango 8.1.2c%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 21,964 kB
  • ctags: 14,286
  • sloc: cpp: 133,954; sh: 14,704; ansic: 1,083; makefile: 944; java: 215; python: 55
file content (499 lines) | stat: -rw-r--r-- 14,518 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
static const char *RcsId = "$Id: subdev_diag.cpp 22213 2013-03-07 14:32:56Z taurel $\n$Name$";

//+=============================================================================
//
// file :               subdev_diag.cpp
//
// description :        Collect information on all used sub devices
//                      in a device server.
//
// project :            TANGO
//
// author(s) :          J.Meyer
//
// Copyright (C) :      2004,2005,2006,2007,2008,2009,2010,2011,2012,2013
//						European Synchrotron Radiation Facility
//                      BP 220, Grenoble 38043
//                      FRANCE
//
// This file is part of Tango.
//
// Tango is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Tango is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Tango.  If not, see <http://www.gnu.org/licenses/>.
//
// $Revision: 22213 $
//
//-=============================================================================

#if HAVE_CONFIG_H
#include <ac_config.h>
#endif

#include <tango.h>

extern omni_thread::key_t key_py_data;

namespace Tango
{

//+----------------------------------------------------------------------------
//
// method :         SubDevDiag::~SubDevDiag()
//
// description :    Destructor to free the map data
//
//-----------------------------------------------------------------------------

SubDevDiag::~SubDevDiag()
{
	cout4 << "SubDevDiag::~SubDevDiag() entering ... " << endl;

	// lock the sub device map
	omni_mutex_lock l(sub_dev_map_mutex);

	// remove all sub devices
	sub_device_map.clear();
	sub_device_startup_map.clear();
}


//+----------------------------------------------------------------------------
//
// method :         SubDevDiag::set_associated_device()
//
// description :    Set the device name that should be asscociated
//                  to a thread in the device server
//
// in :             dev_name - device name
//
//-----------------------------------------------------------------------------

void SubDevDiag::set_associated_device(string dev_name)
{
	cout4 << "SubDevDiag::set_associated_device() entering ... ";

	// get thread
	omni_thread *th = omni_thread::self();
	if ( th != NULL )
	{
		// write the device name to the per thread data structure
		omni_thread::value_t *tmp_py_data = th->get_value(key_py_data);
		if ( tmp_py_data != NULL )
			(static_cast<PyData *>(tmp_py_data))->device_name = dev_name;
	}
}

//+----------------------------------------------------------------------------
//
// method :         SubDevDiag::get_associated_device()
//
// description :    Get the device name that is asscociated
//                  with the current thread of the device server
//
// return :         associated device name
//
//-----------------------------------------------------------------------------

string SubDevDiag::get_associated_device()
{
	cout4 << "SubDevDiag::get_associated_device() entering ... " << endl;

	string dev_name = "";

	// get thread
	omni_thread *th = omni_thread::self();
	if ( th != NULL )
	{
		// read the device name from the per thread data structure
		omni_thread::value_t *tmp_py_data = th->get_value(key_py_data);
		if ( tmp_py_data != NULL )
			dev_name = (static_cast<PyData *>(tmp_py_data))->device_name;
	}

	cout4 << "SubDevDiag::get_associated_device() found : " << dev_name << endl;
	return dev_name;
}

//+----------------------------------------------------------------------------
//
// method :         SubDevDiag::register_sub_device()
//
// description :    Register a sub device for an associated device
//                  in the list of sub devices of the device server
//
// in :             dev_name = associated device name
//                  sub_dev_name = sub device name
//
//-----------------------------------------------------------------------------

void SubDevDiag::register_sub_device (string dev_name, string sub_dev_name)
{
	cout4 << "SubDevDiag::register_sub_device() dev_name = " << dev_name
	      << " sub_dev_name = "<< sub_dev_name << endl;

	bool found = false;

	// be sure that all names are lower case letters
	std::transform(dev_name.begin(), dev_name.end(),
                   dev_name.begin(), ::tolower);
	std::transform(sub_dev_name.begin(), sub_dev_name.end(),
                   sub_dev_name.begin(), ::tolower);

	// lock the sub device map
	omni_mutex_lock l(sub_dev_map_mutex);

	// Find whether a sub device list for the device is already available
	std::map<string,SubDeviceList>::iterator ipos;
	ipos = sub_device_map.find(dev_name);

	if (ipos == sub_device_map.end())
	{
		// device not known, add a new sub device
		sub_device_map[dev_name].sub_devices.push_back(sub_dev_name);
		sub_device_map[dev_name].modified = true;
	}
	else
	{
		// Check whether the sub device name is alreay in the list
		for (unsigned int i=0; i<ipos->second.sub_devices.size(); i++ )
		{
			if (ipos->second.sub_devices[i] == sub_dev_name)
			{
				// Name is already in the list
				found = true;
				break;
			}
		}
		if ( found == false)
		{
			// name is not in the list, add the sub device
			ipos->second.sub_devices.push_back(sub_dev_name);
			ipos->second.modified = true;
		}
	}
}

//+----------------------------------------------------------------------------
//
// method :         SubDevDiag::remove_sub_devices()
//
// description :    Remove all sub devices for a device of the server
//
// in :             dev_name = device name
//
//-----------------------------------------------------------------------------

void SubDevDiag::remove_sub_devices (string dev_name)
{
	cout4 << "SubDevDiag::remove_sub_device() dev_name = " << dev_name << endl;

	// be sure that all names are lower case letters
	std::transform(dev_name.begin(), dev_name.end(),
                   dev_name.begin(), ::tolower);

	// lock the sub device map
	omni_mutex_lock l(sub_dev_map_mutex);

	// remove the list of sub devices for a device
	std::map<string,SubDeviceList>::iterator ipos;
	ipos = sub_device_map.find(dev_name);

	if (ipos != sub_device_map.end())
	{
		sub_device_map.erase(ipos);
	}
}

//+----------------------------------------------------------------------------
//
// method :         SubDevDiag::remove_sub_devices()
//
// description :    Remove all sub devices of the server
//
// in :             dev_name = device name
//
//-----------------------------------------------------------------------------

void SubDevDiag::remove_sub_devices()
{
	cout4 << "SubDevDiag::remove_sub_devices() remove ALL " << endl;

	// lock the sub device map
	omni_mutex_lock l(sub_dev_map_mutex);
	// remove all sub devices
	sub_device_map.clear();
}

//+----------------------------------------------------------------------------
//
// method :         SubDevDiag::get_sub_devices()
//
// description :    Read the list of sub devices for the device server
//                  The returned strings are formated as:
//                    "device_name sub_device_name"
//                  or
//                    sub_device_name
//                  when no associated device could be identified.
//
// return :         An array of formated strings
//
//-----------------------------------------------------------------------------

Tango::DevVarStringArray *SubDevDiag::get_sub_devices()
{
	cout4 << "SubDevDiag::get_sub_devices() entering ... " << endl;

	Tango::DevVarStringArray *ret;
	vector<string> sub_dev_list;
	string tmp;

	// lock the sub device map
	omni_mutex_lock l(sub_dev_map_mutex);

	try
	{
		std::map<string,SubDeviceList>::iterator ipos;
		for (ipos = sub_device_map.begin(); ipos != sub_device_map.end(); ++ipos)
		{
			for (unsigned int i=0; i<ipos->second.sub_devices.size(); i++)
			{
				if ( ipos->first.empty() )
					tmp = ipos->second.sub_devices[i];
				else
					tmp = ipos->first + " " + ipos->second.sub_devices[i];

				sub_dev_list.push_back (tmp);
			}
		}

		ret = new Tango::DevVarStringArray(DefaultMaxSeq);
		ret->length(sub_dev_list.size());
		for (unsigned int k = 0; k<sub_dev_list.size(); k++)
			(*ret)[k] = CORBA::string_dup(sub_dev_list[k].c_str());

		return(ret);
	}

	catch (bad_alloc)
	{
		Except::throw_exception((const char *)API_MemoryAllocation,
				        (const char *)"Can't allocate memory in server",
				        (const char *)"SubDevDiag::get_sub_devices");
	}

	// Should never reach here. To make compiler happy

	ret = NULL;
	return ret;
}

//+----------------------------------------------------------------------------
//
// method :         SubDevDiag::store_sub_devices()
//
// description :    Store the list of sub devices for the devices of
//                  the server.
//                  The sub device names are stored as a string array
//                  under the device property "sub_devices".
//                  Sub device names without an associated device,
//                  will be stored under the name of the administration
//                  device.
//
//                  Database access will only happen when the list of
//                  sub devices was modified and when the list is different
//                  from the list read into the db_cache during the server
//                  startup.
//
//-----------------------------------------------------------------------------

void SubDevDiag::store_sub_devices()
{
	cout4 << "SubDevDiag::store_sub_devices() entering ... " << endl;

	Tango::Util *tg = Tango::Util::instance();

	// lock the sub device map
	omni_mutex_lock l(sub_dev_map_mutex);

	// loop over the sub device map

	std::map<string,SubDeviceList>::iterator ipos;
	for (ipos = sub_device_map.begin(); ipos != sub_device_map.end(); ++ipos)
	{
		// Check whether the list was modified
		if ( ipos->second.modified == true )
		{
			// Check whether for modifications compared to
			// the list read into db_cache during startup

			// check the number of sub devices
			if ( ipos->second.sub_devices.size() ==
			    sub_device_startup_map[ipos->first].sub_devices.size() )
			{
				// find sub device names in the start-up list
				bool is_equal = true;
				for ( unsigned int i=0; i<ipos->second.sub_devices.size(); i++ )
				{
					bool found = false;
					for ( unsigned int k=0; k<sub_device_startup_map[ipos->first].sub_devices.size(); k++ )
					{
						if (ipos->second.sub_devices[i] == sub_device_startup_map[ipos->first].sub_devices[k])
						{
							found = true;
							break;
						}
					}

					if ( found == false )
					{
						is_equal = false;
						break;
					}
				}

				if ( is_equal == true )
				{
					// sub device names are equal to the names
					// read from the database at server start-up.
					// Clear the modification flag
					ipos->second.modified = false;

					continue;
				}
			}




			// write the sub device list as device property
			try
			{
				DbDatum list ("__SubDevices");
				DbData db_data;
				list << ipos->second.sub_devices;
				db_data.push_back(list);

				// Check for a valid database object.
				// In the database server itself or any server
				// running without a database the database object is
				// not initialised.
				if ( Tango::Util::_UseDb == true )
				{
					// cout << "Storing sub device list for " << ipos->first << endl;

					if ( ipos->first.empty() )
					{
						DServer *adm_dev = tg->get_dserver_device();
						tg->get_database()->put_device_property (adm_dev->get_name(), db_data);
					}
					else
					{
						tg->get_database()->put_device_property (ipos->first, db_data);
					}
				}

				// clear the modification flag
				ipos->second.modified = false;
			}
			catch (Tango::DevFailed &) {}
		}
	}
}

//+----------------------------------------------------------------------------
//
// method :         SubDevDiag::get_sub_devices_from_cache()
//
// description :    Read the list of sub devices from the
//                  database cache. The cache is filled at
//                  server sart-up.
//
//-----------------------------------------------------------------------------
void SubDevDiag::get_sub_devices_from_cache()
{
	cout4 << "SubDevDiag::get_sub_devices_from_cache() entering ... " << endl;

	DbServerCache *db_cache;
	Tango::Util *tg = Tango::Util::instance();

	try
	{
		db_cache = tg->get_db_cache();
	}
	catch (Tango::DevFailed &e)
	{
		Except::print_exception(e);
		db_cache = NULL;
	}

	if (db_cache != NULL)
	{
		// get the name of the admin device
		DServer *adm_dev = tg->get_dserver_device();
		string adm_name = adm_dev->get_name();
		// be sure that all names are lower case letters
		std::transform(adm_name.begin(), adm_name.end(),
		               adm_name.begin(), ::tolower);

		// get all devices served
		vector<DeviceImpl *> dev_list = tg->get_device_list("*");

		//cout << "devices served :" << endl;

		for (unsigned int k=0; k<dev_list.size(); k++)
		{
			string dev_name = dev_list[k]->get_name();
			// be sure that all names are lower case letters
			std::transform(dev_name.begin(), dev_name.end(),
			               dev_name.begin(), ::tolower);

			//cout << dev_name << endl;

			DevVarStringArray *property_names = new DevVarStringArray;
			property_names->length(2);
			(*property_names)[0] = string_dup(dev_name.c_str());
			(*property_names)[1] = string_dup("__SubDevices");

			try
			{
				const DevVarStringArray *property_values = db_cache->get_dev_property(property_names);
				if ( atol((*property_values)[3]) > 0 )
				{
					// if the device is the admin device, set dev_name to ""
					// to have the same syntax as in the dynamically created
					// sub device map.

					if ( dev_name == adm_name )
						dev_name = "";

					for (unsigned int i=4; i<property_values->length(); i++)
					{
						//cout << "sub_dev = " << (*property_values)[i].in() << endl;
						sub_device_startup_map[dev_name].sub_devices.push_back((*property_values)[i].in());
					}
				}
			}

			catch(Tango::DevFailed &)
			{
				cerr << "Sub device not found in DB cache for " << dev_name << endl;
			}

			delete property_names;
		}
	}
	else
		cerr << "No database cache found to initialise sub device map!" << endl;
}

} // End of Tango namespace