diff --git a/Makefile b/Makefile index 9ff8493..d20318d 100644 --- a/Makefile +++ b/Makefile @@ -25,7 +25,7 @@ $(OBJDIR)/%.o : %.c Makefile $(HEADERS) $(OBJDIR)/twinklclient.so : $(OBJDIR)/twinklsocket.o $(OBJDIR)/message_wrapper.o $(CC) $+ -shared $(LDFLAGS) -o $@ -$(OBJDIR)/twinkl-client : $(OBJDIR)/main.o $(OBJDIR)/twinklsocket.o +$(OBJDIR)/twinkl-client : $(OBJDIR)/main.o $(OBJDIR)/twinklclient.so $(CC) $+ $(LDFLAGS) -o $@ clean : diff --git a/README.md b/README.md index 05a4fe6..d88c3c0 100644 --- a/README.md +++ b/README.md @@ -49,9 +49,9 @@ They can be connected to a twinkle-client process using a pipe. **Examples:** ``` -python2 fullwithe.py | ../bin/twinkl-client lightwall.lan 7 python2 gradient.py | ../bin/twinkl-client lightwall.lan 7 -python2 matrix.py | ../bin/twinkl-client lightwall.lan 7 +python2 fullwithe.py lightwall.lan 7 +python2 matrix.py lightwall.lan 7 python2 random.py | ../bin/twinkl-client ampel.lan 7 ``` @@ -67,11 +67,11 @@ It offers the following functions: * Functions for handling sockets */ -// Opens a 'connected' upd socket for this host and port, returns the filedescriptor +// Opens a 'connected' upd socket for this host and port. Returns the filedescriptor or -1 on error. int twinklsocket_open(const char *host, const char *port); -// Sends a twinkl message -void twinklsocket_send(int sockfd, const struct twinkl_message *message); +// Sends a twinkl message. Returns 0 on success else -1. +int twinklsocket_send(int sockfd, const struct twinkl_message *message); // Closes the socket void twinklsocket_close(int sockfd); @@ -119,12 +119,15 @@ Using the wrapper functions it is only necessary to deal with integers and point See `animations/twinklclient.py` and the example below for details. ```C -twinkl_server = "127.0.0.1" +twinkl_server = "127.0.0.1"; port = "1337"; -fd = twinklsocket_open(twinkl_server, port) +fd = twinklsocket_open(twinkl_server, port); +if(fd == -1) { + PANIC(); +} -msg = twinklmsg_create() +msg = twinklmsg_create(); twinklmsg_set_priority(msg, 0); @@ -132,7 +135,10 @@ twinklmsg_set_value(msg, 23, 42); twinklmsg_set_value(msg, 46, 5); // ... more stuff -twinklsocket_send(fd, msg); +result = twinklsocket_send(fd, msg); +if(result == -1) { + PANIC(); +} // Tear down diff --git a/animations/matrix.py b/animations/matrix.py index 15ad417..56f9921 100644 --- a/animations/matrix.py +++ b/animations/matrix.py @@ -1,8 +1,12 @@ #!/usr/bin/env python2 +import sys +import signal + from random import randint from time import sleep -from subprocess import Popen, PIPE + +from twinklclient import TwinklSocket, TwinklMessage WIDTH = 6 HEIGHT = 8 @@ -20,22 +24,17 @@ BOX_MAP = [ ] -channels = {} +msg = TwinklMessage() +socket = None +priority = 0 def set_box(x,y,r,g,b): if x >= 0 and y >= 0 and x < WIDTH and y < HEIGHT: base_address = BOX_MAP[y][x] - channels[base_address] = r - channels[base_address + 1] = g - channels[base_address + 2] = b - - -def output_channels(): - for channel, value in channels.items(): - print "%d : %d" % (channel, value) + msg[base_address] = int(r) + msg[base_address + 1] = int(g) + msg[base_address + 2] = int(b) - print "" - def clear(): for x in range(0, WIDTH): @@ -64,6 +63,29 @@ class Column(object): self.y += 1 + +def terminate(signal, frame): + msg.reset() + msg.set_priority(priority) + socket.send(msg) + + if socket: + socket.close() + msg.destroy() + sys.exit(0) + + +signal.signal(signal.SIGINT, terminate) + +if len(sys.argv) != 3: + print "Usage: %s host priority" % sys.argv[0] + sys.exit(1) + +socket = TwinklSocket(sys.argv[1], "1337") + +priority = int(sys.argv[2]) +msg.set_priority(priority) + columns = [] # Add some initial collums for i in range(0, 16): @@ -81,5 +103,5 @@ while(True): columns[i].update() columns[i].render() - output_channels() - sleep(0.050) + socket.send(msg) + sleep(0.05) diff --git a/animations/twinklclient.py b/animations/twinklclient.py index 59f61c5..f5c8b30 100644 --- a/animations/twinklclient.py +++ b/animations/twinklclient.py @@ -29,18 +29,23 @@ _TWINKL_CLIENT.twinklsocket_open.argtypes = [c_char_p, c_char_p] _TWINKL_CLIENT.twinklsocket_open.restype = c_int _TWINKL_CLIENT.twinklsocket_send.argtypes = [c_int, c_void_p] -_TWINKL_CLIENT.twinklsocket_send.restype = None +_TWINKL_CLIENT.twinklsocket_send.restype = c_int class TwinklSocket(object): def __init__(self, host, port): self._socket = _TWINKL_CLIENT.twinklsocket_open(host, port) + if self._socket < 0: + print self._socket + raise RuntimeError("Could not open socket.") def close(self): _TWINKL_CLIENT.twinklsocket_close(self._socket) def send(self, msg): - _TWINKL_CLIENT.twinklsocket_send(self._socket, msg._pointer) + result = _TWINKL_CLIENT.twinklsocket_send(self._socket, msg._pointer) + if result < 0: + raise RuntimeError("Could not send packet") class TwinklMessage(object): diff --git a/include/twinklsocket.h b/include/twinklsocket.h index 831c5dc..e6b6f50 100644 --- a/include/twinklsocket.h +++ b/include/twinklsocket.h @@ -5,7 +5,7 @@ int twinklsocket_open(const char *host, const char *port); -void twinklsocket_send(int sockfd, const struct twinkl_message *message); +int twinklsocket_send(int sockfd, const struct twinkl_message *message); void twinklsocket_close(int sockfd); diff --git a/main.c b/main.c index 008e4a9..4305d93 100644 --- a/main.c +++ b/main.c @@ -27,6 +27,9 @@ int main(int argc, char *argv[]) } twinklsocket = twinklsocket_open(argv[1], port); + if(twinklsocket == -1) { + exit(1); + } struct twinkl_message msg; @@ -47,14 +50,18 @@ int main(int argc, char *argv[]) /* * Line format: : - * With abitrary many spaces btween the numbers and the colon + * With arbitrary many spaces between the numbers and the colon * Example: "5 : 42" */ while(!feof(stdin)) { // Try to read a line from stdin, if the line is empty send the packet if(getline(&line, &len, stdin) <= 1) { - twinklsocket_send(twinklsocket, &msg); + int result = twinklsocket_send(twinklsocket, &msg); + if(result != 0) { + printf("There was an error while sending the twinkl packet.\n"); + exit(1); + } printf("Twinkl paket sent.\n"); twinkl_init_message(&msg); diff --git a/twinklsocket.c b/twinklsocket.c index 04415dc..dc12da7 100644 --- a/twinklsocket.c +++ b/twinklsocket.c @@ -24,7 +24,7 @@ int twinklsocket_open(const char *host, const char *port) { result = getaddrinfo(host, port, &hints, &servinfo); if(result != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(result)); - exit(1); + return -1; } // loop through all the results and make a socket @@ -37,13 +37,13 @@ int twinklsocket_open(const char *host, const char *port) { if (p == NULL) { fprintf(stderr, "failed to create socket\n"); - exit(1); + return -1; } result = connect(sockfd, p->ai_addr, p->ai_addrlen); if(result != 0) { perror("connect"); - exit(1); + return -1; } freeaddrinfo(servinfo); @@ -51,14 +51,16 @@ int twinklsocket_open(const char *host, const char *port) { return sockfd; } -void twinklsocket_send(int sockfd, const struct twinkl_message *message) { +int twinklsocket_send(int sockfd, const struct twinkl_message *message) { int numbytes; numbytes = send(sockfd, message, sizeof(struct twinkl_message), 0); if (numbytes == -1) { perror("sendto"); - exit(1); + return -1; } + + return 0; } void twinklsocket_close(int sockfd) {