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/mobility/list/listmanager.h"
00041 #include "jaus/mobility/list/confirmelementrequest.h"
00042 #include "jaus/mobility/list/rejectelementrequest.h"
00043 #include "jaus/mobility/list/deleteelement.h"
00044 #include "jaus/mobility/list/executelist.h"
00045 #include "jaus/mobility/list/queryelement.h"
00046 #include "jaus/mobility/list/queryelementcount.h"
00047 #include "jaus/mobility/list/queryelementlist.h"
00048 #include "jaus/mobility/list/queryactiveelement.h"
00049 #include "jaus/mobility/list/reportelement.h"
00050 #include "jaus/mobility/list/reportelementlist.h"
00051 #include "jaus/mobility/list/reportelementcount.h"
00052 #include "jaus/mobility/list/reportactiveelement.h"
00053 #include "jaus/mobility/list/setelement.h"
00054 #include "jaus/core/component.h"
00055
00056 using namespace JAUS;
00057
00058 const std::string ListManager::Name = "urn:jaus:jss:mobility:ListManager";
00059
00060
00069 ListManager::Child::Child(const Service::ID& serviceIdentifier,
00070 const Service::ID& parentServiceIdentifier) : Management::Child(serviceIdentifier,
00071 parentServiceIdentifier)
00072 {
00073 mActiveElement = 0;
00074 }
00075
00076
00082 ListManager::Child::~Child()
00083 {
00084
00085 }
00086
00087
00097 void ListManager::Child::SetActiveListElement(const UShort uid)
00098 {
00099 Mutex::ScopedLock lock(&mListMutex);
00100 Element::Map::iterator e = mElementList.find(uid);
00101
00102 if(e != mElementList.end())
00103 {
00104 mActiveElement = uid;
00105 Events::Child::SignalEvent(REPORT_ACTIVE_ELEMENT);
00106 }
00107 }
00108
00109
00125 bool ListManager::Child::DeleteListElement(const UShort id, Byte& rejectReason)
00126 {
00127 Mutex::ScopedLock lock(&mListMutex);
00128 Element::Map::iterator e;
00129 if(id == JAUS_USHORT_MAX)
00130 {
00131 mElementList.clear();
00132 mActiveElement = 0;
00133 return true;
00134 }
00135 e = mElementList.find(id);
00136 if(e != mElementList.end())
00137 {
00138 Element::Map::iterator prev;
00139 Element::Map::iterator next;
00140 prev = mElementList.find(e->second.mPrevID);
00141 next = mElementList.find(e->second.mNextID);
00142 if((e->second.mPrevID == 0 || prev != mElementList.end()) &&
00143 (e->second.mNextID == 0 || next != mElementList.end()))
00144 {
00145 if(prev != mElementList.end())
00146 {
00147 prev->second.mNextID = e->second.mNextID;
00148 }
00149 if(next != mElementList.end())
00150 {
00151 next->second.mPrevID = e->second.mPrevID;
00152 }
00153 mElementList.erase(e);
00154 return true;
00155 }
00156 }
00157 else
00158 {
00159 rejectReason = RejectElementRequest::InvalidElementID;
00160 }
00161
00162 return false;
00163 }
00164
00165
00175 bool ListManager::Child::SetElements(const Element::List& elements)
00176 {
00177 bool result = false;
00178
00179 Mutex::ScopedLock lock(&mListMutex);
00180
00181
00182 Element::Map backup = mElementList;
00183
00184 Element::List::const_iterator ins;
00185 Element::Map::iterator prev;
00186 Element::Map::iterator next;
00187
00188 for(ins = elements.begin();
00189 ins != elements.end();
00190 ins++)
00191 {
00192 if(ins->mID == 0)
00193 {
00194 if(mDebugMessagesFlag)
00195 {
00196 Mutex::ScopedLock lock(&mDebugMessagesMutex);
00197 std::cout << "[" << GetServiceID().ToString() << "-" << GetComponentID().ToString() << "] - Received Invalid Element ID\n";
00198 }
00199 return false;
00200 }
00201 mElementList[ins->mID] = (*ins);
00202
00203 prev = mElementList.find(ins->mPrevID);
00204 if(prev != mElementList.end())
00205 {
00206 prev->second.mNextID = ins->mID;
00207 }
00208 next = mElementList.find(ins->mNextID);
00209 if(next != mElementList.end())
00210 {
00211 next->second.mPrevID = ins->mID;
00212 }
00213 }
00214
00215
00216 Element::Map::iterator e;
00217
00218 std::vector<UShort> attached;
00219
00220 bool success = false;
00221 bool foundLast = false;
00222 for(e = mElementList.begin();
00223 e != mElementList.end();
00224 e++)
00225 {
00226 if(e->second.mPrevID == 0)
00227 {
00228
00229 success = true;
00230 attached.push_back(e->first);
00231 prev = e;
00232 next = mElementList.find(e->second.mNextID);
00233 while(next != mElementList.end())
00234 {
00235
00236 if(next->second.mPrevID == prev->first)
00237 {
00238 attached.push_back(next->first);
00239 }
00240 prev = next;
00241 if(next->second.mNextID == 0)
00242 {
00243 foundLast = true;
00244 break;
00245 }
00246 next = mElementList.find(next->second.mNextID);
00247 }
00248 break;
00249 }
00250 }
00251 if(mElementList.size() == 1 && foundLast == false)
00252 {
00253 if(mElementList.begin()->second.mNextID == 0)
00254 {
00255 foundLast = true;
00256 }
00257 }
00258
00259 if(success == false || attached.size() != mElementList.size() || foundLast == false)
00260 {
00261 mElementList = backup;
00262 return false;
00263 }
00264
00265 return true;
00266 }
00267
00268
00279 Element ListManager::Child::GetActiveListElement() const
00280 {
00281 Mutex::ScopedLock lock(&mListMutex);
00282 if(mActiveElement != 0)
00283 {
00284 Element::Map::const_iterator e;
00285 e = mElementList.find(mActiveElement);
00286 if(e != mElementList.end())
00287 {
00288 return e->second;
00289 }
00290 }
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306 return Element();
00307 }
00308
00309
00322 Element ListManager::Child::GetElement(const UShort id) const
00323 {
00324 Mutex::ScopedLock lock(&mListMutex);
00325 if(id != 0)
00326 {
00327 Element::Map::const_iterator e;
00328 e = mElementList.find(id);
00329 if(e != mElementList.end())
00330 {
00331 return e->second;
00332 }
00333 }
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349 return Element();
00350 }
00351
00352
00360 void ListManager::Child::AdvanceListElement()
00361 {
00362 Mutex::ScopedLock lock(&mListMutex);
00363 if(mActiveElement != 0)
00364 {
00365 Element::Map::const_iterator e;
00366 e = mElementList.find(mActiveElement);
00367 if(e != mElementList.end())
00368 {
00369 mActiveElement = e->second.mNextID;
00370 EventsService()->SignalEvent(REPORT_ACTIVE_ELEMENT);
00371 }
00372 }
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389 }
00390
00391
00397 Element::Map ListManager::Child::GetElementList() const
00398 {
00399 Mutex::ScopedLock lock(&mListMutex);
00400 return mElementList;
00401 }
00402
00403
00409 void ListManager::Child::GetElementList(std::vector<UShort>& list) const
00410 {
00411 Mutex::ScopedLock lock(&mListMutex);
00412 list.clear();
00413 Element::Map::const_iterator e;
00414 for(e = mElementList.begin();
00415 e != mElementList.end();
00416 e++)
00417 {
00418 list.push_back(e->first);
00419 }
00420 }
00421
00422
00428 unsigned int ListManager::Child::GetElementCount() const
00429 {
00430 Mutex::ScopedLock lock(&mListMutex);
00431 return (unsigned int)mElementList.size();
00432 }
00433
00434
00440 ListManager::ListManager() : Management::Child(Service::ID(ListManager::Name),
00441 Service::ID(Management::Name))
00442 {
00443 }
00444
00445
00451 ListManager::~ListManager()
00452 {
00453 }
00454
00455
00461 bool ListManager::Reset()
00462 {
00463
00464 Service::Map::iterator child;
00465 Service::Map children = this->GetChildServices();
00466 for(child = children.begin();
00467 child != children.end();
00468 child++)
00469 {
00470 ListManager::Child* c = dynamic_cast<ListManager::Child*>(child->second);
00471 if(c)
00472 {
00473 Byte responseCode = RejectElementRequest::UnspecifiedError;
00474 c->DeleteListElement(JAUS_USHORT_MAX, responseCode);
00475 }
00476 }
00477 return true;
00478 }
00479
00480
00490 bool ListManager::GenerateEvent(const Events::Subscription& info) const
00491 {
00492 if(info.mpQueryMessage->GetMessageCode() == QUERY_ELEMENT_COUNT)
00493 {
00494 JAUS::ReportElementCount report;
00495
00496 Service::Map::const_iterator child;
00497 const Service::Map children = this->GetChildServices();
00498 for(child = children.begin();
00499 child != children.end();
00500 child++)
00501 {
00502 ListManager::Child* c = dynamic_cast<ListManager::Child*>(child->second);
00503 if(c)
00504 {
00505 report.SetElementCount(c->GetElementCount());
00506 break;
00507 }
00508 }
00509
00510 SendEvent(info, &report);
00511 return true;
00512 }
00513 if(info.mpQueryMessage->GetMessageCode() == QUERY_ELEMENT_COUNT)
00514 {
00515 JAUS::ReportElementList report;
00516
00517 Service::Map::const_iterator child;
00518 const Service::Map children = this->GetChildServices();
00519 for(child = children.begin();
00520 child != children.end();
00521 child++)
00522 {
00523 ListManager::Child* c = dynamic_cast<ListManager::Child*>(child->second);
00524 if(c)
00525 {
00526 c->GetElementList(*report.GetElementList());
00527 break;
00528 }
00529 }
00530
00531 SendEvent(info, &report);
00532 return true;
00533 }
00534 if(info.mpQueryMessage->GetMessageCode() == QUERY_ACTIVE_ELEMENT)
00535 {
00536 JAUS::ReportActiveElement report;
00537
00538 Service::Map::const_iterator child;
00539 const Service::Map children = this->GetChildServices();
00540 for(child = children.begin();
00541 child != children.end();
00542 child++)
00543 {
00544 ListManager::Child* c = dynamic_cast<ListManager::Child*>(child->second);
00545 if(c)
00546 {
00547 report.SetElementUID(c->GetActiveListElement().mID);
00548 break;
00549 }
00550 }
00551
00552 SendEvent(info, &report);
00553 return true;
00554 }
00555 return false;
00556 }
00557
00558
00574 bool ListManager::IsEventSupported(const Events::Type type,
00575 const double requestedPeriodicRate,
00576 const Message* queryMessage,
00577 double& confirmedPeriodicRate,
00578 std::string& errorMessage) const
00579 {
00580 bool result = false;
00581 switch(queryMessage->GetMessageCode())
00582 {
00583 case QUERY_ELEMENT_COUNT:
00584 case QUERY_ELEMENT_LIST:
00585 case QUERY_ACTIVE_ELEMENT:
00586 {
00587 result = true;
00588 confirmedPeriodicRate = requestedPeriodicRate;
00589 }
00590 break;
00591 default:
00592 result = false;
00593 break;
00594 };
00595 return result;
00596 }
00597
00598
00610 void ListManager::Receive(const Message* message)
00611 {
00612
00613
00614 Service::Map::iterator child;
00615 Service::Map children = this->GetChildServices();
00616 ListManager::Child* list = NULL;
00617 Byte responseCode = RejectElementRequest::UnspecifiedError;
00618 for(child = children.begin();
00619 child != children.end();
00620 child++)
00621 {
00622 list = dynamic_cast<ListManager::Child*>(child->second);
00623 if(list)
00624 {
00625 break;
00626 }
00627 }
00628
00629 switch(message->GetMessageCode())
00630 {
00631 case DELETE_ELEMENT:
00632 {
00633 const JAUS::DeleteElement* command = dynamic_cast<const JAUS::DeleteElement*>(message);
00634 if(command)
00635 {
00636 bool success = false;
00637
00638 Byte responseCode = RejectElementRequest::UnspecifiedError;
00639
00640 if(list)
00641 {
00642 success = true;
00643 for(std::vector<UShort>::const_iterator e = command->GetElementList()->begin();
00644 e != command->GetElementList()->end();
00645 e++)
00646 {
00647 if(list->DeleteListElement(*e, responseCode) == false)
00648 {
00649 success = false;
00650 break;
00651 }
00652 }
00653 }
00654
00655 if(success == false)
00656 {
00657 RejectElementRequest reject(command->GetSourceID(), GetComponentID());
00658 reject.SetRequestID(command->GetRequestID());
00659 reject.SetResponseCode((RejectElementRequest::Response)responseCode);
00660 Send(&reject);
00661 }
00662 else
00663 {
00664 ConfirmElementRequest confirm(command->GetSourceID(), GetComponentID());
00665 confirm.SetRequestID(command->GetRequestID());
00666 Send(&confirm);
00667 Events::Child::SignalEvent(REPORT_ELEMENT_COUNT);
00668 Events::Child::SignalEvent(REPORT_ELEMENT_LIST);
00669 }
00670 }
00671 }
00672 break;
00673 case QUERY_ELEMENT:
00674 {
00675 const JAUS::QueryElement* query = dynamic_cast<const JAUS::QueryElement*>(message);
00676 if(query)
00677 {
00678 JAUS::ReportElement report(message->GetSourceID(), GetComponentID());
00679
00680 if(list)
00681 {
00682 Element e = list->GetElement(query->GetElementUID());
00683 if(e.mID != 0)
00684 {
00685 report.SetElement(e);
00686 Send(&report);
00687 }
00688 }
00689 }
00690 }
00691 break;
00692 case QUERY_ELEMENT_COUNT:
00693 {
00694 const JAUS::QueryElementCount* query = dynamic_cast<const JAUS::QueryElementCount*>(message);
00695 if(query)
00696 {
00697 JAUS::ReportElementCount report(message->GetSourceID(), GetComponentID());
00698 if(list)
00699 {
00700 report.SetElementCount(list->GetElementCount());
00701 Send(&report);
00702 }
00703
00704 }
00705 }
00706 break;
00707 case QUERY_ELEMENT_LIST:
00708 {
00709 const JAUS::QueryElementList* query = dynamic_cast<const JAUS::QueryElementList*>(message);
00710 if(query)
00711 {
00712 JAUS::ReportElementList report(message->GetSourceID(), GetComponentID());
00713 if(list)
00714 {
00715 list->GetElementList(*report.GetElementList());
00716 Send(&report);
00717 }
00718 }
00719 }
00720 break;
00721 case QUERY_ACTIVE_ELEMENT:
00722 {
00723 const JAUS::QueryActiveElement* query = dynamic_cast<const JAUS::QueryActiveElement*>(message);
00724 if(query)
00725 {
00726 JAUS::ReportActiveElement report(message->GetSourceID(), GetComponentID());
00727 if(list)
00728 {
00729 report.SetElementUID(list->mActiveElement);
00730 Send(&report);
00731 }
00732 }
00733 }
00734 break;
00735 case SET_ELEMENT:
00736 {
00737 const JAUS::SetElement* command = dynamic_cast<const JAUS::SetElement*>(message);
00738 if(command)
00739 {
00740 bool success = false;
00741 Byte responseCode = RejectElementRequest::UnspecifiedError;
00742
00743 if(list)
00744 {
00745 success = true;
00746 Element::List final;
00747 for(Element::List::const_iterator e = command->GetElementList()->begin();
00748 e != command->GetElementList()->end();
00749 e++)
00750 {
00751 Element copy = (*e);
00752 if(copy.mpElement == NULL)
00753 {
00754 copy.mpElement = GetComponent()->TransportService()->CreateMessageFromPacket(copy.mPayload);
00755 }
00756 if(copy.mpElement)
00757 {
00758 copy.mpElement->CopyHeaderData(command);
00759 }
00760 if(copy.mpElement == NULL ||
00761 list->IsElementSupported(copy.mpElement) == false)
00762 {
00763 success = false;
00764 break;
00765 }
00766 final.push_back(copy);
00767 }
00768
00769 if(list->SetElements(final) == false)
00770 {
00771 success = false;
00772 }
00773 }
00774 if(success == false)
00775 {
00776 RejectElementRequest reject(command->GetSourceID(), GetComponentID());
00777 reject.SetRequestID(command->GetRequestID());
00778 reject.SetResponseCode((RejectElementRequest::Response)responseCode);
00779 Send(&reject);
00780 }
00781 else
00782 {
00783 ConfirmElementRequest confirm(command->GetSourceID(), GetComponentID());
00784 confirm.SetRequestID(command->GetRequestID());
00785 Send(&confirm);
00786 Events::Child::SignalEvent(REPORT_ELEMENT_COUNT);
00787 Events::Child::SignalEvent(REPORT_ELEMENT_LIST);
00788 }
00789 }
00790 }
00791 break;
00792 case CONFIRM_ELEMENT_REQUEST:
00793 {
00794 const JAUS::ConfirmElementRequest* command = dynamic_cast<const JAUS::ConfirmElementRequest*>(message);
00795 if(command)
00796 {
00797 }
00798 }
00799 break;
00800 case REJECT_ELEMENT_REQUEST:
00801 case REPORT_ELEMENT:
00802 case REPORT_ELEMENT_COUNT:
00803 case REPORT_ELEMENT_LIST:
00804 default:
00805 break;
00806 };
00807 }
00808
00809
00821 Message* ListManager::CreateMessage(const UShort messageCode) const
00822 {
00823 Message* message;
00824 switch(messageCode)
00825 {
00826 case SET_ELEMENT:
00827 message = new JAUS::SetElement();
00828 break;
00829 case CONFIRM_ELEMENT_REQUEST:
00830 message = new JAUS::ConfirmElementRequest();
00831 break;
00832 case DELETE_ELEMENT:
00833 message = new JAUS::DeleteElement();
00834 break;
00835 case EXECUTE_LIST:
00836 message = new JAUS::ExecuteList();
00837 break;
00838 case QUERY_ELEMENT:
00839 message = new JAUS::QueryElement();
00840 break;
00841 case QUERY_ACTIVE_ELEMENT:
00842 message = new JAUS::QueryActiveElement();
00843 break;
00844 case QUERY_ELEMENT_COUNT:
00845 message = new JAUS::QueryElementCount();
00846 break;
00847 case QUERY_ELEMENT_LIST:
00848 message = new JAUS::QueryElementList();
00849 break;
00850 case REJECT_ELEMENT_REQUEST:
00851 message = new JAUS::RejectElementRequest();
00852 break;
00853 case REPORT_ELEMENT:
00854 message = new JAUS::ReportElement();
00855 break;
00856 case REPORT_ELEMENT_COUNT:
00857 message = new JAUS::ReportElementCount();
00858 break;
00859 case REPORT_ELEMENT_LIST:
00860 message = new JAUS::ReportElementList();
00861 break;
00862 case REPORT_ACTIVE_ELEMENT:
00863 message = new JAUS::ReportActiveElement();
00864 break;
00865 default:
00866 message = NULL;
00867 break;
00868 };
00869 return message;
00870 }
00871
00872
00873
00874