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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
# mainloop/socketsrc.py -- events and event sources for sockets
#
# Copyright (C) 2012, 2014 Codethink Limited
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA..
import fcntl
import logging
import os
import socket
import distbuild
from eventsrc import EventSource
def set_nonblocking(handle):
'''Make a socket, file descriptor, or other such thing be non-blocking.'''
if type(handle) is int:
fd = handle
else:
fd = handle.fileno()
flags = fcntl.fcntl(fd, fcntl.F_GETFL, 0)
flags = flags | os.O_NONBLOCK
fcntl.fcntl(fd, fcntl.F_SETFL, flags)
class SocketError(object):
'''An error has occured with a socket.'''
def __init__(self, sock, exception):
self.sock = sock
self.exception = exception
class NewConnection(object):
'''A new client connection.'''
def __init__(self, connection, addr):
self.connection = connection
self.addr = addr
class ListeningSocketEventSource(EventSource):
'''An event source for a socket that listens for connections.'''
def __init__(self, addr, port):
self.sock = distbuild.create_socket()
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
logging.info('Binding socket to %s', addr)
self.sock.bind((addr, port))
self.sock.listen(5)
self._accepting = True
logging.info('Listening at %s' % self.sock.remotename())
def get_select_params(self):
r = [self.sock.fileno()] if self._accepting else []
return r, [], [], None
def get_events(self, r, w, x):
if self._accepting and self.sock.fileno() in r:
try:
conn, addr = self.sock.accept()
except socket.error, e:
return [SocketError(self.sock, e)]
else:
logging.info(
'New connection to %s from %s' %
(conn.getsockname(), addr))
return [NewConnection(conn, addr)]
return []
def start_accepting(self):
self._accepting = True
def stop_accepting(self):
self._accepting = False
class SocketReadable(object):
'''A socket is readable.'''
def __init__(self, sock):
self.sock = sock
class SocketWriteable(object):
'''A socket is writeable.'''
def __init__(self, sock):
self.sock = sock
class SocketEventSource(EventSource):
'''Event source for normal sockets (for I/O).
This generates events for indicating the socket is readable or
writeable. It does not actually do any I/O itself, that's for the
handler of the events. There are, however, methods for doing the
reading/writing, and for closing the socket.
The event source can be told to stop checking for readability
or writeability, so that the user may, for example, stop those
events from being triggered while a buffer is full.
'''
def __init__(self, sock):
self.sock = sock
self._reading = True
self._writing = True
set_nonblocking(sock)
def __repr__(self):
return '<SocketEventSource at %x: socket %s>' % (id(self), self.sock)
def get_select_params(self):
r = [self.sock.fileno()] if self._reading else []
w = [self.sock.fileno()] if self._writing else []
return r, w, [], None
def get_events(self, r, w, x):
events = []
fd = self.sock.fileno()
if self._reading and fd in r:
events.append(SocketReadable(self))
if self._writing and fd in w:
events.append(SocketWriteable(self))
return events
def start_reading(self):
self._reading = True
def stop_reading(self):
self._reading = False
def start_writing(self):
self._writing = True
def stop_writing(self):
self._writing = False
def read(self, max_bytes):
fd = self.sock.fileno()
return os.read(fd, max_bytes)
def write(self, data):
fd = self.sock.fileno()
return os.write(fd, data)
def close(self):
self.stop_reading()
self.stop_writing()
self.sock.close()
self.sock = None
def is_finished(self):
return self.sock is None
|