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 char *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));
|
---|
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 | : fMsg(imp),
|
---|
114 | fDimMessage(Form("%s/MESSAGE", name.c_str()).c_str(), const_cast<char*>(""), this)
|
---|
115 | {
|
---|
116 | fMinLogLevel = 0;
|
---|
117 | }
|
---|
118 |
|
---|
119 | // --------------------------------------------------------------------------
|
---|
120 | //
|
---|
121 | //! If the server has been disconnected write a simple log-message.
|
---|
122 | //! Skip all received messages which have a severity smaller than
|
---|
123 | //! fMinLogLevel. Write any other message with MessageImp::Write.
|
---|
124 | //
|
---|
125 | void MessageDimRX::infoHandler()
|
---|
126 | {
|
---|
127 | if (getInfo()!=&fDimMessage)
|
---|
128 | return;
|
---|
129 |
|
---|
130 | // The server is diconnected. Do nothing
|
---|
131 | if (getInfo()->getTimestamp()==0)
|
---|
132 | {
|
---|
133 | fMsg.Message("Disconnected.");
|
---|
134 | return;
|
---|
135 | }
|
---|
136 |
|
---|
137 | // skip all messages with a severity smaller than the minimum log level
|
---|
138 | if (getInfo()->getQuality()<fMinLogLevel)
|
---|
139 | return;
|
---|
140 |
|
---|
141 | const string n = getInfo()->getName();
|
---|
142 |
|
---|
143 | stringstream msg;
|
---|
144 | msg << n.substr(0, n.find_first_of('/')) << ": " << getInfo()->getString();
|
---|
145 |
|
---|
146 | // Write the received message to the output
|
---|
147 | fMsg.Write(Time(getInfo()->getTimestamp(), getInfo()->getTimestampMillisecs()),
|
---|
148 | msg.str().c_str(), getInfo()->getQuality());
|
---|
149 | }
|
---|