00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef MESSAGEOUT_H
00017 #define MESSAGEOUT_H
00018
00019 #ifndef USE_PCH
00020 #include <sstream>
00021 #include <string>
00022 #include <boost/shared_ptr.hpp>
00023 #include "debug.h"
00024 #endif
00025
00026 #include "Priority.h"
00027 #include "Prefix.h"
00028 #include "MessageIn.h"
00029 #include "JustInTime.h"
00030 #include "PseudoMessageEvent.h"
00031
00032
00033 enum msg_type {
00034 unsafe_msg,
00035 safe_query
00036 };
00037
00038
00039 struct MessageOutData {
00040 Priority M_priority;
00041 msg_type M_type;
00042 std::vector<boost::shared_ptr<JustInTimeToken> > M_tokens;
00043 uint32_t M_sequence_number;
00044 static uint32_t S_sequence_number;
00045
00046
00047 MessageOutData(Priority const& priority, msg_type type) : M_priority(priority), M_type(type), M_sequence_number(S_sequence_number++)
00048 { Dout(dc::objects, "Constructing MessageOutData(" << priority << ")"); }
00049
00050 virtual ~MessageOutData() { Dout(dc::objects, "Destructing MessageOutData " << *this << ", sequence number " << M_sequence_number); }
00051
00052
00053 virtual bool is_pseudo_message(void) const { return false; }
00054
00055
00056 bool is_safe(void) const { return M_type == safe_query; }
00057 };
00058
00059
00060
00061 struct PseudoMessageOutData : public MessageOutData {
00062 PseudoMessageEventServer M_pseudo_message_event_server;
00063 PseudoMessageEventType M_request_data;
00064
00065
00066 PseudoMessageOutData(Priority const& priority, msg_type type) : MessageOutData(priority, type) { }
00067
00068
00069 virtual bool is_pseudo_message(void) const { return true; }
00070
00071
00072 PseudoMessageEventType const& request_data(void) const { return M_request_data; }
00073 };
00074
00075
00076
00077
00078
00079 class PseudoMessageOut {
00080 private:
00081 boost::shared_ptr<PseudoMessageOutData> M_data;
00082 public:
00083
00084 PseudoMessageOut(Priority const& priority, msg_type type = unsafe_msg) : M_data(NEW(PseudoMessageOutData(priority, type))) { }
00085 boost::shared_ptr<MessageOutData> data(void) const { return boost::static_pointer_cast<MessageOutData>(M_data); }
00086 PseudoMessageEventServer& pseudo_message_event_server(void) { return M_data->M_pseudo_message_event_server; }
00087 };
00088
00089 #define writeEventRequest pseudo_message_event_server()
00090
00091
00092
00093
00094
00095
00096 class MessageOut {
00097 private:
00098 boost::shared_ptr<MessageOutData> M_data;
00099
00100 public:
00101
00102 MessageOut(Priority const& priority, msg_type type = unsafe_msg) : M_data(NEW(MessageOutData(priority, type))) { }
00103
00104 MessageOut(PseudoMessageOut const& pseudo_message_out) : M_data(pseudo_message_out.data()) { }
00105
00106 MessageOut(MessageOut const& message) : M_data(message.M_data) { }
00107
00108
00109 template<class SESSION>
00110 void write_to(std::ostream& os, SESSION const& session) const;
00111
00112 void write_to(ClientSession& session) const;
00113 void write_to(ServerSession& session) const;
00114
00115
00116 Priority const& priority(void) const { return M_data->M_priority; }
00117
00118
00119 bool is_pseudo_message(void) const { return M_data->is_pseudo_message(); }
00120
00121
00122 bool is_safe(void) const { return M_data->is_safe(); }
00123
00124
00125 friend bool operator<(MessageOut const& msg1, MessageOut const& msg2)
00126 {
00127 return msg1.M_data->M_priority < msg2.M_data->M_priority ||
00128 (msg1.M_data->M_priority == msg2.M_data->M_priority &&
00129 msg1.M_data->M_sequence_number > msg2.M_data->M_sequence_number);
00130 }
00131
00132 friend MessageOut& operator<<(MessageOut& message_out, Prefix const& prefix);
00133 friend MessageOut& operator<<(MessageOut& message_out, int command);
00134 friend MessageOut& operator<<(MessageOut& message_out, long number);
00135 friend MessageOut& operator<<(MessageOut& message_out, std::string const& str);
00136 friend MessageOut& operator<<(MessageOut& message_out, char c);
00137 friend MessageOut& operator<<(MessageOut& message_out, boost::shared_ptr<Target> const& target);
00138 friend MessageOut& operator<<(MessageOut& message_out, MsgPart const& part);
00139 friend MessageOut& operator<<(MessageOut& message_out, JustInTimeCatenate const& to_be_catenated_token);
00140 friend MessageOut& operator<<(MessageOut& message_out, JustInTimeLast const& to_be_appended_token);
00141
00142
00143 friend std::ostream& operator<<(std::ostream& os, MessageOut const& message);
00144 };
00145
00146 #endif // MESSAGEOUT_H