Exemple #1
0
def register_socket_error():
    """
    Register socket.error to Pyro4's SerializerBase so we can send
    socket.errors across Pyro4 connections.
    """
    SerializerBase.register_dict_to_class("socket.error",
                                          socket_error_dict_to_class)
    SerializerBase.register_class_to_dict(socket.error,
                                          socket_error_class_to_dict)
Exemple #2
0
import os
import socket
import sys
from math import sqrt
from Pyro5.api import SerializerBase, Proxy
from workitem import Workitem


# For 'workitem.Workitem' we register a deserialization hook to be able to get these back from Pyro
SerializerBase.register_dict_to_class("workitem.Workitem", Workitem.from_dict)

WORKERNAME = "Worker_%d@%s" % (os.getpid(), socket.gethostname())


def factorize(n):
    """simple algorithm to find the prime factorials of the given number n"""

    def isPrime(n):
        return not any(x for x in range(2, int(sqrt(n)) + 1) if n % x == 0)

    primes = []
    candidates = range(2, n + 1)
    candidate = 2
    while not primes and candidate in candidates:
        if n % candidate == 0 and isPrime(candidate):
            primes = primes + [candidate] + factorize(n // candidate)
        candidate += 1
    return primes


def process(item):
Exemple #3
0
        "number-attribute": obj.number
    }


def thingy_dict_to_class(classname, d):
    print("{deserializer hook, converting to class: %s}" % d)
    return mycustomclasses.Thingy(d["number-attribute"])


def otherthingy_dict_to_class(classname, d):
    print("{deserializer hook, converting to class: %s}" % d)
    return mycustomclasses.OtherThingy(d["number"])


# for 'Thingy' we register both serialization and deserialization hooks
SerializerBase.register_dict_to_class("waheeee-custom-thingy", thingy_dict_to_class)
SerializerBase.register_class_to_dict(mycustomclasses.Thingy, thingy_class_to_dict)

# for 'OtherThingy' we only register a deserialization hook (and for serialization depend on serpent's default behavior)
SerializerBase.register_dict_to_class("mycustomclasses.OtherThingy", otherthingy_dict_to_class)


# regular Pyro server stuff

@expose
class Server(object):
    def method(self, arg):
        print("\nmethod called, arg=", arg)
        response = mycustomclasses.Thingy(999)
        return response
Exemple #4
0
            "__class__": "Pyro5.utils.messagebus.message",
            "msgid": str(obj.msgid),
            "created": obj.created.isoformat(),
            "data": obj.data
        }

    @classmethod
    def from_dict(cls, classname, d):
        return cls(
            uuid.UUID(d["msgid"]),
            datetime.datetime.strptime(d["created"], "%Y-%m-%dT%H:%M:%S.%f"),
            d["data"])


# make sure Pyro knows how to serialize the custom Message class
SerializerBase.register_class_to_dict(Message, Message.to_dict)
SerializerBase.register_dict_to_class("Pyro5.utils.messagebus.message",
                                      Message.from_dict)


@expose
class Subscriber(object):
    def __init__(self, auto_consume=True, max_queue_size=5000):
        self.bus = Proxy("PYRONAME:" + PYRO_MSGBUS_NAME)
        self.received_messages = queue.Queue(maxsize=max_queue_size)
        if auto_consume:
            self.__bus_consumer_thread = threading.Thread(
                target=self.__bus_consume_message)
            self.__bus_consumer_thread.daemon = True
            self.__bus_consumer_thread.start()
Exemple #5
0
    @oneway
    @expose
    def world_update(self, iteration, world, robotdata):
        if iteration % 50 == 0:
            self.robot.emote("I'm scared!")


observers = {
    "drunk": DrunkenGameObserver,
    "angry": AngryGameObserver,
    "scared": ScaredGameObserver,
}

# register the Robot class with Pyro's serializers:
SerializerBase.register_class_to_dict(robot.Robot, robot.Robot.robot_to_dict)
SerializerBase.register_dict_to_class("robot.Robot", robot.Robot.dict_to_robot)


def main(args):
    if len(args) != 3:
        print("usage: client.py <robotname> <robottype>")
        print("   type is one of: %s" % list(observers.keys()))
        return
    name = args[1]
    observertype = args[2]
    with Daemon() as daemon:
        observer = observers[observertype]()
        daemon.register(observer)
        gameserver = Proxy("PYRONAME:example.robotserver")
        robot = gameserver.register(name, observer)
Exemple #6
0
from Pyro5.api import expose, Proxy, SerializerBase
from customdata import CustomData

# teach Serpent how to deserialize our custom data class
SerializerBase.register_dict_to_class(CustomData.serialized_classname,
                                      CustomData.from_dict)


class Listener(object):
    def __init__(self, topic):
        self.topic = topic

    def register_with_dispatcher(self):
        with Proxy("PYRONAME:example.blobdispatcher") as dispatcher:
            dispatcher.register(self.topic, self)

    @expose
    def process_blob(self, blob):
        assert blob.info == self.topic
        customdata = blob.deserialized()
        print("Received custom data (type={}):".format(type(customdata)))
        print("    a={}, b={}, c={}".format(customdata.a, customdata.b,
                                            customdata.c))