Trying but failing to use Register
This commit is contained in:
		
							parent
							
								
									b1ac0c8741
								
							
						
					
					
						commit
						5367cc1961
					
				| @ -0,0 +1,74 @@ | ||||
| #include "SiteServer.h" | ||||
| 
 | ||||
| #include "Sensors/TemperatureSensor.h" | ||||
| 
 | ||||
| #include <functional> | ||||
| #include <unordered_map> | ||||
| #include <memory> | ||||
| 
 | ||||
| namespace Passer { | ||||
| namespace Control { | ||||
| 
 | ||||
| SiteServer::SiteServer(int port) { | ||||
|   this->name = "Site Server"; | ||||
| 
 | ||||
|   this->ipAddress = "0.0.0.0"; | ||||
|   this->port = port; | ||||
| 
 | ||||
|   this->participants.push_back(this); | ||||
| 
 | ||||
|   SetupUDP(port, ipAddress, 0); | ||||
| 
 | ||||
|   Register<TemperatureSensor>((unsigned char)Thing::Type::TemperatureSensor); | ||||
| } | ||||
| 
 | ||||
| void SiteServer::Update(unsigned long currentTimeMs) { | ||||
|   this->ReceiveUDP(); | ||||
|   Thing::UpdateAll(currentTimeMs); | ||||
| } | ||||
| 
 | ||||
| void SiteServer::Process(Participant *sender, ClientMsg *msg) { | ||||
|   if (msg->networkId == 0) { | ||||
|     std::cout << this->name << " received New Client -> " << sender->ipAddress | ||||
|               << "  " << (int)sender->networkId << "\n"; | ||||
|     NetworkIdMsg *msg = new NetworkIdMsg(sender->networkId); | ||||
|     sender->Send(msg); | ||||
|     delete msg; | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| void SiteServer::Process(Participant *sender, NetworkIdMsg *msg) {} | ||||
| 
 | ||||
| using ThingConstructor = std::function<std::unique_ptr<Thing>( | ||||
|     unsigned char networkId, unsigned char thingId)>; | ||||
| std::unordered_map<unsigned char, ThingConstructor> thingMsgProcessors; | ||||
| 
 | ||||
| template <typename ThingClass> | ||||
| void SiteServer::Register(unsigned char thingType) { | ||||
|   thingMsgProcessors[thingType] = [](unsigned char networkId, | ||||
|                                      unsigned char thingId) { | ||||
|     return std::make_unique<ThingClass>(networkId, thingId); | ||||
|   }; | ||||
| } | ||||
| 
 | ||||
| // template <typename ThingClass>
 | ||||
| // void SiteServer::Register(Thing::Type thingType)
 | ||||
| // {
 | ||||
| //   Register<ThingClass>((unsigned char)thingType);
 | ||||
| // }
 | ||||
| 
 | ||||
| void SiteServer::Process(ThingMsg *msg) { | ||||
| 
 | ||||
|   Thing *thing = Thing::Get(msg->networkId, msg->thingId); | ||||
|   if (thing == nullptr) { | ||||
|     auto thingMsgProcessor = thingMsgProcessors.find(msg->thingType); | ||||
|     if (thingMsgProcessor != thingMsgProcessors.end()) // found item
 | ||||
|       thingMsgProcessor->second(msg->networkId, msg->thingId); | ||||
|     else | ||||
|       new Thing(this, msg->networkId, msg->thingId, | ||||
|                 (Thing::Type)msg->thingType); | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| } // namespace Control
 | ||||
| } // namespace Passer
 | ||||
| @ -13,6 +13,7 @@ public: | ||||
|   virtual void Update(unsigned long currentTimeMs) override; | ||||
| 
 | ||||
|   template <typename ThingClass> void Register(unsigned char thingType); | ||||
|   // template <typename ThingClass> void Register(Thing::Type thingType);
 | ||||
| 
 | ||||
| protected: | ||||
|   unsigned long nextPublishMe = 0; | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Pascal Serrarens
						Pascal Serrarens