source: trunk/FACT++/src/MessageDim.cc@ 11328

Last change on this file since 11328 was 11271, checked in by tbretz, 13 years ago
Changed default for fDimMessage from en empty string to a NULL pointer.
File size: 5.9 KB
Line 
1#include "MessageDim.h"
2
3#include "tools.h"
4#include "Time.h"
5
6using namespace std;
7
8// **************************************************************************
9/** @class MessageDimTX
10
11@brief Based on MessageImp, redirects log-output to a Dim service MESSAGE
12
13This is a special DimService which offers SERVER/MESSAGE to the DimNetwork
14and redirects output issued via its base-class MessageImp to the Dim
15service. The severity of the message is send as qualiy of service of
16the service message.
17
18@section Examples
19
20 - A simple and usefull example can be found in \ref log.cc and \ref logtime.cc
21
22**/
23// **************************************************************************
24
25// --------------------------------------------------------------------------
26//
27//! Constructs a DimService with the name SERVER/MESSAGE. And passes the
28//! given ostream down to the MessageImp base.
29//!
30//! @param name
31//! Name of the message server to which we want to subscribe, e.g. DRIVE
32//!
33//! @param out
34//! ostream passed to MessageImp. It is used to redirect the output to.
35//
36MessageDimTX::MessageDimTX(const std::string &name, std::ostream &out)
37 : DimDescribedService((name + "/MESSAGE").c_str(), const_cast<char*>(""),
38 "A general logging service providing a quality of service (severity)"
39 "|Message[string]:The message"),
40MessageImp(out), fDebug(false)
41{
42 // This is a message which will never arrive because
43 // the time to establish a client-sever connection is
44 // too short.
45 Message("MessageDimTX started.");
46}
47
48// --------------------------------------------------------------------------
49//
50//!
51//
52MessageDimTX::~MessageDimTX()
53{
54 Message("MessageDimTX shutting down..");
55}
56
57// --------------------------------------------------------------------------
58//
59//! First calls MessageImp::Write to output the message tobe transmitted
60//! also to a local logging stream. Then the Dim service is updated.
61//! If sending of the message failed a message is written to the
62//! logging stream stored in MessageImp. It is intentionally not
63//! output through Update to make it look different than usual
64//! transmitted messages.
65//
66int MessageDimTX::Write(const Time &t, const string &txt, int qos)
67{
68 MessageImp::Write(t, txt, qos);
69
70
71 // We cannot use our own mutex here because it can create dead-locks
72 // in a hand-shake with the global dim-mutex if a service is
73 // updated from within a dimHandler (dim-mutex already locked)
74 // and at the same time another thread tries to lock the mutex.
75 //
76 // Thread 1: Lock global Dim mutex and call infoHandler
77 //
78 // Thread 2: CALL Write
79 // Thread 2: LOCK Write-mutex
80 // Thread 2: setQuality will try to lock global Dim mutex
81 // (since Thread1!=Thread2 this results in a wait)
82 //
83 // Thread 1: CALL Write from within the infoHandler
84 // Thread 1: LOCK Write-mutex
85 // (since Thread2 now waits for the infoHandler to finish
86 // and the infoHandler has to wait for the Write-mutex
87 // we have a dead-lock)
88 //
89 dim_lock();
90
91 // We have to use setData to make sure the DimService will
92 // hold a local copy of the data.
93 setData(const_cast<char*>(txt.c_str()));
94 setQuality(qos);
95
96 const int rc = updateService();
97
98 dim_unlock();
99
100 if (rc==0 && fDebug)
101 Out() << " !! " << t.GetAsStr() << " - Sending failed!" << endl;
102
103 return rc;
104}
105
106// **************************************************************************
107/** @class MessageDimRX
108
109@brief Based on MessageImp, subscribes to a MESSAGE service in the Dim network
110
111This is a special DimInfoHandler. It subscribes to a service SERVER/MESSAGE
112on the DimNetwork and redirects all received output to its base class
113MessageImp view MessageImp::Write. the quality of service received with
114each service update is passed as severity.
115
116@section Examples
117
118 - A simple and usefull example can be found in \ref log.cc and \ref logtime.cc
119
120 @todo Maybe it is not a good idea that MessageImp is a base class,
121 maybe it should be a reference given in the constructor
122
123**/
124// **************************************************************************
125
126// --------------------------------------------------------------------------
127//
128//! Setup a DimStamedInfo service subscription for SERVER/MESSAGE
129//!
130//! @param name
131//! the name of the SERVER
132//!
133//! @param imp
134//! A reference to MessageImo to which messages will be redirected
135//
136MessageDimRX::MessageDimRX(const std::string &name, MessageImp &imp)
137: fMinLogLevel(0), fConnected(false), fMsg(imp),
138fDimMessage((name+"/MESSAGE").c_str(), (void*)NULL, 0, this)
139{
140}
141
142// --------------------------------------------------------------------------
143//
144//! If the server has been disconnected write a simple log-message.
145//! Skip all received messages which have a severity smaller than
146//! fMinLogLevel. Write any other message with MessageImp::Write.
147//
148void MessageDimRX::infoHandler()
149{
150 if (getInfo()!=&fDimMessage)
151 return;
152
153 const string name = fDimMessage.getName();
154 const string server = name.substr(0, name.find_first_of('/'));
155
156 fConnected = fDimMessage.getSize()!=0;
157
158 // The server is diconnected. Do nothing
159 if (!fConnected)
160 {
161 // We cannot print this message because it is produced by
162 // every server which doesn't have the MESSAGE service, too.
163 //fMsg.Message(server+": Disconnected.");
164 return;
165 }
166
167 // skip all messages with a severity smaller than the minimum log level
168 if (fDimMessage.getQuality()<fMinLogLevel)
169 return;
170
171 stringstream msg;
172 msg << server << ": " << fDimMessage.getString();
173
174 // Make sure getTimestamp is called _before_ getTimestampMillisecs
175 // Must be in exactly this order!
176 const int tsec = fDimMessage.getTimestamp();
177 const int tms = fDimMessage.getTimestampMillisecs();
178
179 // Write the received message to the output
180 fMsg.Write(Time(tsec, tms*1000), msg.str().c_str(), fDimMessage.getQuality());
181}
Note: See TracBrowser for help on using the repository browser.