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

Last change on this file since 15211 was 15088, checked in by tbretz, 12 years ago
Fixed a typo in a log-message.
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", const_cast<char*>("C"),
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 setQuality(qos);
94
95 const int rc = DimDescribedService::Update(txt);
96
97 dim_unlock();
98
99 if (rc==0 && fDebug)
100 Out() << " !! " << t.GetAsStr() << " - Sending failed!" << endl;
101
102 return rc;
103}
104
105// **************************************************************************
106/** @class MessageDimRX
107
108@brief Based on MessageImp, subscribes to a MESSAGE service in the Dim network
109
110This is a special DimInfoHandler. It subscribes to a service SERVER/MESSAGE
111on the DimNetwork and redirects all received output to its base class
112MessageImp view MessageImp::Write. the quality of service received with
113each service update is passed as severity.
114
115@section Examples
116
117 - A simple and usefull example can be found in \ref log.cc and \ref logtime.cc
118
119 @todo Maybe it is not a good idea that MessageImp is a base class,
120 maybe it should be a reference given in the constructor
121
122**/
123// **************************************************************************
124
125// --------------------------------------------------------------------------
126//
127//! Setup a DimStamedInfo service subscription for SERVER/MESSAGE
128//!
129//! @param name
130//! the name of the SERVER
131//!
132//! @param imp
133//! A reference to MessageImo to which messages will be redirected
134//
135MessageDimRX::MessageDimRX(const std::string &name, MessageImp &imp)
136: fMinLogLevel(0), fConnected(false), fMsg(imp),
137fDimMessage((name+"/MESSAGE").c_str(), (void*)NULL, 0, this)
138{
139}
140
141// --------------------------------------------------------------------------
142//
143//! If the server has been disconnected write a simple log-message.
144//! Skip all received messages which have a severity smaller than
145//! fMinLogLevel. Write any other message with MessageImp::Write.
146//
147void MessageDimRX::infoHandler()
148{
149 if (getInfo()!=&fDimMessage)
150 return;
151
152 const string name = fDimMessage.getName();
153 const string server = name.substr(0, name.find_first_of('/'));
154
155 fConnected = fDimMessage.getSize()!=0;
156
157 // The server is diconnected. Do nothing
158 if (!fConnected)
159 {
160 // We cannot print this message because it is produced by
161 // every server which doesn't have the MESSAGE service, too.
162 //fMsg.Message(server+": Disconnected.");
163 return;
164 }
165
166 // skip all messages with a severity smaller than the minimum log level
167 if (fDimMessage.getQuality()<fMinLogLevel)
168 return;
169
170 const string msg = server+": "+fDimMessage.getString();
171
172 // Make sure getTimestamp is called _before_ getTimestampMillisecs
173 // Must be in exactly this order!
174 const int tsec = fDimMessage.getTimestamp();
175 const int tms = fDimMessage.getTimestampMillisecs();
176
177 // Write the received message to the output
178 fMsg.Write(Time(tsec, tms*1000), msg, fDimMessage.getQuality());
179}
Note: See TracBrowser for help on using the repository browser.