Wyatt  1.0.1
Communicator.cpp
1 #include "Communicator.h"
2 #include "hardwareinterface/EncoderAdapter.h"
3 #include "messages/EncoderMessage.h"
4 #include <iostream>
5 
7  this->sensorManager = sensorManager;
8  this->messageMap = new std::map<Hardware, IMessage*>;
9  this->hardwareInterfaceMap = new std::map<Hardware, IHardwareInterface*>;
10  this->hardwareInterfaces = new std::list<IHardwareInterface*>;
11 }
12 
14  delete this->sensorManager;
15  delete this->messageMap;
16  delete this->hardwareInterfaceMap;
17 }
18 
20 
21  if(this->hardwareInterfaceMap->count(hardware) != 0) {
22  throw DuplicateHardwareException(hardware);
23  }
24 
25  this->hardwareInterfaceMap->insert(std::make_pair(hardware, interface));
26  this->hardwareInterfaces->push_back(interface);
27 }
28 
30  m_lock.lock();
31  this->messageMap->erase(msg->getHardware());
32  this->messageMap->insert(std::make_pair(msg->getHardware(), msg));
33  m_lock.unlock();
34 }
35 
36 void Communicator::queueMessage(std::list<IMessage*>* messages) {
37  for(IMessage* msg: *messages) {
38  this->queueMessage(msg);
39  }
40  delete messages;
41 }
42 
43 void Communicator::write() {
44  m_lock.lock();
45  for (std::map<Hardware,IMessage*>::iterator it=messageMap->begin(); it!=messageMap->end(); ++it) {
46  IHardwareInterface* interface = hardwareInterfaceMap->at(it->first);
47  interface->write(it->second);
48  }
49  messageMap->clear();
50  m_lock.unlock();
51 }
52 
53 void Communicator::read() {
54  std::list<IMessage*>* messages = new std::list<IMessage*>;
55 
56  for (IHardwareInterface* interface : *(this->hardwareInterfaces)) {
57  std::list<IMessage*>* msgs = interface->read();
58  while(!msgs->empty()) {
59  IMessage* msg = msgs->front();
60  messages->push_back(msg);
61  msgs->pop_front();
62  }
63  delete msgs;
64  }
65  this->sensorManager->updateSensors(messages);
66 }
67 
69  while(!m_signal) {
70 
71  // Write any data
72  this->write();
73 
74  // Read any data
75  this->read();
76 
77  }
78  return NULL;
79 }
80 
Communicator(ISensorManager *sensorManager)
Definition: Communicator.cpp:6
virtual void updateSensors(std::list< IMessage * > *messages)=0
Hardware getHardware()
Definition: IMessage.h:30
void registerHardware(Hardware hardware, IHardwareInterface *interface)
void queueMessage(IMessage *message)
Definition: Thread.h:32
void * run() override