import net, db class Monitor(db.DBObject): """ This is a demo 'monitor' class. """ # feedback is a string that this program can read and write. # (Note that the privledges are those that the client thinks it # has. The server may disagree. Further, the great and powerful server # can read and write whatsoever it chooses, for any nonprivate # database object.) feedback = db.dstr(db.rw) # chattext is a string that this program can only read, # but when it is updated by the server, a method should be run. chattext = db.dstr(db.r, db.update) # name is also read-only, but we don't care when it is updated. name = db.dstr(db.r) # This function will be called when new objects of this type are # created, since DBObject squats on __init__. # This won't be called when a new object of this type is magicked # out of the aether by the DB, only for ones created here. # Note that none actually are, but it seems like it would incomplete # without it. def init(self, name): self.name = name # this function will be called to "regenerate" the class. Whenever # it is created by the server, this will be called to generate its # local state/variables. You can also use this to not transmit # redundant data in some cases. # Note that there is something wrong with this method so you will # think about it before you make use of this facility. # HINT: what will happen when the server later changes name? def reload(self): self.bracketed_name = "<%s>"%self.name # This is an ordinary method of this class. It happens to interact # with a database property though. def chat(self, text): self.feedback = text # This method, because of its name and the db.update flag in the # declariation of chattext, will be called when the server changes # its value. def db_chattext(self): print self.name, "we get signal:",self.chattext self.chat(self.bracketed_name+" Main screen turn on!") from sys import stdin, argv from time import sleep # So, we're using the network module... network = net.Net() # and the database. We let it know what classes it is permitted to create # on behalf of the server. There can be several, obviously. database = db.DB(Monitor) # DB has its own handler. If you want to make your own handler class, # you need to make it inherit from db.DBHandler. (See dbtserver.py) network.attach_handler(database.handler()) # Connect to localhost port 11115... duh. network.connect('',11115) while True: # This gives time to the network module to do its processing. # Without this, the DB will never get messages and its messages # will not be sent. network.process() # We could do other stuff here but we won't; everything in this example # is database-driven. # don't take up the entire cpu capacity cycling this main loop. sleep(0.01)