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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
# -*- coding: utf-8 -*-
#
# Copyright (c) 2010 Martin S. <opensuse@sukimashita.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
import asyncore
import platform
import socket
import threading
import time
from datetime import datetime, date
from urlparse import urlparse
from ZeroconfService import ZeroconfService
__all__ = ["BaseAirPlayRequest", "AirPlayService", "AirPlayProtocolHandler"]
class BaseAirPlayRequest(object):
def read_from_socket(self, socket):
data = socket.recv(1024)
if not data:
return False
# we split the message into HTTP headers and content body
message = data.split("\r\n\r\n", 1)
headers = message[0]
headerlines = headers.splitlines()
# parse request headers
command = headerlines[0].split()
self.type = command[0]
self.uri = command[1]
self.version = command[2]
del headerlines[0]
self.headers = self.parse_headers(headerlines)
# parse any uri query parameters
self.params = None
if (self.uri.find('?')):
url = urlparse(self.uri)
if (url[4] is not ""):
self.params = dict([part.split('=') for part in url[4].split('&')])
self.uri = url[2]
# parse message body
if (int(self.headers['Content-Length']) > 0):
self.body = message[1]
# read more data if we have to
if len(self.body) < int(self.headers['Content-Length']):
while 1:
data = socket.recv(8192)
if not data:
break
self.body = self.body + data
return True
def parse_headers(self, lines):
headers = {}
for line in lines:
if line:
name, value = line.split(": ", 1)
headers[name.strip()] = value.strip()
return headers
class AirPlayProtocolHandler(asyncore.dispatcher_with_send):
def __init__(self, socket, service):
asyncore.dispatcher_with_send.__init__(self, socket)
self.service = service
def handle_read(self):
# read from the socket and parse a HTTP request
request = BaseAirPlayRequest()
if (not request.read_from_socket(self)):
return
answer = ""
# process the request and run the appropriate callback
if (request.uri.find('/play')>-1):
parsedbody = request.parse_headers(request.body.splitlines())
self.service.play(parsedbody['Content-Location'], float(parsedbody['Start-Position']))
answer = self.create_request()
elif (request.uri.find('/stop')>-1):
self.service.stop(request.headers)
answer = self.create_request()
elif (request.uri.find('/scrub')>-1):
if request.type == 'GET':
d, p = self.service.get_scrub()
content = "duration: " + str(float(d))
content += "\nposition: " + str(float(p))
answer = self.create_request(200, content)
elif request.type == 'POST':
self.service.set_scrub(float(request.params['position']))
answer = self.create_request()
elif (request.uri.find('/reverse')>-1):
self.service.reverse(request.headers)
answer = self.create_request(101)
elif (request.type == 'POST' and request.uri.find('/rate')>-1):
self.service.rate(float(request.params['value']))
answer = self.create_request()
elif (request.type == 'PUT' and request.uri.find('/photo')>-1):
self.photo(request.body, request.headers['X-Apple-Transition'])
answer = self.create_request()
else:
print "ERROR: AirPlay - Unable to handle request \"%s\"" % (request.uri)
answer = self.create_request(404)
if(answer is not ""):
self.send(answer)
def getDateTime(self):
today = datetime.now()
datestr = today.strftime("%a, %d %b %Y %H:%M:%S")
return datestr+" GMT"
def create_request(self, status = 200, body = ""):
clength = len(bytes(body))
if (status == 200):
answer = "HTTP/1.1 200 OK"
elif (status == 404):
answer = "HTTP/1.1 404 Not Found"
elif (status == 101):
answer = "HTTP/1.1 101 Switching Protocols"
answer += "\nUpgrade: PTTH/1.0"
answer += "\nConnection: Upgrade"
answer += "\nDate: " + self.getDateTime()
answer += "\nContent-Length: " + str(clength)
answer +="\n\n"
answer += body
return answer
def get_scrub(self):
return False
def set_scrub(self, position):
return False
def play(self, location, position):
return False
def stop(self, info):
return False
def reverse(self, info):
return True
def photo(self, data, transition):
return False
def rate(self, speed):
return False
def volume(self, info):
return False
def authorize(self, info):
return False
def event(self, info):
return False
class AsyncoreThread(threading.Thread):
def __init__(self, timeout=30.0, use_poll=0,map=None):
self.flag = True
self.timeout = 30.0
self.use_poll = use_poll
self.map = map
threading.Thread.__init__(self, None, None, 'asyncore thread')
def run(self):
self.loop()
def loop(self):
if self.map is None:
self.map = asyncore.socket_map
if self.use_poll:
if hasattr(select, 'poll'):
poll_fun = asyncore.poll3
else:
poll_fun = asyncore.poll2
else:
poll_fun = asyncore.poll
while self.map and self.flag:
poll_fun(self.timeout,self.map)
def end(self):
self.flag=False
self.map=None
class AirPlayService(asyncore.dispatcher):
def __init__(self, name=None, host="0.0.0.0", port=22555):
# create socket server
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.set_reuse_addr()
self.bind((host, port))
self.listen(5)
self.remote_clients = []
# create avahi service
if (name is None):
name = "Airplay Service on " + platform.node()
self.zeroconf_service = ZeroconfService(name, port=port, stype="_airplay._tcp", text=["Name="+name])
# publish avahi service
self.zeroconf_service.publish()
# do this so we do not block the main thread
self.thread = AsyncoreThread(timeout=30)
self.thread.is_finished = False
self.thread.start()
print "AirPlayService running"
def handle_accept(self):
pair = self.accept()
if pair is None:
pass
else:
sock, addr = pair
self.remote_clients.append(AirPlayProtocolHandler(sock, self))
def handle_close(self):
self.close()
def __del__(self):
self.thread.end()
self.close()
del self.thread
# unpublish avahi service
self.zeroconf_service.unpublish()
|