From f1f12489514f47f9154f8e4b6232399e747cfcd8 Mon Sep 17 00:00:00 2001 From: makefu Date: Sun, 24 Jul 2011 04:13:44 +0200 Subject: emergency commit --- cholerab/cholerab-live/chol_net.py | 81 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 cholerab/cholerab-live/chol_net.py (limited to 'cholerab/cholerab-live/chol_net.py') diff --git a/cholerab/cholerab-live/chol_net.py b/cholerab/cholerab-live/chol_net.py new file mode 100644 index 00000000..b5820973 --- /dev/null +++ b/cholerab/cholerab-live/chol_net.py @@ -0,0 +1,81 @@ +from socket import socket, AF_INET,SOCK_DGRAM,IPPROTO_UDP,SOL_SOCKET,SO_REUSEADDR,IP_MULTICAST_TTL,IP_MULTICAST_LOOP,INADDR_ANY,inet_aton,IP_ADD_MEMBERSHIP,IPPROTO_IP +import struct +import threading +import logging +log = None +from select import select +GROUP = '224.110.42.23' +PORT = 42023 +log = logging.getLogger('CholerabNet') +class CholerabMulicastNet(threading.Thread): + def __init__(self,cholerab,group=GROUP,port=PORT): + threading.Thread.__init__(self) + self.cholerab=cholerab + self.group=group + self.port=port + self.initSocket() + def send_char(self,x,y,char): + """ translates given params into network message """ + self.send_mc("%s %d %d" %(str(char),x,y)) + def send_mc(self,arg): + """ Sends message via multicast""" + + try: + log.debug("Sending '%s' to %s:%d" % (arg,self.group,self.port)) + self.s.sendto("%s" % arg,0,(self.group,self.port)) + except Exception ,e: + log.error("IN send_mc:%s"%str(e)) + + def initSocket (self,rcv=1): + ''' Initializes a Multicast socket ''' + host = '' + log.debug("Setting up Multicast Socket") + self.s = socket(AF_INET,SOCK_DGRAM, IPPROTO_UDP) + self.s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) + self.s.setsockopt(IPPROTO_IP, IP_MULTICAST_TTL, 32) + self.s.setsockopt(IPPROTO_IP,IP_MULTICAST_LOOP,1) # we do not want our own packets to be replayed + if rcv==1: + log.debug("Configuring for Read AND Write") + self.s.bind((host,PORT)) + mreq = struct.pack("4sl", inet_aton(GROUP), INADDR_ANY) + self.s.setsockopt(IPPROTO_IP,IP_ADD_MEMBERSHIP,mreq) + def run(self): + self.running = 1 + while self.running: + # break if we do not want to loop on + ready,output,exception = select([self.s],[],[],1) # try every second + for r in ready: + if r == self.s: + (data,addr) = self.s.recvfrom(1024) + log.debug("Received Data from %s, data %s"%(str(addr),str(data))) + self.receive_net(addr,data) + + def send_stupid(self,addr): + """ sends YOU ARE MADE OF STUPID to the right host """ + #TODO implement me + pass + + def receive_net(self,addr,data): + """ resolves which nick sent the message + TODO handle user resolution in mulicast """ + def decode(): + pass #TODO implement me + + #FIXME TODO using port as host identification is probably just plain wrong, fix me for real net! + try: + address,port = addr + user = port + arr = str(data).split() + char = arr[0] + x = arr[1] + y = arr[2] + self.cholerab.write_char(int(x),int(y),char,user) + except Exception, e: + log.error("Triggered YOU ARE MADE OF STUPID: %s" % str(e)) + self.send_stupid(addr) + + def stop(self): + ''' + stops the whole treading stuff gracefully + ''' + self.running=0 -- cgit v1.2.3 From 2862f1ad4ef0439721779a1a93e29bc5dc1c84de Mon Sep 17 00:00:00 2001 From: makefu Date: Sun, 24 Jul 2011 04:56:05 +0200 Subject: added robustness, removed utf8 support to be able to send special characters over the line the characters are translated into their number value and written as string. this essentially breaks utf-8 support for the clients, needs to be fixed some time later added ignore for own messages, message colorization --- cholerab/cholerab-live/chol_net.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'cholerab/cholerab-live/chol_net.py') diff --git a/cholerab/cholerab-live/chol_net.py b/cholerab/cholerab-live/chol_net.py index b5820973..ee0f5378 100644 --- a/cholerab/cholerab-live/chol_net.py +++ b/cholerab/cholerab-live/chol_net.py @@ -16,14 +16,15 @@ class CholerabMulicastNet(threading.Thread): self.initSocket() def send_char(self,x,y,char): """ translates given params into network message """ - self.send_mc("%s %d %d" %(str(char),x,y)) + self.send_mc("%s %d %d" %(str(ord(char)),x,y)) def send_mc(self,arg): """ Sends message via multicast""" - try: log.debug("Sending '%s' to %s:%d" % (arg,self.group,self.port)) + self.ignore_next += 1# we need this to work together correctly with reused sockets self.s.sendto("%s" % arg,0,(self.group,self.port)) except Exception ,e: + self.ignore_next -=1 log.error("IN send_mc:%s"%str(e)) def initSocket (self,rcv=1): @@ -41,14 +42,19 @@ class CholerabMulicastNet(threading.Thread): self.s.setsockopt(IPPROTO_IP,IP_ADD_MEMBERSHIP,mreq) def run(self): self.running = 1 + self.ignore_next = 0 while self.running: # break if we do not want to loop on ready,output,exception = select([self.s],[],[],1) # try every second for r in ready: if r == self.s: + log.debug(str(self.ignore_next)) (data,addr) = self.s.recvfrom(1024) - log.debug("Received Data from %s, data %s"%(str(addr),str(data))) - self.receive_net(addr,data) + if not self.ignore_next: + log.debug("Received Data from %s, data %s"%(str(addr),str(data))) + self.receive_net(addr,data) + else: + self.ignore_next -= 1 def send_stupid(self,addr): """ sends YOU ARE MADE OF STUPID to the right host """ @@ -58,18 +64,13 @@ class CholerabMulicastNet(threading.Thread): def receive_net(self,addr,data): """ resolves which nick sent the message TODO handle user resolution in mulicast """ - def decode(): - pass #TODO implement me - - #FIXME TODO using port as host identification is probably just plain wrong, fix me for real net! try: address,port = addr - user = port arr = str(data).split() char = arr[0] x = arr[1] y = arr[2] - self.cholerab.write_char(int(x),int(y),char,user) + self.cholerab.write_char(int(x),int(y),chr(int(char))) except Exception, e: log.error("Triggered YOU ARE MADE OF STUPID: %s" % str(e)) self.send_stupid(addr) -- cgit v1.2.3