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

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