1 | #include "MessageDim.h"
|
---|
2 |
|
---|
3 | #include "tools.h"
|
---|
4 | #include "Time.h"
|
---|
5 |
|
---|
6 | using namespace std;
|
---|
7 |
|
---|
8 | // **************************************************************************
|
---|
9 | /** @class MessageDimTX
|
---|
10 |
|
---|
11 | @brief Based on MessageImp, redirects log-output to a Dim service MESSAGE
|
---|
12 |
|
---|
13 | This is a special DimService which offers SERVER/MESSAGE to the DimNetwork
|
---|
14 | and redirects output issued via its base-class MessageImp to the Dim
|
---|
15 | service. The severity of the message is send as qualiy of service of
|
---|
16 | the 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 | //
|
---|
36 | MessageDimTX::MessageDimTX(const std::string &name, std::ostream &out)
|
---|
37 | : DimService((name + "/MESSAGE").c_str(), const_cast<char*>("")),
|
---|
38 | MessageImp(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 | //
|
---|
50 | MessageDimTX::~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 | //
|
---|
64 | int 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 |
|
---|
87 | This is a special DimInfoHandler. It subscribes to a service SERVER/MESSAGE
|
---|
88 | on the DimNetwork and redirects all received output to its base class
|
---|
89 | MessageImp view MessageImp::Write. the quality of service received with
|
---|
90 | each 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 | //
|
---|
112 | MessageDimRX::MessageDimRX(const std::string &name, MessageImp &imp)
|
---|
113 | : fMinLogLevel(0), fConnected(false), fMsg(imp),
|
---|
114 | fDimMessage((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 | //
|
---|
124 | void 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 | }
|
---|