Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include "jaus/core/time/timeservice.h"
00041 #include "jaus/core/events/event.h"
00042 #include "jaus/core/time/querytime.h"
00043 #include "jaus/core/time/reporttime.h"
00044 #include "jaus/core/time/settime.h"
00045 #include <iostream>
00046 #include <ctime>
00047
00048 using namespace JAUS;
00049
00050 const std::string TimeService::Name = "urn:jaus:jss:core:Time";
00051
00057 TimeService::TimeService() : AccessControl::Child(Service::ID(TimeService::Name), Service::ID(AccessControl::Name))
00058 {
00059 mpChangeTimeMethod = 0;
00060 }
00061
00062
00068 TimeService::~TimeService()
00069 {
00070 }
00071
00072
00082 bool TimeService::GenerateEvent(const Events::Subscription& info) const
00083 {
00084 if(info.mpQueryMessage->GetMessageCode() == QUERY_TIME)
00085 {
00086 ReportTime report;
00087
00088 const QueryTime* query = dynamic_cast<const QueryTime *>(info.mpQueryMessage);
00089
00090 if(query == NULL || query->IsFieldPresent(SetTime::PresenceVector::Time))
00091 {
00092 Time t;
00093 t.SetCurrentTime();
00094 report.SetTimeStamp(t);
00095 }
00096 if(query == NULL || query->IsFieldPresent(SetTime::PresenceVector::Date))
00097 {
00098 Date d;
00099
00100 time_t rawtime;
00101 tm* ptm;
00102 time(&rawtime);
00103 ptm = gmtime(&rawtime);
00104
00105 d.mYear = ptm->tm_year + 1900;
00106 d.mDay = ptm->tm_mday;
00107 d.mMonth = ptm->tm_mon;
00108 report.SetDateStamp(d);
00109 }
00110
00111 SendEvent(info, &report);
00112 return true;
00113 }
00114
00115 return false;
00116 }
00117
00118
00134 bool TimeService::IsEventSupported(const Events::Type type,
00135 const double requestedPeriodicRate,
00136 const Message* queryMessage,
00137 double& confirmedPeriodicRate,
00138 std::string& errorMessage) const
00139 {
00140
00141 if(queryMessage->GetMessageCode() == QUERY_TIME)
00142 {
00143 confirmedPeriodicRate = requestedPeriodicRate;
00144 return true;
00145 }
00146 return false;
00147 }
00148
00149
00161 void TimeService::Receive(const Message* message)
00162 {
00163 switch(message->GetMessageCode())
00164 {
00165 case QUERY_TIME:
00166 {
00167 const QueryTime* query = dynamic_cast<const QueryTime*>(message);
00168 if(query)
00169 {
00170 ReportTime report(message->GetSourceID(), GetComponentID());
00171
00172 if(query->IsFieldPresent(SetTime::PresenceVector::Time))
00173 {
00174 Time t;
00175 t.SetCurrentTime();
00176 report.SetTimeStamp(t);
00177 }
00178 if(query->IsFieldPresent(SetTime::PresenceVector::Date))
00179 {
00180 Date d;
00181
00182 time_t rawtime;
00183 tm* ptm;
00184 time(&rawtime);
00185 ptm = gmtime(&rawtime);
00186
00187 d.mYear = ptm->tm_year + 1900;
00188 d.mDay = ptm->tm_mday;
00189 d.mMonth = ptm->tm_mon;
00190 report.SetDateStamp(d);
00191 }
00192
00193 Send(&report);
00194 }
00195 }
00196 break;
00197 case REPORT_TIME:
00198 {
00199
00200 const ReportTime* report = dynamic_cast<const ReportTime*>(message);
00201 if(report && report->GetSourceID() == GetSynchronizeID())
00202 {
00203 Time t;
00204 Date d;
00205
00206
00207 time_t rawtime;
00208 tm* ptm;
00209 time(&rawtime);
00210 ptm = gmtime(&rawtime);
00211
00212 d.mYear = ptm->tm_year + 1900;
00213 d.mDay = ptm->tm_mday;
00214 d.mMonth = ptm->tm_mon;
00215
00216 t.SetCurrentTime();
00217
00218 if(report->IsFieldPresent(ReportTime::PresenceVector::Time))
00219 {
00220 t = report->GetTimeStamp();
00221 }
00222 if(report->IsFieldPresent(ReportTime::PresenceVector::Date))
00223 {
00224 d = report->GetDateStamp();
00225 }
00226
00227 if(mpChangeTimeMethod)
00228 {
00229 mpChangeTimeMethod(t, d);
00230 }
00231 else
00232 {
00233 CxUtils::SetSystemTime(d.mYear,
00234 d.mMonth,
00235 t.mDay,
00236 t.mHour,
00237 t.mMinute,
00238 t.mSecond,
00239 t.mMilliseconds);
00240 }
00241
00242 SignalEvent(REPORT_TIME);
00243 }
00244 }
00245 break;
00246 case SET_TIME:
00247 {
00248 const SetTime* command = dynamic_cast<const SetTime*>(message);
00249 if(command)
00250 {
00251 Time t;
00252 Date d;
00253
00254
00255 time_t rawtime;
00256 tm* ptm;
00257 time(&rawtime);
00258 ptm = gmtime(&rawtime);
00259
00260 d.mYear = ptm->tm_year + 1900;
00261 d.mDay = ptm->tm_mday;
00262 d.mMonth = ptm->tm_mon;
00263
00264 t.SetCurrentTime();
00265
00266 if(command->IsFieldPresent(SetTime::PresenceVector::Time))
00267 {
00268 t = command->GetTimeStamp();
00269 }
00270 if(command->IsFieldPresent(SetTime::PresenceVector::Date))
00271 {
00272 d = command->GetDateStamp();
00273 }
00274
00275 if(mpChangeTimeMethod)
00276 {
00277 mpChangeTimeMethod(t, d);
00278 }
00279 else
00280 {
00281 CxUtils::SetSystemTime(d.mYear,
00282 d.mMonth,
00283 t.mDay,
00284 t.mHour,
00285 t.mMinute,
00286 t.mSecond,
00287 t.mMilliseconds);
00288 }
00289
00290 SignalEvent(REPORT_TIME);
00291 }
00292 }
00293 break;
00294 default:
00295 break;
00296 }
00297 }
00298
00299
00313 Message* TimeService::CreateMessage(const UShort messageCode) const
00314 {
00315 Message* message;
00316 switch(messageCode)
00317 {
00318 case QUERY_TIME:
00319 message = new QueryTime();
00320 break;
00321 case REPORT_TIME:
00322 message = new ReportTime();
00323 break;
00324 case SET_TIME:
00325 message = new SetTime();
00326 break;
00327 default:
00328 message = NULL;
00329 break;
00330 }
00331 return message;
00332 }
00333
00334
00344 void TimeService::CheckServiceSynchronization(const unsigned int timeSinceLastCheckMs)
00345 {
00346 Address syncID = GetSynchronizeID();
00347 if(syncID.IsValid())
00348 {
00349
00350 if(EventsService()->HaveSubscription(REPORT_TIME, syncID) == false)
00351 {
00352 QueryTime query(syncID, GetComponentID());
00353 query.SetPresenceVector(query.GetPresenceVectorMask());
00354 EventsService()->RequestEveryChangeEvent(syncID, &query);
00355 }
00356 }
00357 }
00358
00359