-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlircsocket.py
215 lines (210 loc) · 5.68 KB
/
lircsocket.py
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/python
import sys
import os
import argparse
import socket
import select
import signal
server_address = '/run/lirc/lircd'
eventlircd_address = '/run/lirc/eventlircd'
lircd_address = '/run/lirc/lircd0'
size=4096
timeout=1000
debug=False
verbose=False
parser = argparse.ArgumentParser(description='interconnect eventlircd and lircd to get lircd (ir blasting) and eventlircd to work over a single socket.')
parser.add_argument('-e','--eventlircd',help='socket for eventlircd')
parser.add_argument('-l','--lircd',help='socket for lircd')
parser.add_argument('-s','--socket',help='listening socket for me')
parser.add_argument('-v','--verbose',help='be a bit verbose',action='store_true',default=False)
parser.add_argument('-d','--debug',help='debug communication',action='store_true',default=False)
args = parser.parse_args()
if args.eventlircd:
eventlircd_address=args.eventlircd
if args.lircd:
lircd_address=args.lircd
if args.socket:
server_address=args.socket
if args.socket:
server_address=args.socket
if args.debug:
debug=True
if args.verbose:
verbose=True
del args
del parser
eventlircd=None
lircd = None
def cleanup():
if verbose:
print "server shutdown"
server.close()
if not lircd is None:
lircd.close()
if not lircd is None:
eventlircd.close()
def signal_shutdown(signal, frame):
cleanup()
sys.exit(0)
try:
os.unlink(server_address)
except OSError:
if os.path.exists(server_address):
raise
server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
signal.signal(signal.SIGINT, signal_shutdown)
signal.signal(signal.SIGTERM, signal_shutdown)
signal.signal(signal.SIGQUIT, signal_shutdown)
server.bind(server_address)
if verbose:
print 'Listening on %s' % server_address
server.listen(1)
server.setblocking(0)
input = [server]
output = []
running = 1
while running:
if eventlircd is None:
eventlircd = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
try:
eventlircd.connect(eventlircd_address)
input.append(eventlircd)
if verbose:
print 'connected to %s' % eventlircd_address
except socket.error, msg:
if verbose:
print >>sys.stderr, '%s: %s' % (eventlircd_address,msg)
eventlircd=None
if lircd is None:
lircd = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
try:
lircd.connect(lircd_address)
input.append(lircd)
if verbose:
print 'connected to lircd %s' % lircd_address
except socket.error, msg:
if verbose:
print >>sys.stderr, '%s: %s' % (lircd_address,msg)
lircd=None
# process sockets
inputready,outputready,exceptready = select.select(input,[],[],timeout)
for s in inputready:
if s == server:
# handle the server socket
if verbose:
print "new connection on %s" % server_address
client, address = server.accept()
input.append(client)
output.append(client)
elif s == lircd:
if debug:
print "reading from lircd"
try:
data = s.recv(size)
except:
print "failed to receive from lircd:"
data=None
if data:
if debug:
print "lircd: '%s'" % data
for o in output:
try:
o.send(data)
except:
if verbose:
print >>sys.stderr, 'failed to send data to client'
try:
input.remove(o)
except:
print >>sys.stderr, 'could not cleanup input'
try:
o.close()
except:
print >>sys.stderr, 'could not close socket'
try:
output.remove(o)
except:
print >>sys.stderr, 'could not cleanup output'
else:
if verbose:
print "connection to %s lost" % lircd_address
s.close()
input.remove(s)
lircd=None
elif s == eventlircd:
if debug:
print "reading from eventlircd"
try:
data = s.recv(size)
except:
data=None
if data:
if debug:
print "eventlircd: '%s'" % data
for o in output:
try:
o.send(data)
except:
if verbose:
print >>sys.stderr, 'failed to send data to client'
output.remove(o)
input.remove(o)
s.close()
else:
if verbose:
print "connection to %s lost" % eventlircd_address
s.close()
input.remove(s)
eventlircd=None
else:
# handle all other sockets
try:
if debug:
print "reading from client"
data = s.recv(size)
except:
data = None
if data:
if debug:
print "client: '%s'" % data
if lircd:
try:
lircd.send(data)
if debug:
print "sent to lircd"
data=lircd.recv(size)
if debug:
print "read from lircd '%s'" % data
except:
print >>sys.stderr, 'failed to send data to lircd'
try:
lircd.close()
except:
if debug:
print "cannot close lircd"
lircd=None
data=None
try:
s.send(data)
if debug:
print "sent to client '%s'" % data
except:
print "cannot sent answer to client '%s'" % data
else:
if verbose:
print "client disconnected"
try:
s.close()
except:
if debug:
print "cannot close socket"
try:
input.remove(s)
except:
if debug:
print "cannot remove input"
try:
output.remove(s)
except:
if debug:
print "cannot remove output"