Merge pull request #17 from tripzero/master
[profile/ivi/automotive-message-broker.git] / ambd / core.h
1 /*
2         Copyright (C) 2012  Intel Corporation
3
4         This library is free software; you can redistribute it and/or
5         modify it under the terms of the GNU Lesser General Public
6         License as published by the Free Software Foundation; either
7         version 2.1 of the License, or (at your option) any later version.
8
9         This library is distributed in the hope that it will be useful,
10         but WITHOUT ANY WARRANTY; without even the implied warranty of
11         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12         Lesser General Public License for more details.
13
14         You should have received a copy of the GNU Lesser General Public
15         License along with this library; if not, write to the Free Software
16         Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19
20 #ifndef CORE_H
21 #define CORE_H
22
23 #include "abstractsink.h"
24 #include "abstractsource.h"
25 #include "abstractroutingengine.h"
26 #include "asyncqueue.hpp"
27
28 #include <unordered_map>
29 #include <unordered_set>
30 #include <map>
31
32 namespace amb
33 {
34
35 struct PropertyCompare
36 {
37         bool operator()(AbstractPropertyType* const & lhs, AbstractPropertyType* & rhs) const
38         {
39                 if (lhs->name == rhs->name
40                                 && lhs->sourceUuid == rhs->sourceUuid
41                                 && lhs->zone == rhs->zone)
42                 {
43                         return true;
44                 }
45
46                 return false;
47         }
48
49 };
50
51 }
52
53 class Core: public AbstractRoutingEngine
54 {
55
56 public:
57         Core(std::map<std::string, std::string> config);
58         ~Core();
59         /// sources:
60
61         void registerSource(AbstractSource *src);
62         void updateSupported(PropertyList added, PropertyList removed, AbstractSource* source);
63         void updateProperty(AbstractPropertyType* value, const std::string &uuid);
64
65         /// sinks:
66
67         void registerSink(AbstractSink *self);
68         void unregisterSink(AbstractSink *self);
69         AsyncPropertyReply* getPropertyAsync(AsyncPropertyRequest request);
70         AsyncRangePropertyReply* getRangePropertyAsync(AsyncRangePropertyRequest request);
71         AsyncPropertyReply * setProperty(AsyncSetPropertyRequest request);
72         uint subscribeToProperty(const VehicleProperty::Property &, AbstractRoutingEngine::PropertyChangedType, std::string pid="");
73         bool subscribeToProperty(const VehicleProperty::Property &, AbstractSink* self);
74         bool subscribeToProperty(const VehicleProperty::Property &, const std::string & sourceUuidFilter, AbstractSink *self);
75         bool subscribeToProperty(const VehicleProperty::Property &, const std::string & sourceUuidFilter, Zone::Type zoneFilter, AbstractSink *self);
76         bool unsubscribeToProperty(const VehicleProperty::Property &, AbstractSink* self);
77         void unsubscribeToProperty(uint handle);
78         PropertyList supported();
79
80         PropertyInfo getPropertyInfo(const VehicleProperty::Property &,const std::string &sourceUuid);
81         std::list<std::string> sourcesForProperty(VehicleProperty::Property property);
82
83         struct Performance {
84                 Performance(): propertiesPerSecond(0), firedPropertiesPerSecond(0) {}
85                 int propertiesPerSecond;
86                 int firedPropertiesPerSecond;
87         };
88
89         void inspectSupported();
90
91 private:
92
93         void handleAddSupported(const PropertyList& added, AbstractSource* source);
94         void handleRemoveSupported(const PropertyList& removed, AbstractSource* source);
95         AbstractSource* sourceForProperty(const VehicleProperty::Property& property, const std::string& sourceUuidFilter = "") const;
96         void updateProperty(AbstractPropertyType* value);
97
98 private:
99
100         //typedef std::map< Zone::Type, AbstractPropertyType> ZonePropertyType;
101
102         // to support zone filtering replace VehicleProperty::Property with ZonePropertyType
103         std::multimap<AbstractSource*, VehicleProperty::Property> mMasterPropertyList;
104
105         // K = AbstractSource::uuid(), T = AbstractSource*
106         std::unordered_set<AbstractSource*> mSources;
107         std::unordered_set<AbstractSink*> mSinks;
108
109         Performance performance;
110
111         // std::string here is AbstractSource::uuid()
112         typedef std::map<AbstractSink*, std::string> FilteredSourceSinkMap;
113
114         ///uint cbHandle, std::string uuid
115         typedef std::unordered_map<uint, std::string> FilteredSourceCbMap;
116
117         // to support zone filtering replace VehicleProperty::Property with ZonePropertyType
118         std::unordered_map<VehicleProperty::Property, FilteredSourceSinkMap > propertySinkMap;
119         std::unordered_map<VehicleProperty::Property, FilteredSourceCbMap> propertyCbMap;
120         std::unordered_map<uint, AbstractRoutingEngine::PropertyChangedType> handleCbMap;
121
122         amb::Queue<AbstractPropertyType*, amb::PropertyCompare> updatePropertyQueue;
123         amb::Queue<AbstractPropertyType*, amb::PropertyCompare> updatePropertyQueueHigh;
124         amb::Queue<AbstractPropertyType*, amb::PropertyCompare> updatePropertyQueueLow;
125         amb::AsyncQueueWatcher<AbstractPropertyType*, amb::PropertyCompare>* watcherPtr;
126         amb::AsyncQueueWatcher<AbstractPropertyType*, amb::PropertyCompare>* watcherPtrHigh;
127         amb::AsyncQueueWatcher<AbstractPropertyType*, amb::PropertyCompare>* watcherPtrLow;
128
129         uint handleCount;
130 };
131
132 #endif // CORE_H