Subversion Repositories HomeAutomation

Rev

Rev 981 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 981 Rev 983
1
/***************************************************************************
1
/***************************************************************************
2
 *   Copyright (C) December 6, 2008 by Mattias Runge                             *
2
 *   Copyright (C) December 6, 2008 by Mattias Runge                             *
3
 *   mattias@runge.se                                                      *
3
 *   mattias@runge.se                                                      *
4
 *   asyncsocket.h                                            *
4
 *   asyncsocket.h                                            *
5
 *                                                                         *
5
 *                                                                         *
6
 *   This program is free software; you can redistribute it and/or modify  *
6
 *   This program is free software; you can redistribute it and/or modify  *
7
 *   it under the terms of the GNU General Public License as published by  *
7
 *   it under the terms of the GNU General Public License as published by  *
8
 *   the Free Software Foundation; either version 2 of the License, or     *
8
 *   the Free Software Foundation; either version 2 of the License, or     *
9
 *   (at your option) any later version.                                   *
9
 *   (at your option) any later version.                                   *
10
 *                                                                         *
10
 *                                                                         *
11
 *   This program is distributed in the hope that it will be useful,       *
11
 *   This program is distributed in the hope that it will be useful,       *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14
 *   GNU General Public License for more details.                          *
14
 *   GNU General Public License for more details.                          *
15
 *                                                                         *
15
 *                                                                         *
16
 *   You should have received a copy of the GNU General Public License     *
16
 *   You should have received a copy of the GNU General Public License     *
17
 *   along with this program; if not, write to the                         *
17
 *   along with this program; if not, write to the                         *
18
 *   Free Software Foundation, Inc.,                                       *
18
 *   Free Software Foundation, Inc.,                                       *
19
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
20
 ***************************************************************************/
20
 ***************************************************************************/
21
 
21
 
22
#ifndef _ASYNCSOCKET_H
22
#ifndef _ASYNCSOCKET_H
23
#define _ASYNCSOCKET_H
23
#define _ASYNCSOCKET_H
24
 
24
 
25
using namespace std;
25
using namespace std;
26
 
26
 
27
#include <string>
27
#include <string>
28
#include <queue>
28
#include <queue>
29
#include <map>
29
#include <map>
30
 
30
 
31
#include <sys/types.h>
31
#include <sys/types.h>
32
#include <sys/socket.h>
32
#include <sys/socket.h>
33
#include <netinet/in.h>
33
#include <netinet/in.h>
34
#include <netdb.h>
34
#include <netdb.h>
35
#include <unistd.h>
35
#include <unistd.h>
36
#include <string>
36
#include <string>
37
#include <arpa/inet.h>
37
#include <arpa/inet.h>
38
#include <string.h>
38
#include <string.h>
39
#include <errno.h>
39
#include <errno.h>
40
#include <fcntl.h>
40
#include <fcntl.h>
41
#include <sys/ioctl.h>
41
#include <sys/ioctl.h>
42
#include <signal.h>
42
#include <signal.h>
43
#include <netinet/tcp.h>
43
#include <netinet/tcp.h>
44
 
44
 
45
 
45
 
46
#include "../Threads/thread.h"
46
#include "../Threads/thread.h"
47
#include "../Threads/semaphore.h"
47
#include "../Threads/semaphore.h"
48
#include "../Threads/threadsafequeue.h"
48
#include "../Threads/threadsafequeue.h"
49
#include "../Tools/tools.h"
49
#include "../Tools/tools.h"
50
#include "../SyslogStream/syslogstream.h"
50
#include "../SyslogStream/syslogstream.h"
51
 
51
 
52
#include "socketexception.h"
52
#include "socketexception.h"
53
 
53
 
54
#define ASYNCSOCKET_EVENT_NONE 0
54
#define ASYNCSOCKET_EVENT_NONE 0
55
#define ASYNCSOCKET_EVENT_DATA 1
55
#define ASYNCSOCKET_EVENT_DATA 1
-
 
56
#define ASYNCSOCKET_EVENT_CONNECTED 2
-
 
57
#define ASYNCSOCKET_EVENT_CONNECT_FAILED 3
-
 
58
#define ASYNCSOCKET_EVENT_WAITING_TO_RECONNECT 4
56
#define ASYNCSOCKET_EVENT_CLOSED 2
59
#define ASYNCSOCKET_EVENT_CLOSED 5
-
 
60
#define ASYNCSOCKET_EVENT_RESET 6
57
#define ASYNCSOCKET_EVENT_DIED 3
61
#define ASYNCSOCKET_EVENT_DIED 7
58
#define ASYNCSOCKET_EVENT_INACTIVITY 4
62
#define ASYNCSOCKET_EVENT_INACTIVITY 8
59
 
63
 
60
const int MAXBUFFER = 1024;
64
const int MAXBUFFER = 1024;
61
const int MAXCONNECTIONS = 10;
65
const int MAXCONNECTIONS = 10;
62
 
66
 
63
class AsyncSocket : public Thread<AsyncSocket>
67
class AsyncSocket : public Thread<AsyncSocket>
64
{
68
{
65
public:
69
public:
66
    AsyncSocket();
70
    AsyncSocket();
67
    ~AsyncSocket();
71
    ~AsyncSocket();
68
 
72
 
69
    void run();
73
    void run();
70
 
74
 
71
    void setReconnectTimeout(unsigned int timeout);
75
    void setReconnectTimeout(unsigned int timeout);
72
    void setAddress(string address, int port);
76
    void setAddress(string address, int port);
73
    void setPort(int port);
77
    void setPort(int port);
74
    void setSocket(int socket);
78
    void setSocket(int socket);
75
    int getSocket();
79
    int getSocket();
76
 
80
 
-
 
81
    bool availableEvent();
77
    void startEvent();
82
    void startEvent();
78
    int getEvent();
83
    int getEvent();
79
    void waitForEvent();
84
    void waitForEvent();
80
    void stopEvent();
85
    void stopEvent();
81
 
86
 
82
    bool availableData();
87
    bool availableData();
83
    string getData();
88
    string getData();
84
    bool sendData(string data);
89
    bool sendData(string data);
85
    void sendDataDirect(string data);
90
    void sendDataDirect(string data);
86
    void forceReconnect();
91
    void forceReconnect();
87
    void startListen();
92
    void startListen();
88
    bool accept(AsyncSocket* newSocket);
93
    bool accept(AsyncSocket* newSocket);
89
    bool isConnected();
94
    bool isConnected();
90
 
95
 
91
    static void signalHandler(int signum);
96
    //static void signalHandler(int signum);
92
    string getId() { return myId; };
97
    string getId() { return myId; };
93
 
98
 
94
    static Mutex mySocketsMutex;
99
    //static Mutex mySocketsMutex;
95
    static map<string, AsyncSocket*> mySockets;
100
    //static map<string, AsyncSocket*> mySockets;
96
    Semaphore mySemaphore;
101
    //static Semaphore mySemaphore;
97
 
102
 
98
protected:
103
protected:
99
    void reconnectLoop();
104
    void reconnectLoop();
100
    void connect();
105
    void connect();
101
    void create();
106
    void create();
102
    void close();
107
    void close();
103
    void setEvent(int event);
108
    void setEvent(int event);
104
 
109
 
105
 
110
 
106
 
111
 
107
private:
112
private:
108
    string myId;
113
    string myId;
109
    Semaphore myEventSemaphore;
114
    Semaphore myEventSemaphore;
110
    int myEvent;
115
    ThreadSafeQueue<int> myEventQueue;
111
 
116
 
112
    bool myForceReconnect;
117
    bool myForceReconnect;
113
   
118
   
114
    ThreadSafeQueue<string> myInQueue;
119
    ThreadSafeQueue<string> myInQueue;
115
    ThreadSafeQueue<string> myOutQueue;
120
    //ThreadSafeQueue<string> myOutQueue;
116
 
121
 
117
    int mySocket;
122
    int mySocket;
118
    sockaddr_in myAddressStruct;
123
    sockaddr_in myAddressStruct;
119
    string myAddress;
124
    string myAddress;
120
    int myPort;
125
    int myPort;
121
    unsigned int myReconnectTimeout;
126
    unsigned int myReconnectTimeout;
122
};
127
};
123
 
128
 
124
#endif  /* _ASYNCSOCKET_H */
129
#endif  /* _ASYNCSOCKET_H */
125
 
130
 
126
 
131