import net, db class Monitor(db.DBObject): """ This is a demo 'monitor' class. """ # feedback is a string that the client can read and write. # Being the great and powerful server, we can also read and write them. # the word below is law: if a presumptuous client tries to change a # value we say it may only read, nothing happens. # db.update means we want to run a method (named db_feedback) when the # client changes this value. feedback = db.dstr(db.rw, db.update) # this is some private data item that we want to be preserved. # note that since we don't send it a privledge flag like db.rw, # the default is "private". You can say db.private if you're that # kind of person. pdata = db.dint() # We're going to let the client only read this variable. chattext = db.dstr(db.r) # Same deal here. name = db.dstr(db.r) # This function will be called when new objects of this type are # created, since DBObject squats on __init__. def init(self, name): self.name = name self.pdata = 329 + # 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. def reload(self): # This is just an example self.temp_state = hash(self.name) # 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_feedback(self): print "Feedback from", self.name, ":",self.feedback from sys import stdin, argv from time import sleep from random import randint, choice # So, we're using the network module... and it will be a server on # port 11115 network = net.Net(11115) # and the database. Since it is the server, it will not create any # clases (without being told to do so locally - in other words, it won't # create ten million class instances because a malicious client said to do # that.) Seing the empty class list, it will also know it is the server. database = db.DB() # At any time after this point, we could create some database zones... # but we won't in this example. # DB has its own handler. Since this is the server, as a practical matter # it has to have its own handler as well, to facilitate database connections. class SimpleHandler(db.DBHandler): def new_client(self, name): print "**", name, "joined" # In our case, we're going to add all these clients. # The db attribute is coming from DBHandler's init. # it is being added to the default global zone since we # don't specify a zone. client = self.db.add_client(name) # So, let's create an object on the client. # The parameter's we pass go to Monitor's init. # the second parameter of add is the client to whom this # belongs. (That is, the one to whom the permissions apply.) # We can say None here to mean that it belongs to everyone. self.db.add(Monitor("chat"+name), client) def dropped_client(self, name): print "!!", name, "dropped" self.db.del_client(name) # Attach our handler to the db handler and that to the network. network.attach_handler(SimpleHandler(database)) 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() # so, once in a while, do something. if not randint(0,1000): # Select all objects from the database. # We have some choices here: # objects(): select ALL objects from ALL ZONES. # objects('default'): select ALL objects from the zone 'default' # objects('default', Monitor) : select all the objects from default # with the class Monitor. # objects(['z1', 'z2'], [Monitor, Foo]) : select all the objects # from zones z1 and z2 who are monitor or foo instances. monobjs = database.objects() choice(monobjs).chattext = "Message"+str(randint(1,500)) # don't take up the entire cpu capacity cycling this main loop. sleep(0.01)