00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef USE_PCH
00017 #include "sys.h"
00018 #include "debug.h"
00019 #include <iostream>
00020 #endif
00021
00022 #include "MessageOut.h"
00023 #include "JustInTime.h"
00024 #include "Target.h"
00025 #include "ClientSession.h"
00026 #include "ServerSession.h"
00027
00028 uint32_t MessageOutData::S_sequence_number;
00029
00030 MessageOut& operator<<(MessageOut& message_out, Prefix const& prefix)
00031 {
00032 if (!message_out.M_data->M_tokens.empty())
00033 message_out.M_data.reset(NEW(MessageOutData(message_out.M_data->M_priority, message_out.M_data->M_type)));
00034 if (!prefix.empty())
00035 {
00036 boost::shared_ptr<JustInTimeToken> token(NEW(JustInTimePrefix(prefix)));
00037 message_out.M_data->M_tokens.push_back(token);
00038 }
00039 return message_out;
00040 }
00041
00042 MessageOut& operator<<(MessageOut& message_out, int command)
00043 {
00044 size_t n = message_out.M_data->M_tokens.size();
00045 if (n > 1 || (n == 1 && !message_out.M_data->M_tokens[0]->is_prefix()))
00046 message_out.M_data.reset(NEW(MessageOutData(message_out.M_data->M_priority, message_out.M_data->M_type)));
00047 boost::shared_ptr<JustInTimeToken> token(NEW(JustInTimeCommand(command)));
00048 message_out.M_data->M_tokens.push_back(token);
00049 return message_out;
00050 }
00051
00052 MessageOut& operator<<(MessageOut& message_out, std::string const& str)
00053 {
00054 boost::shared_ptr<JustInTimeToken> token(NEW(JustInTimeString(str)));
00055 message_out.M_data->M_tokens.push_back(token);
00056 return message_out;
00057 }
00058
00059 MessageOut& operator<<(MessageOut& message_out, char c)
00060 {
00061 boost::shared_ptr<JustInTimeToken> token(NEW(JustInTimeChar(c)));
00062 message_out.M_data->M_tokens.push_back(token);
00063 return message_out;
00064 }
00065
00066 MessageOut& operator<<(MessageOut& message_out, long val)
00067 {
00068 boost::shared_ptr<JustInTimeToken> token(NEW(JustInTimeLong(val)));
00069 message_out.M_data->M_tokens.push_back(token);
00070 return message_out;
00071 }
00072
00073 MessageOut& operator<<(MessageOut& message_out, boost::shared_ptr<Target> const& target)
00074 {
00075 boost::shared_ptr<JustInTimeToken> token(NEW(JustInTimeTarget(target)));
00076 message_out.M_data->M_tokens.push_back(token);
00077 return message_out;
00078 }
00079
00080 MessageOut& operator<<(MessageOut& message_out, MsgPart const& part)
00081 {
00082 if (part.start())
00083 {
00084 boost::shared_ptr<JustInTimeToken> token(NEW(JustInTimeMsgPart(part)));
00085 message_out.M_data->M_tokens.push_back(token);
00086 }
00087 return message_out;
00088 }
00089
00090 MessageOut& operator<<(MessageOut& message_out, JustInTimeCatenate const& to_be_catenated_token)
00091 {
00092 boost::shared_ptr<JustInTimeToken> token(NEW(JustInTimeCatenate(to_be_catenated_token)));
00093 message_out.M_data->M_tokens.push_back(token);
00094 return message_out;
00095 }
00096
00097 MessageOut& operator<<(MessageOut& message_out, JustInTimeLast const& to_be_appended_token)
00098 {
00099 boost::shared_ptr<JustInTimeToken> token(NEW(JustInTimeLast(to_be_appended_token)));
00100 message_out.M_data->M_tokens.push_back(token);
00101 return message_out;
00102 }
00103
00104 template<class SESSION>
00105 void MessageOut::write_to(std::ostream& os, SESSION const& session) const
00106 {
00107 DoutEntering(dc::debug, "MessageOut<" << type_info_of<SESSION>().demangled_name() << ">::write_to(" << os << ", " << session << ") for " << *this);
00108 if (M_data->is_pseudo_message())
00109 boost::static_pointer_cast<PseudoMessageOutData>(M_data)->M_pseudo_message_event_server.trigger(PseudoMessageEventType());
00110 else
00111 {
00112 #ifndef CWDEBUG
00113 std::ostream& tmp(os);
00114 #else
00115 ASSERT(!os.fail());
00116 std::stringstream tmp;
00117 #endif
00118 for (std::vector<boost::shared_ptr<JustInTimeToken> >::iterator iter = M_data->M_tokens.begin(); iter != M_data->M_tokens.end(); ++iter)
00119 (*iter)->write_to(tmp, session);
00120 tmp << "\r\n";
00121 #ifdef CWDEBUG
00122 std::string result(tmp.str());
00123 os << result;
00124 Dout(dc::debug, "Wrote string \"" << buf2str(result.data(), result.size()) << "\".");
00125 #endif
00126 }
00127 }
00128
00129 void MessageOut::write_to(ClientSession& session) const
00130 {
00131 write_to(session, session);
00132 }
00133
00134 void MessageOut::write_to(ServerSession& session) const
00135 {
00136 write_to(session, session);
00137 }