From b2bdf4e508bd6c58445de4ad302c656a8cf2e983 Mon Sep 17 00:00:00 2001 From: makefu Date: Tue, 19 Nov 2013 08:38:38 +0100 Subject: cholerab-live -> graveyard --- .graveyard/cholerab-live/README | 6 ++ .graveyard/cholerab-live/chol_net.py | 82 ++++++++++++++++++++ .graveyard/cholerab-live/chol_net.pyc | Bin 0 -> 4361 bytes .graveyard/cholerab-live/cholerab.py | 36 +++++++++ .graveyard/cholerab-live/live.sh | 88 ++++++++++++++++++++++ .graveyard/cholerab-live/makefu/README | 6 ++ .graveyard/cholerab-live/makefu/chol_net.py | 82 ++++++++++++++++++++ .graveyard/cholerab-live/makefu/cholerab.py | 36 +++++++++ .graveyard/cholerab-live/makefu/view.py | 112 ++++++++++++++++++++++++++++ .graveyard/cholerab-live/ttycnser.sh | 27 +++++++ .graveyard/cholerab-live/view.py | 112 ++++++++++++++++++++++++++++ 11 files changed, 587 insertions(+) create mode 100644 .graveyard/cholerab-live/README create mode 100644 .graveyard/cholerab-live/chol_net.py create mode 100644 .graveyard/cholerab-live/chol_net.pyc create mode 100755 .graveyard/cholerab-live/cholerab.py create mode 100755 .graveyard/cholerab-live/live.sh create mode 100644 .graveyard/cholerab-live/makefu/README create mode 100644 .graveyard/cholerab-live/makefu/chol_net.py create mode 100755 .graveyard/cholerab-live/makefu/cholerab.py create mode 100644 .graveyard/cholerab-live/makefu/view.py create mode 100755 .graveyard/cholerab-live/ttycnser.sh create mode 100644 .graveyard/cholerab-live/view.py (limited to '.graveyard/cholerab-live') diff --git a/.graveyard/cholerab-live/README b/.graveyard/cholerab-live/README new file mode 100644 index 00000000..b778b98c --- /dev/null +++ b/.graveyard/cholerab-live/README @@ -0,0 +1,6 @@ + +view.py: contains view classes for cholerab +cholerab.py: main file + +start with : +python cholerab.py diff --git a/.graveyard/cholerab-live/chol_net.py b/.graveyard/cholerab-live/chol_net.py new file mode 100644 index 00000000..ee0f5378 --- /dev/null +++ b/.graveyard/cholerab-live/chol_net.py @@ -0,0 +1,82 @@ +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(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): + ''' 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 + 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) + 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 """ + #TODO implement me + pass + + def receive_net(self,addr,data): + """ resolves which nick sent the message + TODO handle user resolution in mulicast """ + try: + address,port = addr + arr = str(data).split() + char = arr[0] + x = arr[1] + y = arr[2] + 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) + + def stop(self): + ''' + stops the whole treading stuff gracefully + ''' + self.running=0 diff --git a/.graveyard/cholerab-live/chol_net.pyc b/.graveyard/cholerab-live/chol_net.pyc new file mode 100644 index 00000000..0694ad6d Binary files /dev/null and b/.graveyard/cholerab-live/chol_net.pyc differ diff --git a/.graveyard/cholerab-live/cholerab.py b/.graveyard/cholerab-live/cholerab.py new file mode 100755 index 00000000..eb9e66df --- /dev/null +++ b/.graveyard/cholerab-live/cholerab.py @@ -0,0 +1,36 @@ +#!/usr/bin/python2 +# -*- coding: utf-8 -*- +import curses,time +from view import CursesView +from chol_net import CholerabMulicastNet +import logging +logging.basicConfig(filename='here.log',level=logging.DEBUG) +log = logging.getLogger('main') +class Cholerab: + def __init__(self): + self.view = CursesView(cholerab=self) + self.transport = CholerabMulicastNet(cholerab=self) + def send_char(self,x,y,char): + log.info("Sending %s at (%d,%d) to connected peers" %(char,x,y)) + self.transport.send_char(x,y,char) + + def write_char(self,x,y,char): + log.info("Writing %s at (%d,%d) to view" %(char,x,y)) + self.view.write_char(x,y,char,user=2) + def stop(self): + self.view.stop() + self.transport.stop() + def main(self): + self.view.start() + self.transport.start() + self.view.join() + #after view dies, kill the transport as well + self.transport.stop() + self.transport.join() +def main(): + log.debug('started main') + chol = Cholerab() + chol.main() + +if __name__ == "__main__": + main() diff --git a/.graveyard/cholerab-live/live.sh b/.graveyard/cholerab-live/live.sh new file mode 100755 index 00000000..62a2c3cf --- /dev/null +++ b/.graveyard/cholerab-live/live.sh @@ -0,0 +1,88 @@ +#! /bin/sh +set -euf +stty cbreak -echo + +go() { + state=$1 + wr 7 + wr " " >&2 + wr "state=$state" >&2 + wr 8 + $1 +} + +rd() { + dd bs=1 count=1 2>/dev/null +} + +bufrd() { + buf="`rd`" + bufinfowr +} + +bufrda() { + buf="$buf`rd`" + bufinfowr +} + +bufinfowr() { + wr 7 + wr " " >&2 + wr " " >&2 + case "$buf" in + () wr '^[' >&2;; + (*) wr "$buf" >&2;; + esac + wr "`wr "$buf" | xxd -p`" >&2 + wr 8 +} + +wr() { + echo -n "$1" +} + +C0="`echo C0 | xxd -r -p`"; DF="`echo DF | xxd -r -p`" +E0="`echo E0 | xxd -r -p`"; EF="`echo EF | xxd -r -p`" +F0="`echo F0 | xxd -r -p`"; F7="`echo F7 | xxd -r -p`" +S() { + bufrd + case "$buf" in + () go ESC;; + () wr ' '; go S;; + ([$C0-$DF]) go U1;; + ([$E0-$EF]) go U2;; + ([$F0-$F7]) go U3;; + (*) wr "$buf"; go S;; + esac +} + +U1() { buf="$buf`rd`"; wr "$buf"; go S; } +U2() { buf="$buf`rd`"; go U1; } +U3() { buf="$buf`rd`"; go U2; } + + +ESC() { + bufrda + case "$buf" in + ('[') go ESC_OSQRB;; + (*) + wr '^[' + go S + ;; + esac +} + +ESC_OSQRB() { + bufrda + case "$buf" in + (''|''|''|'') wr "$buf"; go S;; + (*) + wr '^[[' + go S + ;; + esac +} + + +wr 'c' +go S diff --git a/.graveyard/cholerab-live/makefu/README b/.graveyard/cholerab-live/makefu/README new file mode 100644 index 00000000..b778b98c --- /dev/null +++ b/.graveyard/cholerab-live/makefu/README @@ -0,0 +1,6 @@ + +view.py: contains view classes for cholerab +cholerab.py: main file + +start with : +python cholerab.py diff --git a/.graveyard/cholerab-live/makefu/chol_net.py b/.graveyard/cholerab-live/makefu/chol_net.py new file mode 100644 index 00000000..ee0f5378 --- /dev/null +++ b/.graveyard/cholerab-live/makefu/chol_net.py @@ -0,0 +1,82 @@ +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(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): + ''' 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 + 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) + 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 """ + #TODO implement me + pass + + def receive_net(self,addr,data): + """ resolves which nick sent the message + TODO handle user resolution in mulicast """ + try: + address,port = addr + arr = str(data).split() + char = arr[0] + x = arr[1] + y = arr[2] + 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) + + def stop(self): + ''' + stops the whole treading stuff gracefully + ''' + self.running=0 diff --git a/.graveyard/cholerab-live/makefu/cholerab.py b/.graveyard/cholerab-live/makefu/cholerab.py new file mode 100755 index 00000000..eb9e66df --- /dev/null +++ b/.graveyard/cholerab-live/makefu/cholerab.py @@ -0,0 +1,36 @@ +#!/usr/bin/python2 +# -*- coding: utf-8 -*- +import curses,time +from view import CursesView +from chol_net import CholerabMulicastNet +import logging +logging.basicConfig(filename='here.log',level=logging.DEBUG) +log = logging.getLogger('main') +class Cholerab: + def __init__(self): + self.view = CursesView(cholerab=self) + self.transport = CholerabMulicastNet(cholerab=self) + def send_char(self,x,y,char): + log.info("Sending %s at (%d,%d) to connected peers" %(char,x,y)) + self.transport.send_char(x,y,char) + + def write_char(self,x,y,char): + log.info("Writing %s at (%d,%d) to view" %(char,x,y)) + self.view.write_char(x,y,char,user=2) + def stop(self): + self.view.stop() + self.transport.stop() + def main(self): + self.view.start() + self.transport.start() + self.view.join() + #after view dies, kill the transport as well + self.transport.stop() + self.transport.join() +def main(): + log.debug('started main') + chol = Cholerab() + chol.main() + +if __name__ == "__main__": + main() diff --git a/.graveyard/cholerab-live/makefu/view.py b/.graveyard/cholerab-live/makefu/view.py new file mode 100644 index 00000000..6a75f655 --- /dev/null +++ b/.graveyard/cholerab-live/makefu/view.py @@ -0,0 +1,112 @@ +#!/usr/bin/python2 + +from curses import * +import socket +import threading +import logging +log = logging.getLogger('cholerab-curses') + +class CursesView(threading.Thread): + def addch(self,char): + """ + adds a char at the current cursor position + abstraction to the curses win.addch() + """ + try: self.win.addch(char) + except: pass + self.cholerab.send_char(self.x,self.y,chr(char)) + def stop(self): + #TODO setting back the whole terminal currently does not work correctly, fix me harder + self.running = False + self.clear() + self.win.refresh() + nocbreak(); self.scr.keypad(0); echo() + #endwin() + + def run(self): + """ + input loop + + TODO add Unicode Input Support + """ + self.running = True + def try_move(x,y): + if x >= self.width: x = 0;y = y+1 + if x < 0 : x = self.width-1; y= y-1 + if y >= self.height : x = x+1;y = 0 + if y < 0 : x = x-1; y = self.height-1 + self.win.move(y,x); return x,y + + while self.running: + c = self.scr.getch() #get_char(self.scr) + #TODO UTF8 here, get_wch not yet implemented + log.debug("Pressed : %d" % c) + if c == KEY_LEFT : self.x -=1 + elif c == KEY_RIGHT : self.x +=1 + elif c == KEY_UP : self.y -=1 + elif c == KEY_DOWN : self.y +=1 + elif c == ord('q') : self.stop() + elif c == 127 or c == KEY_BACKSPACE: + log.info('backspace pressed') + self.x -=1; + self.x,self.y = try_move(self.x,self.y) + self.addch(ord(' ')) + elif c == ord('\n'): + log.info('enter pressed') + self.y +=1; + self.x,self.y = try_move(self.x,self.y) + else : + self.addch(c) + self.x +=1 + self.x,self.y = try_move(self.x,self.y) + self.refresh() + + def write_char(self,x,y,char,user=1): + user = user % 3 + 1 + self.win.addch(y,x,char,color_pair(user)) + self.win.move(self.y,self.x) + self.refresh() + def write_str(self,x,y,string,user=1): + self.win.addstr(y,x,string,color_pair(user)) + self.win.move(self.y,self.x) + self.refresh() + def refresh(self): + self.scr.refresh() + self.win.refresh() + def clear(self): + self.win.clear() + pass + def write_field(self,ar): + """ + writes the whole field with given 2-dimensional array + """ + self.clear() + pass + + def __init__(self,height=24,width=80,cholerab=None,scr=None): + # TODO handle sessions somehow + if scr: + self.scr = scr + else: + self.scr = initscr() + start_color() + init_pair(1,COLOR_WHITE,COLOR_BLACK) + init_pair(2,COLOR_RED,COLOR_BLACK) + init_pair(3,COLOR_GREEN,COLOR_BLACK) + init_pair(3,COLOR_CYAN,COLOR_BLACK) + threading.Thread.__init__(self) + self.cholerab = cholerab + + noecho() + cbreak() + self.scr.keypad(1) + try: curs_set(2) + except: pass # go home with your non-standard terminals! + + begin_x = 0;begin_y = 0 + self.height = height + self.width = width + self.x = 0 ; self.y = 0 + + self.win = newwin(height,width,begin_y,begin_x) + self.clear() diff --git a/.graveyard/cholerab-live/ttycnser.sh b/.graveyard/cholerab-live/ttycnser.sh new file mode 100755 index 00000000..0972dbbb --- /dev/null +++ b/.graveyard/cholerab-live/ttycnser.sh @@ -0,0 +1,27 @@ +#! /bin/sh +set -euf + +tty="${TMPDIR-/tmp}/ttycnser.$LOGNAME.tty" + +case "${mode-server}" in + (server) + host=0.0.0.0 + port=8080 + export mode=client + echo "ttycnser @ $host $port" >&2 + exec tcpserver $host $port "$0" + ;; + (client) + line="`read line && echo "$line"`" + echo -n '7>>>> '"$line"'8' > "$tty" + ;; + (install) + # TODO tell the user to do something like + # PROMPT_COMMAND="`mode=install ~/p/krebscode/painload/cholerab/ttycnser`" + echo "ln -snf '`tty`' '$tty'" + ;; + (*) + echo 'Error 1: You are made of stupid!' >&2 + exit 23 + ;; +esac diff --git a/.graveyard/cholerab-live/view.py b/.graveyard/cholerab-live/view.py new file mode 100644 index 00000000..6a75f655 --- /dev/null +++ b/.graveyard/cholerab-live/view.py @@ -0,0 +1,112 @@ +#!/usr/bin/python2 + +from curses import * +import socket +import threading +import logging +log = logging.getLogger('cholerab-curses') + +class CursesView(threading.Thread): + def addch(self,char): + """ + adds a char at the current cursor position + abstraction to the curses win.addch() + """ + try: self.win.addch(char) + except: pass + self.cholerab.send_char(self.x,self.y,chr(char)) + def stop(self): + #TODO setting back the whole terminal currently does not work correctly, fix me harder + self.running = False + self.clear() + self.win.refresh() + nocbreak(); self.scr.keypad(0); echo() + #endwin() + + def run(self): + """ + input loop + + TODO add Unicode Input Support + """ + self.running = True + def try_move(x,y): + if x >= self.width: x = 0;y = y+1 + if x < 0 : x = self.width-1; y= y-1 + if y >= self.height : x = x+1;y = 0 + if y < 0 : x = x-1; y = self.height-1 + self.win.move(y,x); return x,y + + while self.running: + c = self.scr.getch() #get_char(self.scr) + #TODO UTF8 here, get_wch not yet implemented + log.debug("Pressed : %d" % c) + if c == KEY_LEFT : self.x -=1 + elif c == KEY_RIGHT : self.x +=1 + elif c == KEY_UP : self.y -=1 + elif c == KEY_DOWN : self.y +=1 + elif c == ord('q') : self.stop() + elif c == 127 or c == KEY_BACKSPACE: + log.info('backspace pressed') + self.x -=1; + self.x,self.y = try_move(self.x,self.y) + self.addch(ord(' ')) + elif c == ord('\n'): + log.info('enter pressed') + self.y +=1; + self.x,self.y = try_move(self.x,self.y) + else : + self.addch(c) + self.x +=1 + self.x,self.y = try_move(self.x,self.y) + self.refresh() + + def write_char(self,x,y,char,user=1): + user = user % 3 + 1 + self.win.addch(y,x,char,color_pair(user)) + self.win.move(self.y,self.x) + self.refresh() + def write_str(self,x,y,string,user=1): + self.win.addstr(y,x,string,color_pair(user)) + self.win.move(self.y,self.x) + self.refresh() + def refresh(self): + self.scr.refresh() + self.win.refresh() + def clear(self): + self.win.clear() + pass + def write_field(self,ar): + """ + writes the whole field with given 2-dimensional array + """ + self.clear() + pass + + def __init__(self,height=24,width=80,cholerab=None,scr=None): + # TODO handle sessions somehow + if scr: + self.scr = scr + else: + self.scr = initscr() + start_color() + init_pair(1,COLOR_WHITE,COLOR_BLACK) + init_pair(2,COLOR_RED,COLOR_BLACK) + init_pair(3,COLOR_GREEN,COLOR_BLACK) + init_pair(3,COLOR_CYAN,COLOR_BLACK) + threading.Thread.__init__(self) + self.cholerab = cholerab + + noecho() + cbreak() + self.scr.keypad(1) + try: curs_set(2) + except: pass # go home with your non-standard terminals! + + begin_x = 0;begin_y = 0 + self.height = height + self.width = width + self.x = 0 ; self.y = 0 + + self.win = newwin(height,width,begin_y,begin_x) + self.clear() -- cgit v1.2.3