[Twisted-Python] Controlling the classes being Jellied

Brian Warner warner at lothar.com
Wed Jul 2 04:59:03 EDT 2003


"Patrik Blommaskog" <pb_twisted at olga.mine.nu> writes:

> I'm using PB and want to accomplish something like this:
> 
> On the server, I have the following class hierarchy:
> 
>     Actor(pb.Copyable)
>   
>     GoodActor(Actor)
>     BadActor(Actor)
>     ...
>     WhateverActor(Actor)
> 
> There exists a number of instances of each of these classes.
> 
> When Jellying them over to the client, I want each of them to create 
> instances of the client class RemoteActor, no matter which of the server 
> classes they are of. In practice, the different server classes will 
> generate different state data that is transmitted to the client, where 
> that state data just updates __dict__ , at least for now.

You probably want something like this on the sending side:
  
  class Actor(pb.Copyable):
    def getTypeToCopy(self):
      return "package.module.Actor"
  
  class GoodActor(Actor):
    def getStateToCopy(self):
      return good_dict
  
  class BadActor(Actor):
    def getStateToCopy(self):
      return bad_dict

and then the receiving side can just do:
  
  class RemoteActor(pb.RemoteCopy):
    ...
  
  pb.setUnjellyableForClass("package.module.Actor", RemoteActor)


The two important points are:

 1: by overriding .getTypeToCopy(), the sending class can control what class
    name is put into the serialized stream.

 2: pb.setUnjellyableForClass maps a name (the one in the serialized stream)
    to a local class. The name can be specified either as a string or as a
    class. If you don't have access to the the class, just use a string that
    matches the one used by the sender. The only caveat is to make sure the
    string is unique with respect to packages and modules and such.. in
    general is should be a "fully qualified" class name.


Hope that helps.. let me know if you've got more questions. I think we could
use an example of this (string-based jellied class names instead of classes
shared between sender and recipient) in the docs.

cheers,
 -Brian




More information about the Twisted-Python mailing list