# in order to use the database, we import the net and db modules which are # part of ostendi. import net, db # This is a simple object with one database property, 'status', # which we set up to be an int. You don't have to declare the type if you # don't want to (say db.ddtyp for database dynamic typed.) class Trivial(db.DBObject): status = db.dint(db.r) # the flag db.r is the mode: we can read this variable but not write it. # (The server can both read and write the variable, so it has db.rw.) # Note that permissions are enforced on the server, so we're really just # writing it here for completeness. # Create a new instance of the network class. Since we don't pass a port # number, it is created in client mode. n = net.Net() # the argument is the list of classes it may instantiate. # it goes by name of course; the classes do not have to be the same in any way # other than their shared db attributes. You can specify more than one class # here, e.g. db.DB(Player,Item,Level). If no items are provided, the DB will be # created in server mode. (Since the server does not create objects in response # to client requests, it is not nessicary to have a manifest of acceptable # objects.) d = db.DB(Trivial) # This command attaches the network object we have created to the database. d.bind(n) # Now, we connect to a server. The parameters are IP,Port. n.connect('',12349) # join the zone 'default' of the connected DB. To join a zone means that the # client will be sent the data of all the objects in that zone, and will be # updated thereafter on changes to them. z = d.join('default') from time import sleep while True: # We have to give the network class some time to process any new messages. # The database bound to the network automatically processes db-related # messages. f = n.process() # Between joining a zone and having received all its contents, the zone is # in the "not ready" state and zone.is_ready() returns False. When joining # a zone, a client should ordinarily wait until the zone is ready before # doing anything with it, as it may contain incompletely formed objects. print "------ Zone <%s> : %s ------"%( z.name, "Ready" if z.is_ready() else "Not Ready" ) # z.items() unsurprisingly returns a list of all database objects in z # with their names, like the corisponding method of a dictionary. for name,obj in z.items(): print "Status of %s: <%s>"%(name,obj.status) # don't use up all the CPU time polling for messages if not f: sleep(2)