defuze.me
Client
|
00001 /************************************************************************** 00002 ** defuze.me Epitech Innovative Project 00003 ** 00004 ** Copyright 2010-2011 00005 ** Athena Calmettes - Jocelyn De La Rosa - Francois Gaillard 00006 ** Adrien Jarthon - Alexandre Moore - Luc Peres - Arnaud Sellier 00007 ** 00008 ** All rights reserved. 00009 **************************************************************************/ 00010 00011 #include "remoteevent.hpp" 00012 00013 using namespace Network; 00014 00015 RemoteEvent::RemoteEvent(RemoteSock *remote, const QByteArray& packet) : remote(remote) 00016 { 00017 if (packet.size() > 0) 00018 { 00019 QVariant json = remote->eventParser().parse(packet); 00020 event = json.toMap()["event"].toString(); 00021 uid = json.toMap()["uid"].toUInt(); 00022 replyTo = json.toMap()["replyTo"].toUInt(); 00023 if (json.toMap().contains("data")) 00024 data = json.toMap()["data"].toMap(); 00025 } 00026 else 00027 { 00028 uid = 0; 00029 replyTo = 0; 00030 } 00031 } 00032 00033 RemoteEvent::~RemoteEvent() 00034 { 00035 } 00036 00037 unsigned RemoteEvent::getUid() const 00038 { 00039 return uid; 00040 } 00041 00042 bool RemoteEvent::isReply() const 00043 { 00044 return (getRequestId() > 0); 00045 } 00046 00047 unsigned RemoteEvent::getRequestId() const 00048 { 00049 return replyTo; 00050 } 00051 00052 RemoteSock* RemoteEvent::getRemote() const 00053 { 00054 return remote; 00055 } 00056 00057 RemoteEvent* RemoteEvent::getRequest() const 00058 { 00059 return remote->events[getRequestId()]; 00060 } 00061 00062 const QString& RemoteEvent::getEvent() const 00063 { 00064 return event; 00065 } 00066 00067 void RemoteEvent::send() 00068 { 00069 remote->sendEvent(this); 00070 deleteLater(); 00071 } 00072 00073 void RemoteEvent::sendWithReply(const QObject *receiver, const char *method) 00074 { 00075 connect(this, SIGNAL(receiveReply(const RemoteEvent&)), receiver, method); 00076 remote->events[getUid()] = this; 00077 remote->sendEvent(this); 00078 } 00079 00080 void RemoteEvent::setData(const QVariantMap& data) 00081 { 00082 this->data = data; 00083 } 00084 00085 QVariant& RemoteEvent::operator[](const QString& key) 00086 { 00087 return data[key]; 00088 } 00089 00090 QVariant RemoteEvent::operator[](const QString& key) const 00091 { 00092 return data[key]; 00093 } 00094 00095 QByteArray RemoteEvent::toStream() const 00096 { 00097 QByteArray out; 00098 QVariantMap map; 00099 00100 map["event"] = getEvent(); 00101 map["uid"] = getUid(); 00102 if (isReply()) 00103 map["replyTo"] = getRequestId(); 00104 if (!data.empty()) 00105 map["data"] = data; 00106 out = remote->eventParser().serialize(map); 00107 out += '\0'; 00108 return out; 00109 } 00110 00111 RemoteEvent& RemoteEvent::newReply(const QString& name) const 00112 { 00113 RemoteEvent &event = remote->newEvent(name); 00114 00115 event.replyTo = this->uid; 00116 return event; 00117 } 00118 00119 void RemoteEvent::autoReply(AutoReplyType type) const 00120 { 00121 if (type == OK) 00122 newReply("ok").send(); 00123 if (type == NO_CHANGES) 00124 newReply("noChanges").send(); 00125 if (type == ERROR) 00126 newReply("error").send(); 00127 } 00128