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

Last change on this file since 10422 was 10422, checked in by tbretz, 9 years ago
Fixed the millisecond part of the time, the Time class expects microseconds; some changes to the order of the data members to ensure a proper destruction.
File size: 4.7 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: DimService((name + "/MESSAGE").c_str(), const_cast<char*>("")),
38MessageImp(out), fDebug(false)
39{
40    // This is a message which will never arrive because
41    // the time to establish a client-sever connection is
42    // too short.
43    Message("MessageDimTX started.");
44}
45
46// --------------------------------------------------------------------------
47//
48//!
49//
50MessageDimTX::~MessageDimTX()
51{
52    Message("MessageDimTX shutting down..");
53}
54
55// --------------------------------------------------------------------------
56//
57//! First calls MessageImp::Write to output the message tobe transmitted
58//! also to a local logging stream. Then the Dim service is updated.
59//! If sending of the message failed a message is written to the
60//! logging stream stored in MessageImp. It is intentionally not
61//! output through Update to make it look different than usual
62//! transmitted messages.
63//
64int MessageDimTX::Write(const Time &t, const string &txt, int qos)
65{
66    MessageImp::Write(t, txt, qos);
67
68    // We have to use setData to make sure the DimService will
69    // hold a local copy of the data.
70    setQuality(qos);
71    setData(const_cast<char*>(txt.c_str()));
72    const int rc = updateService();
73
74    if (rc==0 && fDebug)
75        Out() << " !! " << t.GetAsStr() << " - Sending failed!" << endl;
76
77    return rc;
78}
79
80
81
82// **************************************************************************
83/** @class MessageDimRX
84
85@brief Based on MessageImp, subscribes to a MESSAGE service in the Dim network
86
87This is a special DimInfoHandler. It subscribes to a service SERVER/MESSAGE
88on the DimNetwork and redirects all received output to its base class
89MessageImp view MessageImp::Write. the quality of service received with
90each service update is passed as severity.
91
92@section Examples
93
94 - A simple and usefull example can be found in \ref log.cc and \ref logtime.cc
95
96 @todo Maybe it is not a good idea that MessageImp is a base class,
97 maybe it should be a reference given in the constructor
98
99**/
100// **************************************************************************
101
102// --------------------------------------------------------------------------
103//
104//! Setup a DimStamedInfo service subscription for SERVER/MESSAGE
105//!
106//! @param name
107//!    the name of the SERVER
108//!
109//! @param imp
110//!    A reference to MessageImo to which messages will be redirected
111//
112MessageDimRX::MessageDimRX(const std::string &name, MessageImp &imp)
113: fMinLogLevel(0), fConnected(false), fMsg(imp),
114fDimMessage((name+"/MESSAGE").c_str(), const_cast<char*>(""), this)
115{
116}
117
118// --------------------------------------------------------------------------
119//
120//! If the server has been disconnected write a simple log-message.
121//! Skip all received messages which have a severity smaller than
122//! fMinLogLevel. Write any other message with MessageImp::Write.
123//
124void MessageDimRX::infoHandler()
125{
126    if (getInfo()!=&fDimMessage)
127        return;
128
129    // The server is diconnected. Do nothing
130    if (getInfo()->getTimestamp()==0 || getInfo()->getSize()==1)
131    {
132        fConnected=false;
133        fMsg.Message("Disconnected.");
134        return;
135    }
136
137    fConnected=true;
138
139    // skip all messages with a severity smaller than the minimum log level
140    if (getInfo()->getQuality()<fMinLogLevel)
141        return;
142
143    const string n = getInfo()->getName();
144
145    stringstream msg;
146    msg << n.substr(0, n.find_first_of('/')) << ": " << getInfo()->getString();
147
148    // Write the received message to the output
149    fMsg.Write(Time(getInfo()->getTimestamp(), getInfo()->getTimestampMillisecs()*1000),
150               msg.str().c_str(), getInfo()->getQuality());
151}
Note: See TracBrowser for help on using the repository browser.