source: trunk/external/atnf/pks/pksmb_support.cc@ 1688

Last change on this file since 1688 was 1427, checked in by Malte Marquarding, 16 years ago

sync with livedata/implement/atnf

File size: 7.4 KB
Line 
1//#---------------------------------------------------------------------------
2//# pksmb_support.cc: Support functions for Parkes glish clients.
3//#---------------------------------------------------------------------------
4//# Copyright (C) 1994-2007
5//# Associated Universities, Inc. Washington DC, USA.
6//#
7//# This library is free software; you can redistribute it and/or modify it
8//# under the terms of the GNU Library General Public License as published by
9//# the Free Software Foundation; either version 2 of the License, or (at your
10//# option) any later version.
11//#
12//# This library is distributed in the hope that it will be useful, but WITHOUT
13//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14//# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
15//# License for more details.
16//#
17//# You should have received a copy of the GNU Library General Public License
18//# along with this library; if not, write to the Free Software Foundation,
19//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
20//#
21//# Correspondence concerning AIPS++ should be addressed as follows:
22//# Internet email: aips2-request@nrao.edu.
23//# Postal address: AIPS++ Project Office
24//# National Radio Astronomy Observatory
25//# 520 Edgemont Road
26//# Charlottesville, VA 22903-2475 USA
27//#
28//# Original: David Barnes, February 1997
29//# $Id: pksmb_support.cc,v 19.9 2007-02-07 06:44:05 cal103 Exp $
30//----------------------------------------------------------------------------
31
32// AIPS++ includes.
33#include <casa/sstream.h>
34#include <casa/Arrays/Array.h>
35#include <casa/BasicSL/Complex.h>
36#include <casa/BasicSL/String.h>
37
38// ATNF includes.
39#include <atnf/pksGlish/GlishArray.h>
40#include <atnf/pksGlish/GlishSysEvent.h>
41#include <atnf/pksGlish/GlishRecord.h>
42#include <atnf/pks/pksmb_support.h>
43
44
45GlishSysEventSource *g_glishStream;
46GlishRecord g_msg;
47
48//----------------------------------------------------------------- pksmbSetup
49
50void pksmbSetup(GlishSysEventSource &glishStream, String clientName)
51{
52 glishStream.addTarget(shutdown_event, "shutdown");
53 glishStream.setDefault(unknown_event);
54
55 // Set up message handling.
56 g_glishStream = &glishStream;
57 g_msg.add("location", clientName);
58
59 // Install client.
60 glishStream.loop();
61}
62
63//------------------------------------------------------------- shutdown_event
64
65// Handler for "shutdown" event.
66
67Bool shutdown_event(GlishSysEvent &, void *)
68{
69 exit(0);
70
71 return True;
72}
73
74//-------------------------------------------------------------- unknown_event
75
76// Handler for "unknown" event.
77
78Bool unknown_event(GlishSysEvent &event, void *)
79{
80 logWarning("WARNING: Unknown event: " + event.type());
81 return True;
82}
83
84//-------------------------------------------------------------------- getParm
85
86// Utility functions for extracting parameter values from a Glish record.
87
88template<class T>
89Bool getParm(const GlishRecord &parms,
90 const String &item,
91 const T &default_val,
92 T &value)
93{
94 if (parms.exists(item)) {
95 GlishArray tmp = parms.get(item);
96 tmp.get(value);
97 return True;
98 } else {
99 value = default_val;
100 return False;
101 }
102}
103
104template Bool getParm(const GlishRecord &parms, const String &item,
105 const Bool &default_val, Bool &value);
106template Bool getParm(const GlishRecord &parms, const String &item,
107 const Int &default_val, Int &value);
108template Bool getParm(const GlishRecord &parms, const String &item,
109 const Float &default_val, Float &value);
110template Bool getParm(const GlishRecord &parms, const String &item,
111 const Double &default_val, Double &value);
112template Bool getParm(const GlishRecord &parms, const String &item,
113 const Complex &default_val, Complex &value);
114template Bool getParm(const GlishRecord &parms, const String &item,
115 const String &default_val, String &value);
116
117template<class T>
118Bool getParm(const GlishRecord &parms,
119 const String &item,
120 const T &default_val,
121 Array<T> &value)
122{
123 if (parms.exists(item)) {
124 GlishArray tmp = parms.get(item);
125 tmp.get(value);
126 return True;
127 } else {
128 value = default_val;
129 return False;
130 }
131}
132
133template Bool getParm(const GlishRecord &parms, const String &item,
134 const Bool &default_val, Array<Bool> &value);
135template Bool getParm(const GlishRecord &parms, const String &item,
136 const Int &default_val, Array<Int> &value);
137template Bool getParm(const GlishRecord &parms, const String &item,
138 const Float &default_val, Array<Float> &value);
139template Bool getParm(const GlishRecord &parms, const String &item,
140 const Double &default_val, Array<Double> &value);
141template Bool getParm(const GlishRecord &parms, const String &item,
142 const Complex &default_val, Array<Complex> &value);
143template Bool getParm(const GlishRecord &parms, const String &item,
144 const String &default_val, Array<String> &value);
145
146template<class T>
147Bool getParm(const GlishRecord &parms,
148 const String &item,
149 const Array<T> &default_val,
150 Array<T> &value)
151{
152 if (parms.exists(item)) {
153 GlishArray tmp = parms.get(item);
154 tmp.get(value);
155 return True;
156 } else {
157 value.assign(default_val);
158 return False;
159 }
160}
161
162template Bool getParm(const GlishRecord &parms, const String &item,
163 const Array<Bool> &default_val, Array<Bool> &value);
164template Bool getParm(const GlishRecord &parms, const String &item,
165 const Array<Int> &default_val, Array<Int> &value);
166template Bool getParm(const GlishRecord &parms, const String &item,
167 const Array<Float> &default_val, Array<Float> &value);
168template Bool getParm(const GlishRecord &parms, const String &item,
169 const Array<Double> &default_val, Array<Double> &value);
170template Bool getParm(const GlishRecord &parms, const String &item,
171 const Array<Complex> &default_val, Array<Complex> &value);
172template Bool getParm(const GlishRecord &parms, const String &item,
173 const Array<String> &default_val, Array<String> &value);
174
175//----------------------------------------------------------------- logMessage
176
177// Log message.
178
179void logMessage(String msg)
180{
181 g_msg.add("message", msg);
182 g_msg.add("priority", "NORMAL");
183
184 g_glishStream->postEvent("log", g_msg);
185}
186
187void logMessage(String msg, uInt val, String suffix)
188{
189 logMessage(msg, Int(val), suffix);
190}
191
192void logMessage(String msg, Int val, String suffix)
193{
194 ostringstream buf;
195
196 buf << msg << val;
197 if (suffix != "") {
198 buf << suffix;
199 }
200
201 logMessage(String(buf.str()));
202}
203
204void logMessage(String msg, Double val, String suffix)
205{
206 ostringstream buf;
207
208 buf << msg << val;
209 if (suffix != "") {
210 buf << suffix;
211 }
212
213 logMessage(String(buf.str()));
214}
215
216//----------------------------------------------------------------- logWarning
217
218// Log warning.
219
220void logWarning(String warn)
221{
222 g_msg.add("message", warn);
223 g_msg.add("priority", "WARN");
224
225 g_glishStream->postEvent("log", g_msg);
226}
227
228//------------------------------------------------------------------- logError
229
230// Log error.
231
232void logError(String err)
233{
234 g_msg.add("message", err);
235 g_msg.add("priority", "SEVERE");
236
237 g_glishStream->postEvent("log", g_msg);
238}
Note: See TracBrowser for help on using the repository browser.