aboutsummaryrefslogtreecommitdiff
path: root/client.py
diff options
context:
space:
mode:
authorGrissess <grissess@nexusg.org>2015-06-14 04:30:59 -0400
committerGrissess <grissess@nexusg.org>2015-06-14 04:30:59 -0400
commitb3606eb073651b92a7f3ba7df56d6a7d697bcc13 (patch)
treeec18209d58b63f18cbaaa7aaaed093ec2fe3627e /client.py
parent0e2f4d381644e062b840ec27cecd18e43b0c94be (diff)
Added a major packet type and cleaned up
Diffstat (limited to 'client.py')
-rw-r--r--client.py29
1 files changed, 24 insertions, 5 deletions
diff --git a/client.py b/client.py
index 9ffb292..23244aa 100644
--- a/client.py
+++ b/client.py
@@ -9,9 +9,15 @@ import math
import struct
import socket
-from packet import Packet, CMD
+from packet import Packet, CMD, stoi
PORT = 13676
+STREAMS = 1
+IDENT = 'TONE'
+if len(sys.argv) > 1:
+ UID = sys.argv[1].rfill(24, '\x00')
+else:
+ UID = '\x00'*24
LAST_SAMP = 0
FREQ = 0
@@ -63,23 +69,36 @@ pa = pyaudio.PyAudio()
stream = pa.open(rate=RATE, channels=1, format=pyaudio.paInt32, output=True, frames_per_buffer=FPB, stream_callback=gen_data)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+sock.bind(('', PORT))
signal.signal(signal.SIGALRM, sigalrm)
while True:
- data, cli = sock.recvfrom(4096)
+ data = ''
+ while not data:
+ try:
+ data, cli = sock.recvfrom(4096)
+ except socket.error:
+ pass
pkt = Packet.FromStr(data)
print 'From', cli, 'command', pkt.cmd
- if pkt.cmd == KA:
+ if pkt.cmd == CMD.KA:
pass
- elif pkt.cmd == PING:
+ elif pkt.cmd == CMD.PING:
sock.sendto(data, cli)
elif pkt.cmd == CMD.QUIT:
break
elif pkt.cmd == CMD.PLAY:
dur = pkt.data[0]+pkt.data[1]/1000000.0
FREQ = pkt.data[2]
- AMP = MAX * (pkt.data[2]/255.0)
+ AMP = MAX * (pkt.data[3]/255.0)
signal.setitimer(signal.ITIMER_REAL, dur)
+ elif pkt.cmd == CMD.CAPS:
+ data = [0] * 8
+ data[0] = STREAMS
+ data[1] = stoi(IDENT)
+ for i in xrange(len(UID)/4):
+ data[i+2] = stoi(UID[4*i:4*(i+1)])
+ sock.sendto(str(Packet(CMD.CAPS, *data)), cli)
else:
print 'Unknown cmd', pkt.cmd