From b3606eb073651b92a7f3ba7df56d6a7d697bcc13 Mon Sep 17 00:00:00 2001 From: Grissess Date: Sun, 14 Jun 2015 04:30:59 -0400 Subject: Added a major packet type and cleaned up --- .gitignore | 1 + broadcast.py | 10 ++++++++-- client.c | 30 +++++++++++++++++++++++++++--- client.py | 29 ++++++++++++++++++++++++----- packet.py | 11 +++++++++-- packet.pyc | Bin 1423 -> 0 bytes 6 files changed, 69 insertions(+), 12 deletions(-) delete mode 100644 packet.pyc diff --git a/.gitignore b/.gitignore index 58448e3..1f96c55 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ client *.xml *.swp *.swo +*.pyc diff --git a/broadcast.py b/broadcast.py index f244aa6..fe2b466 100644 --- a/broadcast.py +++ b/broadcast.py @@ -5,7 +5,7 @@ import time import xml.etree.ElementTree as ET import threading -from packet import Packet, CMD +from packet import Packet, CMD, itos PORT = 13676 if len(sys.argv) > 2: @@ -32,7 +32,13 @@ except socket.timeout: print 'Clients:' for cl in clients: - print cl + print cl, + s.sendto(str(Packet(CMD.CAPS)), cl) + data, _ = s.recvfrom(4096) + pkt = Packet.FromStr(data) + print 'ports', pkt.data[0], + print 'type', itos(pkt.data[1]), + print 'uid', ''.join([itos(i) for i in pkt.data[2:]]).rstrip('\x00') if sys.argv[1] == '-t': s.sendto(str(Packet(CMD.PLAY, 0, 250000, 440, 255)), cl) time.sleep(0.25) diff --git a/client.c b/client.c index 6e4ac34..830ef19 100644 --- a/client.c +++ b/client.c @@ -13,12 +13,16 @@ #define CLK_FREQ 1193180 int term; +static int ident = 0x42454550; -enum cmd_t {CMD_KA, CMD_PING, CMD_QUIT, CMD_PLAY}; +enum cmd_t {CMD_KA, CMD_PING, CMD_QUIT, CMD_PLAY, CMD_CAPS}; struct cmd_buffer { int cmd; - int data[8]; + union { + int data[8]; + char string[8*sizeof(int)]; + }; }; void sigalrm(int sig) { @@ -27,11 +31,15 @@ void sigalrm(int sig) { int main(int argc, char **argv) { struct sockaddr_in addr, remote; - int sock, rlen = sizeof(remote), i; + int sock, rlen = sizeof(remote), i, len_uid = 0; struct itimerval tmr; struct cmd_buffer cmd; struct sigaction sa; + if(argc > 1) { + len_uid = strlen(argv[1]); + } + if((term = open("/dev/console", O_WRONLY)) < 0) { perror("open"); return 1; @@ -80,6 +88,22 @@ int main(int argc, char **argv) { setitimer(ITIMER_REAL, &tmr, NULL); ioctl(term, KIOCSOUND, (int) (CLK_FREQ / cmd.data[2])); break; + + case CMD_CAPS: + cmd.data[0] = 1; + cmd.data[1] = ident; + for(i = 0; i < 6 * sizeof(int); i++) { + if(argc > 1 && i < len_uid) { + cmd.string[i+8] = argv[1][i]; + } else { + cmd.string[i+8] = '\0'; + } + } + for(i = 0; i < 8; i++) { + cmd.data[i] = htonl(cmd.data[i]); + } + sendto(sock, &cmd, sizeof(cmd), 0, (struct sockaddr *) &remote, rlen); + break; default: printf("WARNING: Unknown cmd %d\n", cmd.cmd); 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 diff --git a/packet.py b/packet.py index 951425c..aefa758 100644 --- a/packet.py +++ b/packet.py @@ -6,7 +6,7 @@ class Packet(object): def __init__(self, cmd, *data): self.cmd = cmd self.data = data - if len(data) >= 8: + if len(data) > 8: raise ValueError('Too many data') self.data = list(self.data) + [0] * (8-len(self.data)) @classmethod @@ -20,4 +20,11 @@ class CMD: KA = 0 # No important data PING = 1 # Data are echoed exactly QUIT = 2 # No important data - PLAY = 3 # seconds, microseconds, frequency (Hz), amplitude (0-255) + PLAY = 3 # seconds, microseconds, frequency (Hz), amplitude (0-255), port + CAPS = 4 # ports, client type (1), user ident (2-7) + +def itos(i): + return struct.pack('>L', i) + +def stoi(s): + return struct.unpack('>L', s)[0] diff --git a/packet.pyc b/packet.pyc deleted file mode 100644 index ea0785b..0000000 Binary files a/packet.pyc and /dev/null differ -- cgit v1.2.3-70-g09d2