From: Mark Tearle Date: Sun, 27 Jun 2004 10:09:37 +0000 (+0000) Subject: Virtual vend X-Git-Tag: IMPORT-FROM-UCCCVS~146 X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=b46a16fb5eb1bf46046bc590ac31887f83ea5af8;p=uccvend-vendserver.git Virtual vend --- diff --git a/virtualvend/vvend.glade b/virtualvend/vvend.glade new file mode 100644 index 0000000..cac8393 --- /dev/null +++ b/virtualvend/vvend.glade @@ -0,0 +1,583 @@ + + + + + + + True + Virtual Vend + GTK_WINDOW_TOPLEVEL + GTK_WIN_POS_NONE + False + True + False + True + False + False + GDK_WINDOW_TYPE_HINT_UTILITY + GDK_GRAVITY_NORTH_WEST + + + + + + True + False + 0 + + + + True + label1 + False + False + GTK_JUSTIFY_LEFT + False + False + 0.5 + 0.5 + 0 + 0 + + + 0 + True + True + + + + + + True + 3 + 4 + False + 0 + 0 + + + + True + True + 1 + True + GTK_RELIEF_NORMAL + True + + + + + 0 + 1 + 0 + 1 + fill + + + + + + + True + True + 3 + True + GTK_RELIEF_NORMAL + True + + + + + 2 + 3 + 0 + 1 + fill + + + + + + + True + True + 4 + True + GTK_RELIEF_NORMAL + True + + + + + 3 + 4 + 0 + 1 + fill + + + + + + + True + True + 5 + True + GTK_RELIEF_NORMAL + True + + + + + 0 + 1 + 1 + 2 + fill + + + + + + + True + True + 6 + True + GTK_RELIEF_NORMAL + True + + + + + 1 + 2 + 1 + 2 + fill + + + + + + + True + True + 7 + True + GTK_RELIEF_NORMAL + True + + + + + 2 + 3 + 1 + 2 + fill + + + + + + + True + True + 8 + True + GTK_RELIEF_NORMAL + True + + + + + 3 + 4 + 1 + 2 + fill + + + + + + + True + True + RESET + True + GTK_RELIEF_NORMAL + True + + + + + + 2 + 4 + 2 + 3 + + + + + + + True + True + 0 + True + GTK_RELIEF_NORMAL + True + + + + + 1 + 2 + 2 + 3 + + + + + + + True + True + 9 + True + GTK_RELIEF_NORMAL + True + + + + + 0 + 1 + 2 + 3 + + + + + + + True + True + 2 + True + GTK_RELIEF_NORMAL + True + + + + + 1 + 2 + 0 + 1 + fill + + + + + + 0 + True + True + + + + + + True + False + 0 + + + + True + True + 0.05 + True + GTK_RELIEF_NORMAL + True + + + + 0 + True + True + + + + + + True + True + 0.10 + True + GTK_RELIEF_NORMAL + True + + + + 0 + True + True + + + + + + True + True + 0.25 + True + GTK_RELIEF_NORMAL + True + + + + 0 + True + True + + + + + + True + True + $1 + True + GTK_RELIEF_NORMAL + True + + + + 0 + True + True + + + + + + True + True + MODE + True + GTK_RELIEF_NORMAL + True + + + + 0 + True + True + + + + + 0 + True + True + + + + + + True + False + 0 + + + + True + True + DIP1 + True + GTK_RELIEF_NORMAL + True + False + False + + + 0 + True + True + + + + + + True + True + DIP2 + True + GTK_RELIEF_NORMAL + True + False + False + + + 0 + True + True + + + + + + True + True + DIP3 + True + GTK_RELIEF_NORMAL + True + False + False + + + 0 + True + True + + + + + + True + True + DIP4 + True + GTK_RELIEF_NORMAL + True + False + False + + + 0 + True + True + + + + + + True + True + DIP5 + True + GTK_RELIEF_NORMAL + True + False + False + + + 0 + True + True + + + + + + True + True + DIP6 + True + GTK_RELIEF_NORMAL + True + False + False + + + 0 + True + True + + + + + + True + True + DIP7 + True + GTK_RELIEF_NORMAL + True + False + False + + + 0 + True + True + + + + + + True + True + DIP8 + True + GTK_RELIEF_NORMAL + True + False + False + + + 0 + True + True + + + + + 0 + True + True + + + + + + True + True + DOOR + True + GTK_RELIEF_NORMAL + True + True + False + + + + 0 + False + False + + + + + + True + True + + + 0 + False + False + + + + + + + diff --git a/virtualvend/vvend.py b/virtualvend/vvend.py new file mode 100644 index 0000000..ac9de42 --- /dev/null +++ b/virtualvend/vvend.py @@ -0,0 +1,193 @@ +#!/usr/bin/env python + +import sys +import socket + +try: + import pygtk + #tell pyGTK, if possible, that we want GTKv2 + pygtk.require("2.0") +except: + #Some distributions come with GTK2, but not pyGTK + pass + +try: + import gtk + import gtk.glade +except: + print "You need to install pyGTK or GTKv2 ", + print "or set your PYTHONPATH correctly." + print "try: export PYTHONPATH=", + print "/usr/local/lib/python2.2/site-packages/" + sys.exit(1) + +#now we have both gtk and gtk.glade imported +#Also, we know we are running GTK v2 + +class appgui: + def __init__(self): + """ + In this init we are going to display the main + serverinfo window + """ + gladefile="vvend.glade" + windowname="vvend" + self.wTree=gtk.glade.XML (gladefile,windowname) + # we only have two callbacks to register, but + # you could register any number, or use a + # special class that automatically + # registers all callbacks. If you wanted to pass + # an argument, you would use a tuple like this: + # dic = { "on button1_clicked" : + # (self.button1_clicked, arg1,arg2) , ... + + dic = { + "on_button1_clicked" : self.keypad_clicked, + "on_button2_clicked" : self.keypad_clicked, + "on_button3_clicked" : self.keypad_clicked, + "on_button4_clicked" : self.keypad_clicked, + "on_button5_clicked" : self.keypad_clicked, + "on_button6_clicked" : self.keypad_clicked, + "on_button7_clicked" : self.keypad_clicked, + "on_button8_clicked" : self.keypad_clicked, + "on_button9_clicked" : self.keypad_clicked, + "on_button10_clicked" : self.keypad_clicked, + "on_button11_clicked" : self.keypad_clicked, + "on_vvend_destroy_event" : self.quit, + "on_vvend_delete_event" : self.quit } + self.wTree.signal_autoconnect (dic) + display = self.wTree.get_widget("label1") + display.set_text("*5N4CK0RZ*") + + self.messageid = None + + #s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + #s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) +# +# listenhost="" +# listenport=5150 +# s.bind((listenhost, listenport)) +# # only one connection +# s.listen(1) +# s.setblocking(1) +# #GDK->gtk.gdk in GTK V 2.0 +# id=gtk.input_add(s, gtk.gdk.INPUT_READ, self.handleNewConnection) + + self.server() + + + return + +#####CALLBACKS + def keypad_clicked(self,widget): + print "button clicked" + print widget.get_label() + self.do_send(widget.get_label()) + sys.stdout.write("\a") + + def handleNewConnection(self,source,condition): + #source is a socket in GTK v 1 and a fd in version 2 + conn, addr = source.accept() + sys.stdout.write(conn.recv(1)) + conn.send("bing\n") + return gtk.TRUE + +# from http://www.pythonbrasil.com.br/moin.cgi/MonitorandoSocketsComPygtk + + + def send(self, data=None, widget=None): + text = self.entry.get_text() + self.do_send(text) + self.entry.set_text('') + + # + + def do_send(self, data): + + # envia ''data'' para todos os clientes conectados + + for addr, (conn, tag) in self.clients.iteritems(): + conn.send(data) + + def server(self): + + # inicializa o servidor + + self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + self.sock.bind(('localhost', 5051)) + self.sock.listen(0) + + # + # + + self.server_tag = gtk.input_add(self.sock, gtk.gdk.INPUT_READ, self.accept) + + # mantemos uma lista dos clientes conectados + + self.clients = {} + + def accept(self, source, condition): + + # + # esperando para ser aceito + + conn, addr = source.accept() + self.insert("%s:%s conectado\n" % addr) + + # insere o cliente na lista e registra o método self.write como + # callback para quando existirem dados esperando para serem lidos + + self.clients[addr] = (conn, gtk.input_add(conn, gtk.gdk.INPUT_READ, self.write)) + + def write(self, source, condition): + + # método chamado quando um cliente envia dados + + data = source.recv(1024) + if data.strip() == 'bye' or not len(data): + + # se o cliente enviar um ''bye'', desconecte-o :) + + source.close() + for addr, (conn, tag) in self.clients.iteritems(): + if source is conn: + gtk.input_remove(tag) + self.insert('%s:%s desconectado\n' % addr) + del self.clients[addr] + + self.server_tag = gtk.input_add(self.sock, gtk.gdk.INPUT_READ, self.accept) + break + elif data.strip() == 'quit': + self.quit() + else: + for (addr, port), (conn, tag) in self.clients.iteritems(): + if source is conn: + self.insert('%s:%s >>> %s\n'%(addr, port, data.strip())) + break + + return gtk.TRUE + + def insert(self, data): + statusbar = self.wTree.get_widget("statusbar1") + if self.messageid: + statusbar.remove(1, self.messageid) + self.messageid=statusbar.push(1,data) + + def quit(self, *args): + sys.stdout.write("quiting...\n") + gtk.input_remove(self.server_tag) + for addr, (conn, tag) in self.clients.iteritems(): + gtk.input_remove(tag) + conn.close() + self.sock.close() + + gtk.mainquit() + sys.stdout.write("quit!\n") + +# we start the app like this... +app=appgui() +gtk.mainloop() + + +# notes +# http://www.async.com.br/faq/pygtk/index.py?req=show&file=faq20.011.htp