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

Last change on this file since 17230 was 17230, checked in by tbretz, 11 years ago
Use Queue from externals instead of src.
File size: 5.4 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"),
40 MessageImp(out), fDebug(false),
41 fMsgQueue(std::bind(&MessageDimTX::UpdateService, this, placeholders::_1))
42{
43 // This is a message which will never arrive because
44 // the time to establish a client-sever connection is
45 // too short.
46 Message("MessageDimTX started.");
47}
48
49// --------------------------------------------------------------------------
50//
51//!
52//
53MessageDimTX::~MessageDimTX()
54{
55 // Everything here will never be sent by dim because the
56 // dim services have been stopped already. This is necessary,
57 // to have them available already during startup
58 Message("MessageDimTX shutting down ["+to_string(fMsgQueue.size())+"]");
59 fMsgQueue.wait();
60}
61
62bool MessageDimTX::UpdateService(const tuple<Time,string,int> &data)
63{
64 setData(get<1>(data));
65 setQuality(get<2>(data));
66
67 const int rc = DimDescribedService::Update(get<0>(data));
68 if (rc==0 && fDebug)
69 Out() << " !! " << get<0>(data).GetAsStr() << " - Sending failed!" << endl;
70
71 return true;
72}
73
74// --------------------------------------------------------------------------
75//
76//! First calls MessageImp::Write to output the message tobe transmitted
77//! also to a local logging stream. Then the Dim service is updated.
78//! If sending of the message failed a message is written to the
79//! logging stream stored in MessageImp. It is intentionally not
80//! output through Update to make it look different than usual
81//! transmitted messages.
82//
83int MessageDimTX::Write(const Time &t, const string &txt, int qos)
84{
85 MessageImp::Write(t, txt, qos);
86 fMsgQueue.emplace(t, txt, qos);
87 return 1;
88}
89
90// **************************************************************************
91/** @class MessageDimRX
92
93@brief Based on MessageImp, subscribes to a MESSAGE service in the Dim network
94
95This is a special DimInfoHandler. It subscribes to a service SERVER/MESSAGE
96on the DimNetwork and redirects all received output to its base class
97MessageImp view MessageImp::Write. the quality of service received with
98each service update is passed as severity.
99
100@section Examples
101
102 - A simple and usefull example can be found in \ref log.cc and \ref logtime.cc
103
104 @todo Maybe it is not a good idea that MessageImp is a base class,
105 maybe it should be a reference given in the constructor
106
107**/
108// **************************************************************************
109
110// --------------------------------------------------------------------------
111//
112//! Setup a DimStamedInfo service subscription for SERVER/MESSAGE
113//!
114//! @param name
115//! the name of the SERVER
116//!
117//! @param imp
118//! A reference to MessageImo to which messages will be redirected
119//
120MessageDimRX::MessageDimRX(const std::string &name, MessageImp &imp)
121: fMinLogLevel(0), fConnected(false), fMsg(imp),
122fDimMessage((name+"/MESSAGE").c_str(), (void*)NULL, 0, this)
123{
124}
125
126// --------------------------------------------------------------------------
127//
128//! If the server has been disconnected write a simple log-message.
129//! Skip all received messages which have a severity smaller than
130//! fMinLogLevel. Write any other message with MessageImp::Write.
131//
132void MessageDimRX::infoHandler()
133{
134 if (getInfo()!=&fDimMessage)
135 return;
136
137 const string name = fDimMessage.getName();
138 const string server = name.substr(0, name.find_first_of('/'));
139
140 fConnected = fDimMessage.getSize()!=0;
141
142 // The server is diconnected. Do nothing
143 if (!fConnected)
144 {
145 // We cannot print this message because it is produced by
146 // every server which doesn't have the MESSAGE service, too.
147 //fMsg.Message(server+": Disconnected.");
148 return;
149 }
150
151 // skip all messages with a severity smaller than the minimum log level
152 if (fDimMessage.getQuality()<fMinLogLevel)
153 return;
154
155 const string msg = server+": "+fDimMessage.getString();
156
157 // Make sure getTimestamp is called _before_ getTimestampMillisecs
158 // Must be in exactly this order!
159 const int tsec = fDimMessage.getTimestamp();
160 const int tms = fDimMessage.getTimestampMillisecs();
161
162 // Write the received message to the output
163 fMsg.Write(Time(tsec, tms*1000), msg, fDimMessage.getQuality());
164}
Note: See TracBrowser for help on using the repository browser.