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