-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
172 lines (153 loc) · 4.21 KB
/
client.py
File metadata and controls
172 lines (153 loc) · 4.21 KB
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
#!/usr/bin/env python
import ao
import mad
import readline
import socket
import struct
import sys
import time
import threading
import Queue
kill = threading.Event()
"""
Allows use to MAD audio library for streaming
"""
class mywrapper(object):
def __init__(self):
self.data = ""
# When it asks to read a specific size, give it that many bytes, and
# update our remaining data.
def read(self, size):
result = self.data[:size]
self.data = self.data[size:]
return result
"""
Displays list of available songs
"""
def show_list(sock, header):
size = int(header[5:len(header)-4])
data = recv_data(sock,size)
lists = data[5:].split("/")
lists = filter(None, lists)
print "\n"
for line in lists:
combined = line.split(",")
print "({0}) {1}".format(combined[0], combined[1])
"""
Displays current info
"""
def show_info(sock, header):
size = int(header[5:len(header)-4])+5
data = ""
while True:
data += sock.recv(size)
if len(data) >= size:
break
print "\n-- Info --\n\n{0}\n".format(data[5:size])
"""
Asks for music and then plays it
"""
def play_func(wrap, cond_connected):
audio_device = ao.AudioDevice('pulse')
stream = True
mf = mad.MadFile(wrap)
time.sleep(.5)
cond_connected.acquire()
cond_connected.wait()
cond_connected.release()
while not kill.is_set():
buf = mf.read()
if buf is not None:
# Tell the audio device to play our decoded audio.
audio_device.play(buf, len(buf))
else:
sys.exit(0)
sys.exit(0)
"""
Helper function for receiving data from server
"""
def recv_data(sock, size):
length = 0
new_data = ""
while True:
received = sock.recv(size-length)
length+= len(received)
new_data += received
if length >= size:
return new_data
"""
Fetch data from server
"""
def recv_func(wrap, sock, cond_connected):
while True:
data = recv_data(sock, 13)
if data[:4] == "list":
print "here"
show_list(sock, data)
elif data[:4] == "info":
show_info(sock, data)
elif data[:5] == "play ":
length = 0
new_data = ""
received = recv_data(sock,5000)
cond_connected.acquire()
wrap.data+=received
cond_connected.notify()
cond_connected.release()
elif data[:5] == "play-":
data = data[6:]
data = data[:data.find(" ")]
received = recv_data(sock,int(data))
cond_connected.acquire()
wrap.data+=received
print len(received)
cond_connected.notify()
cond_connected.release()
def stop(wrap):
kill.set()
kill.clear()
wrap.data =""
time.sleep(1)
def main():
global playing
if len(sys.argv) < 3:
print 'Usage: %s <server name/ip> <server port>' % sys.argv[0]
sys.exit(1)
wrap = mywrapper()
cond_connected = threading.Condition()
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
recv_thread = threading.Thread(target=recv_func, args=(wrap,sock, cond_connected))
recv_thread.daemon = True
sock.connect((sys.argv[1], int(sys.argv[2])))
data = recv_data(sock,9)
if "connect success" in data:
pass
else:
print "error conntecting"
sys.exit(0)
recv_thread.start()
print "To begin, please enter a command\nPlay # - Info # - List - Stop\n"
while True:
line = raw_input('>> ')
line = line.lower()
if line in ['quit', 'q', 'exit']:
sys.exit(0)
elif line == "list":
sock.sendall("list")
time.sleep(.01)
elif "info" in line:
sock.sendall(line)
time.sleep(.01)
elif "play" in line:
if playing:
stop(wrap)
playing = True
play_thread = threading.Thread(target=play_func, args=(wrap, cond_connected))
play_thread.daemon = True
play_thread.start()
sock.sendall(line)
elif "stop" in line:
stop(wrap)
if __name__ == '__main__':
playing = False
main()