summaryrefslogtreecommitdiff
path: root/AudioManagerDaemon/include/SocketHandler.h
blob: df6fa7aa818c3359b24d58ca3f88313a668306ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
 * SocketHandler.h
 *
 *  Created on: Dec 18, 2011
 *      Author: christian
 */

#ifndef SOCKETHANDLER_H_
#define SOCKETHANDLER_H_

#include <audiomanagertypes.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <map>
#include <stdint.h>
#include <sys/poll.h>
#include <list>

namespace am {

class TBasicPollCallback;
class TBasicTimerCallback;

class SocketHandler
{
public:
	typedef uint16_t timerHandle_t;   //!<this is a handle for a timer to be used with the SocketHandler
	SocketHandler();
	virtual ~SocketHandler();

	am_Error_e addFDPoll(const int fd,const short event,TBasicPollCallback*& callback);
	am_Error_e removeFDPoll(const int fd);
	am_Error_e addTimer(const timespec timeouts,TBasicTimerCallback*& callback,timerHandle_t& handle);
	am_Error_e removeTimer(const timerHandle_t handle);
	void start_listenting();
	void stop_listening();

private:
	struct timer_s						//!<struct that holds information of timers
	{
		timerHandle_t handle;			//!<the handle of the timer
		timespec countdown;				//!<the countdown, this value is decreased every time the timer is up
		timespec timeout;				//!<the original timer value
		TBasicTimerCallback* callback;	//!<the callbackfunction
	};

	class SubstractTime					//!<functor to easy substract from each countdown value
	{
	private:
		timespec param;
	public:
		SubstractTime(timespec param): param(param) {}
		void operator()(timer_s& t) const;
	};

	typedef std::vector<pollfd> mPollfd_t;						//!<vector of filedescriptors
	typedef std::map<int,TBasicPollCallback*> mMapFdCallback_t;	//!<map for the callbacks

	bool fdIsValid(const int fd) const;
	void initTimer();
	void timerUp();
	int timespec2ms(const timespec& time);
	static bool compareCountdown(const timer_s& a, const timer_s& b)
	{
		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);
	}


	mMapFdCallback_t mMapFdCallback;
	std::list<timer_s> mListTimer;
	mPollfd_t mListPollfd;
	timerHandle_t mNextTimer;
	timerHandle_t mLastInsertedHandle;
	timespec mTimeout;
	bool mDispatch;
};

/**
 * classic functor for the BasiCallCallback
 */
class TBasicPollCallback
{
public:
	virtual void Call (int fd, const short revent)=0;
	virtual ~TBasicPollCallback(){};
};

/**
 * classic functor for the BasicTimerCallback
 */
class TBasicTimerCallback
{
public:
	virtual void Call (SocketHandler::timerHandle_t handle)=0;
	virtual ~TBasicTimerCallback(){};
};


/**
 * template to create the functor for a class
 */
template <class TClass> class TSpecificPollCallback : public TBasicPollCallback
{
private:
    TClass* mInstance;
    void (TClass::*mFunction)(int fd, const short revent);

public:
    TSpecificPollCallback(TClass* instance, void(TClass::*function)(int fd, const short revent))
    :mInstance(instance), mFunction(function){};

    virtual void Call(int fd, const short revent)
    {
    	(*mInstance.*mFunction)(fd,revent);
    };
};

/**
 * template to create the functor for a class
 */
template <class TClass> class TSpecificTimerCallback : public TBasicTimerCallback
{
private:
    TClass* mInstance;
    void (TClass::*mFunction)(SocketHandler::timerHandle_t handle);

public:
    TSpecificTimerCallback(TClass* instance, void(TClass::*function)(SocketHandler::timerHandle_t handle))
    :mInstance(instance), mFunction(function){};

    virtual void Call(SocketHandler::timerHandle_t handle)
    {
    	(*mInstance.*mFunction)(handle);
    }
};
} /* namespace am */
#endif /* SOCKETHANDLER_H_ */