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

Last change on this file since 10491 was 10491, checked in by tbretz, 9 years ago
Added some comment.
File size: 5.0 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    // We have to use setData to make sure the DimService will
71    // hold a local copy of the data.
72    setQuality(qos);
73    setData(const_cast<char*>(txt.c_str()));
74    const int rc = updateService();
75
76    if (rc==0 && fDebug)
77        Out() << " !! " << t.GetAsStr() << " - Sending failed!" << endl;
78
79    return rc;
80}
81
82
83
84// **************************************************************************
85/** @class MessageDimRX
86
87@brief Based on MessageImp, subscribes to a MESSAGE service in the Dim network
88
89This is a special DimInfoHandler. It subscribes to a service SERVER/MESSAGE
90on the DimNetwork and redirects all received output to its base class
91MessageImp view MessageImp::Write. the quality of service received with
92each service update is passed as severity.
93
94@section Examples
95
96 - A simple and usefull example can be found in \ref log.cc and \ref logtime.cc
97
98 @todo Maybe it is not a good idea that MessageImp is a base class,
99 maybe it should be a reference given in the constructor
100
101**/
102// **************************************************************************
103
104// --------------------------------------------------------------------------
105//
106//! Setup a DimStamedInfo service subscription for SERVER/MESSAGE
107//!
108//! @param name
109//!    the name of the SERVER
110//!
111//! @param imp
112//!    A reference to MessageImo to which messages will be redirected
113//
114MessageDimRX::MessageDimRX(const std::string &name, MessageImp &imp)
115: fMinLogLevel(0), fConnected(false), fMsg(imp),
116fDimMessage((name+"/MESSAGE").c_str(), const_cast<char*>(""), 0, this)
117{
118}
119
120// --------------------------------------------------------------------------
121//
122//! If the server has been disconnected write a simple log-message.
123//! Skip all received messages which have a severity smaller than
124//! fMinLogLevel. Write any other message with MessageImp::Write.
125//
126void MessageDimRX::infoHandler()
127{
128    if (getInfo()!=&fDimMessage)
129        return;
130
131    const string name   = getInfo()->getName();
132    const string server = name.substr(0, name.find_first_of('/'));
133
134    fConnected = getInfo()->getSize()!=0;
135
136    // The server is diconnected. Do nothing
137    if (!fConnected)
138    {
139        // We cannot print this message because it is produced by
140        // every server which doesn't have the MESSAGE service, too.
141        //fMsg.Message(server+": Disconnected.");
142        return;
143    }
144
145    // skip all messages with a severity smaller than the minimum log level
146    if (getInfo()->getQuality()<fMinLogLevel)
147        return;
148
149    stringstream msg;
150    msg << server << ": " << getInfo()->getString();
151
152    // Write the received message to the output
153    fMsg.Write(Time(getInfo()->getTimestamp(), getInfo()->getTimestampMillisecs()*1000),
154               msg.str().c_str(), getInfo()->getQuality());
155}
Note: See TracBrowser for help on using the repository browser.