From a001e41a8b89b26a5fedb991f9ac531411346560 Mon Sep 17 00:00:00 2001 From: Jan Klopper Date: Fri, 16 Jun 2017 15:14:11 +0200 Subject: [PATCH] Clean up some code, add __del__ handler, make methods and names more clear for inexperienced users, handle keyboard exits, add server autodiscovery questions if multiple servers are found --- client.py | 34 +++++++------ sdlvloed.py | 134 ---------------------------------------------------- vloed.py | 57 ++++++++++++++++------ 3 files changed, 58 insertions(+), 167 deletions(-) diff --git a/client.py b/client.py index e0e96d0..adb2857 100755 --- a/client.py +++ b/client.py @@ -11,15 +11,18 @@ __author__ = "Jan Klopper " import random from vloed import PixelVloedClient, Packet, RGBPixel, MAX_PIXELS -def RandomFill(pixels, width, height): +def RandomFill(screen, width, height): """Generates a random number of pixels with a random color""" - for pixel in xrange(0, random.randint(10, MAX_PIXELS)): - pixel = RGBPixel(random.randint(0, width), - random.randint(0, height), - random.randint(0, 255), - random.randint(0, 255), - random.randint(0, 255)) - pixels.append(pixel) + for _x in xrange(1, # at least one pixel + random.randint(10, MAX_PIXELS) # at most the max number of pixels + ): + pixel = RGBPixel(random.randint(0, width), # select a random position on the width of the screen + random.randint(0, height), # select a random position on the height of the screen + random.randint(0, 255), # select a random value for red + random.randint(0, 255), # select a random value for green + random.randint(0, 255) # select a random value for blue + ) + screen.show(pixel) # lets push the pixel to the screen! def RunClient(options): """Discover the servers and start sending to the first one""" @@ -32,19 +35,14 @@ def RunClient(options): options.height # Screen pixels height, None for autodetect ) + # Lets create a screen which buffers the pixels we add to it, and sends them to the actual screen. + screen = Packet(client) # loop the effect until we cancel by pressing ctrl+c / exit the program - while True: + while screen: - # packet will automatically send its pixels if gets to the maximum pixel length - pixels = Packet(client) - - # add some pixels to the output with our functions + # add some pixels to the screen with our functions # the width/height are read from the client's config - RandomFill(pixels, client.width, client.height) - - # send whatever pixels are left in the packet - pixels.flush() - + RandomFill(screen, client.width, client.height) if __name__ == '__main__': # if this script is called from the command line, and thus not imported diff --git a/sdlvloed.py b/sdlvloed.py index 0bffdd3..32b795a 100755 --- a/sdlvloed.py +++ b/sdlvloed.py @@ -183,140 +183,6 @@ class Canvas(object): """Clean up any sockets we created""" self.broadcastsocket.close() -class PixelVloedClient(object): - """Sets up a client - - Arguments: - firstserver: (bool) False, select the first server immediately - debug: (bool) False - ip: (str) None - port: (int) None - width: (int) 640 - height: (int) 480 - - Listens for servers if no ip is given - """ - - def __init__(self, firstserver=False, debug=False, - ip=None, port=None, - width=640, height=480): - self.sleep = 0.01 - self.debug = debug - if not ip: - servers = False - while servers == False: - servers = self.DiscoverServers(firstserver) - self.ipaddress = servers[0]['ip'] - self.port = servers[0]['port'] - self.width = servers[0]['width'] - self.height = servers[0]['height'] - - if self.debug: - print ('displaying on %(ip)s:%(port)d, %(width)d*%(height)dpx' % - servers[0]) - else: - self.ipaddress = ip - self.port = port if port else UDP_PORT - self.width = width - self.height = height - if self.debug: - print ('displaying on %(ip)s:%(port)d, %(width)d*%(height)dpx' % - self) - self.sock = socket.socket(socket.AF_INET, # Internet - socket.SOCK_DGRAM) # UDP - - def Sleep(self, duration=None): - """Sleeps the designated amount of time""" - time.sleep(duration if duration else self.sleep) - - def SendPacket(self, message, sleep=0.01): - """Sends the message to the udp server - - Arguments: - message: (str, 140) - sleep: (float) 0.01, duration of time the client should sleep - """ - self.sock.sendto(message, (self.ipaddress, self.port)) - if sleep: - self.Sleep(duration=sleep) - - @staticmethod - def DiscoverServers(returnfirst=False, timeout=5): - """Discover servers that send out the pixelvloed preample""" - discoverysock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - discoverysock.bind(('', DISCOVER_PORT)) - starttime = time.time() - servers = [] - foundhash = {} - while (time.time() - timeout) < starttime: - data, _addr = discoverysock.recvfrom(1024) - try: - if data.startswith(PROTOCOL_PREAMBLE): - dataset = data.split(' ') - if float(dataset[0].split(':')[1]) <= MAX_PROTOCOL_VERSION: - ipaddress = dataset[1].split(':')[0] - port = int(dataset[1].split(':')[1]) - width = int(dataset[2].split('*')[0]) - height = int(dataset[2].split('*')[1]) - if data not in foundhash: - newserver = {'ip': ipaddress, - 'port': port, - 'width': width, - 'height': height} - foundhash[data] = True - print 'New pixelvloed screen found: %r' % newserver - servers.append(newserver) - if returnfirst: - return servers - except: - pass - if servers: - return servers - return False - -def NewMessage(): - """Creates a new message with the correct max size, rgb mode and version""" - message = MaxSizeList(MAX_PIXELS+2) - message.append(SetRGBAMode(False)) - message.append(SetVersionBit()) - return message - -def RGBPixel(x, y, r, g, b, a=None): # pylint: disable=C0103 - """Generates the packed data for a pixel""" - if a is not None: - return struct.pack("<2H4B", x, y, r, g, b, a) - return struct.pack("<2H3B", x, y, r, g, b) - -def SetRGBAMode(mode): - """Generate the rgb/rgba bit""" - return struct.pack("