df6fa7aa818c3359b24d58ca3f88313a668306ba
[profile/ivi/genivi/genivi-audio-manager.git] / AudioManagerDaemon / include / SocketHandler.h
1 /*
2  * SocketHandler.h
3  *
4  *  Created on: Dec 18, 2011
5  *      Author: christian
6  */
7
8 #ifndef SOCKETHANDLER_H_
9 #define SOCKETHANDLER_H_
10
11 #include <audiomanagertypes.h>
12 #include <sys/socket.h>
13 #include <sys/time.h>
14 #include <map>
15 #include <stdint.h>
16 #include <sys/poll.h>
17 #include <list>
18
19 namespace am {
20
21 class TBasicPollCallback;
22 class TBasicTimerCallback;
23
24 class SocketHandler
25 {
26 public:
27         typedef uint16_t timerHandle_t;   //!<this is a handle for a timer to be used with the SocketHandler
28         SocketHandler();
29         virtual ~SocketHandler();
30
31         am_Error_e addFDPoll(const int fd,const short event,TBasicPollCallback*& callback);
32         am_Error_e removeFDPoll(const int fd);
33         am_Error_e addTimer(const timespec timeouts,TBasicTimerCallback*& callback,timerHandle_t& handle);
34         am_Error_e removeTimer(const timerHandle_t handle);
35         void start_listenting();
36         void stop_listening();
37
38 private:
39         struct timer_s                                          //!<struct that holds information of timers
40         {
41                 timerHandle_t handle;                   //!<the handle of the timer
42                 timespec countdown;                             //!<the countdown, this value is decreased every time the timer is up
43                 timespec timeout;                               //!<the original timer value
44                 TBasicTimerCallback* callback;  //!<the callbackfunction
45         };
46
47         class SubstractTime                                     //!<functor to easy substract from each countdown value
48         {
49         private:
50                 timespec param;
51         public:
52                 SubstractTime(timespec param): param(param) {}
53                 void operator()(timer_s& t) const;
54         };
55
56         typedef std::vector<pollfd> mPollfd_t;                                          //!<vector of filedescriptors
57         typedef std::map<int,TBasicPollCallback*> mMapFdCallback_t;     //!<map for the callbacks
58
59         bool fdIsValid(const int fd) const;
60         void initTimer();
61         void timerUp();
62         int timespec2ms(const timespec& time);
63         static bool compareCountdown(const timer_s& a, const timer_s& b)
64         {
65                 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);
66         }
67
68
69         mMapFdCallback_t mMapFdCallback;
70         std::list<timer_s> mListTimer;
71         mPollfd_t mListPollfd;
72         timerHandle_t mNextTimer;
73         timerHandle_t mLastInsertedHandle;
74         timespec mTimeout;
75         bool mDispatch;
76 };
77
78 /**
79  * classic functor for the BasiCallCallback
80  */
81 class TBasicPollCallback
82 {
83 public:
84         virtual void Call (int fd, const short revent)=0;
85         virtual ~TBasicPollCallback(){};
86 };
87
88 /**
89  * classic functor for the BasicTimerCallback
90  */
91 class TBasicTimerCallback
92 {
93 public:
94         virtual void Call (SocketHandler::timerHandle_t handle)=0;
95         virtual ~TBasicTimerCallback(){};
96 };
97
98
99 /**
100  * template to create the functor for a class
101  */
102 template <class TClass> class TSpecificPollCallback : public TBasicPollCallback
103 {
104 private:
105     TClass* mInstance;
106     void (TClass::*mFunction)(int fd, const short revent);
107
108 public:
109     TSpecificPollCallback(TClass* instance, void(TClass::*function)(int fd, const short revent))
110     :mInstance(instance), mFunction(function){};
111
112     virtual void Call(int fd, const short revent)
113     {
114         (*mInstance.*mFunction)(fd,revent);
115     };
116 };
117
118 /**
119  * template to create the functor for a class
120  */
121 template <class TClass> class TSpecificTimerCallback : public TBasicTimerCallback
122 {
123 private:
124     TClass* mInstance;
125     void (TClass::*mFunction)(SocketHandler::timerHandle_t handle);
126
127 public:
128     TSpecificTimerCallback(TClass* instance, void(TClass::*function)(SocketHandler::timerHandle_t handle))
129     :mInstance(instance), mFunction(function){};
130
131     virtual void Call(SocketHandler::timerHandle_t handle)
132     {
133         (*mInstance.*mFunction)(handle);
134     }
135 };
136 } /* namespace am */
137 #endif /* SOCKETHANDLER_H_ */