00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <iostream>
00023 #include <OPENR/OObserver.h>
00024 #include <opensdkAPI.h>
00025 #include <ModuleData.h>
00026
00027 using namespace std;
00028
00029 #define FETCH_ITERATOR(ret) \
00030 SubjectConstIterator it = findSubject(id); \
00031 if (it == end()) { return ret; }
00032
00033
00034 OObserver::OObserver(void) {
00035
00036 d_skip_ = 0;
00037 d_min_ = 1;
00038 d_max_ = 1;
00039 }
00040
00041 OObserver::~OObserver() {
00042
00043 }
00044
00045 OStatus OObserver::SetNotifyEntry(const OServiceEntry& entry)
00046 {
00047 myID_.Set(entry.GetOID(), entry.GetSelector());
00048
00049 for (std::list< std::pair<int,int> >::iterator it = MOD_DATA(ConnectHandlers).begin(); it != MOD_DATA(ConnectHandlers).end(); ++it) {
00050 if (it->second == 0) {
00051 it->second = entry.GetSelector();
00052 return oSUCCESS;
00053 }
00054 }
00055
00056 return oFAIL;
00057 }
00058
00059 OStatus OObserver::ConnectHandler(const OConnectMessage& msg, OStatus status)
00060 {
00061 if (status != oSUCCESS) {
00062 return status;
00063 }
00064
00065 if (msg.msgType != omsgCONNECT) {
00066 OSYSLOG1((osyslogERROR, "Unknown message type received in ConnectHandler: %d (Entry: %d/%d)", msg.msgType, (size_t)myID_.GetOID().GetAddress(), (int)myID_.GetSelector()));
00067 return oFAIL;
00068 }
00069
00070 switch (msg.command) {
00071 case oconnectcmdCONNECT:
00072 {
00073 OSubjectInfo info(msg.entry, objcommVERSION22);
00074 subjects_.push_back(info);
00075 }
00076 break;
00077
00078 case oconnectcmdDISCONNECT:
00079 for (SubjectIterator it = subjects_.begin(); it != subjects_.end(); ++it) {
00080 if (it->GetSubjectID().GetOID() == msg.entry.GetOID() &&
00081 it->GetSubjectID().GetSelector() == msg.entry.GetSelector()) {
00082 subjects_.erase(it);
00083 break;
00084 }
00085 }
00086 return oFAIL;
00087
00088 case oconnectcmdDISCONNECTALL:
00089 subjects_.clear();
00090 break;
00091
00092 default:
00093 OSYSLOG1((osyslogERROR, "Unknown message command received in ConnectHandler: %d", msg.command));
00094 return oFAIL;
00095 }
00096
00097 return oSUCCESS;
00098 }
00099
00100 OStatus OObserver::NotifyHandler(const ONotifyMessage& msg, ONotifyEvent* pEvent)
00101 {
00102 if (msg.msgType != omsgNOTIFY && msg.msgType != omsgNOTIFY_V21 && msg.msgType != omsgNOTIFY_V22) {
00103 OSYSLOG1((osyslogERROR, "Unknown message type received in NotifyHandler: %d (Entry: %d/%d)", msg.msgType, (size_t)myID_.GetOID().GetAddress(), (int)myID_.GetSelector()));
00104 return oFAIL;
00105 }
00106
00107 pEvent->SetNumOfData(msg.NumberOfData());
00108 pEvent->SetNumOfNotify(msg.NumberOfNotify());
00109 pEvent->SetSubjectInfo(OSubjectInfo(msg.subjectID, objcommVERSION22));
00110
00111 for (int i = 0; i < msg.NumberOfData(); ++i) {
00112 pEvent->SetData(i, msg.data[i].srcRCR);
00113 }
00114
00115 return oSUCCESS;
00116 }
00117
00118 int OObserver::NumberOfSubjects(void) const
00119 {
00120 return subjects_.size();
00121 }
00122
00123 OStatus OObserver::HandleAddObserver(const OConnectMessage& msg)
00124 {
00125
00126 return oFAIL;
00127 }
00128
00129 OStatus OObserver::HandleRemoveObserver(const OConnectMessage& msg)
00130 {
00131
00132 return oFAIL;
00133 }
00134
00135 OStatus OObserver::SendControlMessage(const OConnectMessage& msg, ObjcommCommand command, OControlReplyMessage* replyMsg)
00136 {
00137
00138 return oFAIL;
00139 }
00140
00141 OStatus OObserver::SendSelfControlMessage(const OConnectMessage& msg, ObjcommCommand command, OControlReplyMessage* replyMsg)
00142 {
00143 return SendControlMessage(msg, command, replyMsg);
00144 }
00145
00146 OStatus OObserver::SendReadyAll(int cmd)
00147 {
00148 for (SubjectConstIterator it = begin(); it != end(); ++it){
00149 if (SendReady(cmd, *it) != oSUCCESS) {
00150 return oFAIL;
00151 }
00152 }
00153
00154 return oSUCCESS;
00155 }
00156
00157 OStatus OObserver::SendReady(int cmd, const SubjectID& id)
00158 {
00159 FETCH_ITERATOR(oFAIL)
00160 return SendReady(cmd, *it);
00161 }
00162
00163 OStatus OObserver::SendReady(int cmd, const OSubjectInfo& info)
00164 {
00165 OReadyMessage *msg = new OReadyMessage;
00166 msg->command = cmd;
00167 msg->observerID = myID_;
00168
00169 return _sendMessage(info.GetSubjectID(), msg);
00170 }
00171
00172 OStatus OObserver::SendReadyV22(int cmd, const SubjectID& id)
00173 {
00174 return SendReady(cmd, id);
00175 }
00176
00177 OStatus OObserver::SendReadyV10(int cmd, const SubjectID& id)
00178 {
00179 return SendReady(cmd, id);
00180 }
00181
00182 OStatus OObserver::NotifyHandlerV22(const ONotifyMessage& msg, ONotifyEvent* pEvent)
00183 {
00184 return NotifyHandler(msg, pEvent);
00185 }
00186
00187 SubjectConstIterator OObserver::findSubject(const ObserverID& id) const
00188 {
00189 for (SubjectConstIterator it = begin(); it != end(); ++it){
00190 if (it->GetSubjectID() == id) {
00191 return it;
00192 }
00193 }
00194
00195 return end();
00196 }
00197
00198 SubjectIterator OObserver::findSubject(const SubjectID& id)
00199 {
00200 for (SubjectIterator it = subjects_.begin(); it != subjects_.end(); ++it){
00201 if (it->GetSubjectID() == id) {
00202 return it;
00203 }
00204 }
00205
00206 return subjects_.end();
00207 }