Simple media server using Atlas-Python

by Aloril

Media description file

First you need to write/generate media description objects.

In this demo though only few very basic Atlas objects included in media.bach because media format is not yet defined. Sample Atlas object:

{
        id: "elf",
        parents: ["humanoid"],
        specification: "atlas_game_rpg",
        interface: "game_interface",
        description: "Image of ordinary elf like you would expect
them, real media info object will include pointers to various
media info objects for each type of media available for this
class instead of direct link to image",
        image_url: "http://foo.game.org/elf.png"
}

For simple example media file see forge/libs/Atlas-Python/media.bach

For encoding of it see http://www.worldforge.org/website/protocols/tutorial

Server code

Server code consist of 3 parts: Actual server class, client class and main part.

Server class

Here we override only setup method: It loads media description objects from file and displays count of them.

class MediaServer(server.SocketServer):
    def setup(self):
        self.objects = files.read_file("media.bach")
        print len(self.objects), "objects loaded"

Client class

Instance of this class created for each connection. It responds only to "get" -operation. First we read id from operation arguments. Then we get requested object from object dictionary loaded into server. If object exist, send it as argument inside info operation. If it doesn't exist, send error operation including original operation. Python-Atlas handles cases where no argument is given for get operation or if no id attribute exists there (it catches exceptions and forwards traceback to client).

class MediaClient(server.TcpClient): def get_op(self, op): id = op.args[0].id obj = self.server.objects.get(id) if obj: self.reply_operation(op, atlas.Operation("info", obj)) else: self.send_error(op, "no object with id " + id)

Main part

Here we create server instance giving it MediaClient as argument to use when creating new connections and then go to processing loop:

    s = MediaServer("Simple media server", server.args2address(sys.argv),
MediaClient)
    s.loop()

For whole example code see forge/libs/Atlas-Python/media_server.py


Aloril
Last modified: Thu Jul 26 20:10:59 EEST 2001