c76e343137f11d6746338b92e209fe0126e45f05
[profile/ivi/audiomanager.git] / includes / SocketHandler.h
1 /**
2 * Copyright (C) 2011, BMW AG
3 *
4 * GeniviAudioMananger AudioManagerDaemon
5 *
6 * \file SocketHandler.h
7 *
8 * \date 20-Oct-2011 3:42:04 PM
9 * \author Christian Mueller (christian.ei.mueller@bmw.de)
10 *
11 * \section License
12 * GNU Lesser General Public License, version 2.1, with special exception (GENIVI clause)
13 * Copyright (C) 2011, BMW AG Christian Mueller  Christian.ei.mueller@bmw.de
14 *
15 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License, version 2.1, as published by the Free Software Foundation.
16 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License, version 2.1, for more details.
17 * You should have received a copy of the GNU Lesser General Public License, version 2.1, along with this program; if not, see <http://www.gnu.org/licenses/lgpl-2.1.html>.
18 * Note that the copyright holders assume that the GNU Lesser General Public License, version 2.1, may also be applicable to programs even in cases in which the program is not a library in the technical sense.
19 * Linking AudioManager statically or dynamically with other modules is making a combined work based on AudioManager. You may license such other modules under the GNU Lesser General Public License, version 2.1. If you do not want to license your linked modules under the GNU Lesser General Public License, version 2.1, you may use the program under the following exception.
20 * As a special exception, the copyright holders of AudioManager give you permission to combine AudioManager with software programs or libraries that are released under any license unless such a combination is not permitted by the license of such a software program or library. You may copy and distribute such a system following the terms of the GNU Lesser General Public License, version 2.1, including this special exception, for AudioManager and the licenses of the other code concerned.
21 * Note that people who make modified versions of AudioManager are not obligated to grant this special exception for their modified versions; it is their choice whether to do so. The GNU Lesser General Public License, version 2.1, gives permission to release a modified version without this exception; this exception also makes it possible to release a modified version which carries forward this exception.
22 *
23 */
24
25 #ifndef SOCKETHANDLER_H_
26 #define SOCKETHANDLER_H_
27
28 #include <audiomanagertypes.h>
29 #include <sys/socket.h>
30 #include <sys/time.h>
31 #include <stdint.h>
32 #include <sys/poll.h>
33 #include <list>
34 #include <map>
35
36 namespace am {
37
38 typedef uint16_t sh_timerHandle_t;      //!<this is a handle for a timer to be used with the SocketHandler
39 typedef uint16_t sh_pollHandle_t;               //!<this is a handle for a filedescriptor to be used with the SocketHandler
40
41 class shPollPrepare;
42 class shPollCheck;
43 class shPollFired;
44 class shPollDispatch;
45 class TBasicTimerCallback;
46
47 class SocketHandler
48 {
49 public:
50         SocketHandler();
51         virtual ~SocketHandler();
52
53         am_Error_e addFDPoll(const int fd,const short event, shPollPrepare *prepare,shPollFired *fired,shPollCheck *check,shPollDispatch *dispatch, void* userData,sh_pollHandle_t& handle);
54         am_Error_e removeFDPoll(const sh_pollHandle_t handle);
55         am_Error_e updateEventFlags(const sh_pollHandle_t handle, const short events);
56         am_Error_e addTimer(const timespec timeouts,TBasicTimerCallback*& callback,sh_timerHandle_t& handle, void* userData);
57         am_Error_e removeTimer(const sh_timerHandle_t handle);
58         am_Error_e restartTimer(const sh_timerHandle_t handle, const timespec timeouts);
59         am_Error_e stopTimer(const sh_timerHandle_t handle);
60         void start_listenting();
61         void stop_listening();
62
63 private:
64         struct timer_s                                          //!<struct that holds information of timers
65         {
66                 sh_timerHandle_t handle;                //!<the handle of the timer
67                 timespec countdown;                             //!<the countdown, this value is decreased every time the timer is up
68                 timespec timeout;                               //!<the original timer value
69                 TBasicTimerCallback* callback;  //!<the callbackfunction
70                 void * userData;                                //!<saves a void pointer together with the rest.
71         };
72
73         class SubstractTime                                     //!<functor to easy substract from each countdown value
74         {
75         private:
76                 timespec param;
77         public:
78                 SubstractTime(timespec param): param(param) {}
79                 void operator()(timer_s& t) const;
80         };
81
82         struct sh_poll_s                                                //!<struct that holds information about polls
83         {
84                 sh_pollHandle_t handle;                         //!<handle to uniquely adress a filedesriptor
85                 shPollPrepare *prepareCB;
86                 shPollFired *firedCB;
87                 shPollCheck *checkCB;
88                 shPollDispatch *dispatchCB;
89                 pollfd pollfdValue;                                             //!<the array for polling the filedescriptors
90                 void *userData;                                         //!<userdata saved together with the callback.
91         };
92
93         typedef std::vector<pollfd> mPollfd_t;          //!<vector of filedescriptors
94         typedef std::vector<sh_poll_s> mListPoll_t;     //!<list for the callbacks
95
96         class CopyPollfd
97         {
98         private:
99                 mPollfd_t& mArray;
100         public:
101                 CopyPollfd(mPollfd_t& dest): mArray(dest) {}
102                 void operator()(const sh_poll_s& row);
103         };
104
105         bool fdIsValid(const int fd) const;
106         void initTimer();
107         void timerUp();
108         int timespec2ms(const timespec& time);
109         static bool compareCountdown(const timer_s& a, const timer_s& b)
110         {
111                 return (a.countdown.tv_sec==b.countdown.tv_sec) ? (a.countdown.tv_nsec < b.countdown.tv_nsec) : (a.countdown.tv_sec < b.countdown.tv_sec);
112         }
113
114         static bool onlyFiredEvents(const pollfd& a)
115         {
116                 return a.events==0 ? false : true;
117         }
118
119         //todo: maybe we could simplify mListActiveTimer to hold only the handle and the countdown ....
120         mPollfd_t mfdPollingArray;
121         mListPoll_t mListPoll;
122         std::list<timer_s> mListTimer;                  //!<list of all timers
123         std::list<timer_s> mListActiveTimer;    //!<list of all currently active timers
124         sh_timerHandle_t mNextTimer;
125         sh_timerHandle_t mLastInsertedHandle;
126         sh_pollHandle_t mLastInsertedPollHandle;
127         timespec mTimeout;
128         bool mDispatch;
129         bool mRecreatePollfds;
130 };
131
132 /**
133  * classic functor for the BasicTimerCallback
134  */
135 class TBasicTimerCallback
136 {
137 public:
138         virtual void Call (const sh_timerHandle_t handle, void* userData)=0;
139         virtual ~TBasicTimerCallback(){};
140 };
141
142 class shPollPrepare
143 {
144 public:
145         virtual void Call (const sh_pollHandle_t handle, void* userData)=0;
146         virtual ~shPollPrepare(){};
147 };
148
149 class shPollFired
150 {
151 public:
152         virtual void Call(const pollfd pollfd,const sh_pollHandle_t handle, void* userData)=0;
153         virtual ~ shPollFired(){};
154 };
155
156 class shPollCheck
157 {
158 public:
159         virtual bool Call (const sh_pollHandle_t handle, void* userData)=0;
160         virtual ~ shPollCheck(){};
161 };
162
163 class shPollDispatch
164 {
165 public:
166         virtual bool Call (const sh_pollHandle_t handle, void* userData)=0;
167         virtual ~ shPollDispatch(){};
168 };
169
170 /**
171  * template to create the functor for a class
172  */
173 template <class TClass> class TSpecificTimerCallback : public TBasicTimerCallback
174 {
175 private:
176         TClass* mInstance;
177         void (TClass::*mFunction)(sh_timerHandle_t handle, void* userData);
178
179 public:
180         TSpecificTimerCallback(TClass* instance, void(TClass::*function)(sh_timerHandle_t handle, void* userData))
181         :mInstance(instance), mFunction(function){};
182
183         virtual void Call(sh_timerHandle_t handle, void* userData)
184         {
185                 (*mInstance.*mFunction)(handle, userData);
186         }
187 };
188
189 /**
190  * template to create the functor for a class
191  */
192 template <class TClass> class shPollPrepare_T : public shPollPrepare
193 {
194 private:
195         TClass* mInstance;
196         void (TClass::*mFunction)(const sh_timerHandle_t handle, void* userData);
197
198 public:
199         shPollPrepare_T(TClass* instance, void(TClass::*function)(const sh_timerHandle_t handle, void* userData))
200         :mInstance(instance), mFunction(function){};
201
202         virtual void Call(const sh_timerHandle_t handle, void* userData)
203         {
204                 (*mInstance.*mFunction)(handle,userData);
205         };
206 };
207
208 template <class TClass> class shPollFired_T : public shPollFired
209 {
210 private:
211         TClass* mInstance;
212         void (TClass::*mFunction)(const pollfd pollfd,const sh_pollHandle_t handle, void* userData);
213
214 public:
215         shPollFired_T(TClass* instance, void(TClass::*function)(const pollfd pollfd,const sh_pollHandle_t handle, void* userData))
216         :mInstance(instance), mFunction(function){};
217
218         virtual void Call(const pollfd pollfd,const sh_pollHandle_t handle, void* userData)
219         {
220                 (*mInstance.*mFunction)(pollfd,handle,userData);
221         };
222 };
223
224 template <class TClass> class shPollCheck_T : public shPollCheck
225 {
226 private:
227         TClass* mInstance;
228         bool (TClass::*mFunction)(const sh_pollHandle_t handle, void* userData);
229
230 public:
231         shPollCheck_T(TClass* instance, bool(TClass::*function)(const sh_pollHandle_t handle, void* userData))
232         :mInstance(instance), mFunction(function){};
233
234         virtual bool Call(const sh_pollHandle_t handle, void* userData)
235         {
236                 return (*mInstance.*mFunction)(handle,userData);
237         };
238 };
239
240 template <class TClass> class shPollDispatch_T : public shPollDispatch
241 {
242 private:
243         TClass* mInstance;
244         bool (TClass::*mFunction)(const sh_pollHandle_t handle, void* userData);
245
246 public:
247         shPollDispatch_T(TClass* instance, bool (TClass::*function)(const sh_pollHandle_t handle, void* userData))
248         :mInstance(instance), mFunction(function){};
249
250         virtual bool Call(const sh_pollHandle_t handle, void* userData)
251         {
252                 return (*mInstance.*mFunction)(handle,userData);
253         };
254 };
255 } /* namespace am */
256 #endif /* SOCKETHANDLER_H_ */