diff options
author | Florian Jung <flo@windfisch.org> | 2015-01-05 00:43:29 +0100 |
---|---|---|
committer | Florian Jung <flo@windfisch.org> | 2015-01-05 00:43:29 +0100 |
commit | 98bdee0b582ddc7010065ed7b378677017683ce7 (patch) | |
tree | f21ca1e0a20432c34b269e1bf5bfcee29de52ee9 | |
parent | 4bf9dfc71de9c331e4659ea9f7accc804874685f (diff) |
multi-threaded server
-rw-r--r-- | server.py | 75 |
1 files changed, 45 insertions, 30 deletions
@@ -2,6 +2,8 @@ import cv2 import os import socket import sys +import threading +import time def encode_int(i): i = int(i) @@ -9,6 +11,38 @@ def encode_int(i): chr( (i/(2**8))%256) + chr(i%256) +class ServerThread(threading.Thread): + def run(self): + while True: + # Wait for a connection + print >>sys.stderr, 'waiting for a connection' + connection, client_address = sock.accept() + try: + print >>sys.stderr, 'connection from', client_address + + # Receive the data in small chunks and retransmit it + while True: + data = connection.recv(16) + if data: + if data=="get\n": + lock.acquire() + framestr = frame.tostring() + lenframestr=len(framestr) + connection.sendall(encode_int(lenframestr)+framestr); + lock.release() + else: + print >>sys.stderr, 'no more data from', client_address + break + except: + print "Dingens!!11!1!!!" + finally: + # Clean up the connection + connection.close() + + + + + server_address = '/home/flo/uds_socket' try: os.unlink(server_address) @@ -26,37 +60,18 @@ sock.bind(server_address) # Listen for incoming connections sock.listen(1) -while True: - # Wait for a connection - print >>sys.stderr, 'waiting for a connection' - connection, client_address = sock.accept() - try: - print >>sys.stderr, 'connection from', client_address - cap = cv2.VideoCapture("/home/flo/kruschkram/out2.avi") - - # Receive the data in small chunks and retransmit it - while True: - data = connection.recv(16) - if data: - if data=="get\n": - status, frame = cap.read() - framestr = frame.tostring() - lenframestr=len(framestr) - connection.sendall(encode_int(lenframestr)+framestr); - #cv2.imshow("img",frame) - #cv2.waitKey(1) - else: - print >>sys.stderr, 'no more data from', client_address - break - except: - print "Dingens!!11!1!!!" - - finally: - # Clean up the connection - connection.close() +serverthread=ServerThread() +lock=threading.Lock() + +cap = cv2.VideoCapture("/home/flo/kruschkram/out2.avi") +status, frame = cap.read() + +serverthread.start() while True: + print "hello world :)" + time.sleep(1) + lock.acquire() status, frame = cap.read() - cv2.imshow("img",frame) - cv2.waitKey(20) + lock.release() |