spoon.messaging.messaging

Package spoon :: Package messaging :: Module messaging |
[hide private]
[frames] | no frames]

# Source Code for Module spoon.messaging.messaging

      1  #
      2  # Copyright (C) 2006, Matt Sullivan <matts@zarrf.com>
      3  #
      4  # Permission is hereby granted, free of charge, to any person obtaining
      5  # a copy of this software and associated documentation files (the
      6  # "Software"), to deal in the Software without restriction, including
      7  # without limitation the rights to use, copy, modify, merge, publish,
      8  # distribute, sublicense, and/or sell copies of the Software, and to
      9  # permit persons to whom the Software is furnished to do so, subject to
     10  # the following conditions:
     11  #
     12  # The above copyright notice and this permission notice shall be included
     13  # in all copies or substantial portions of the Software.
     14  #
     15  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
     16  # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     17  # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
     18  # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
     19  # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
     20  # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
     21  # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     22  #
     23
     24  from spoon import NMTYPE_MESSAGING
     25
     26
     27 -class Messaging(object):
     28      """
     29      This is the main messaging class that implements the basic functionality for Spoon.
     30      Messaging implementations that need specific functionality (such as reliablility) will
     31      probably want to subclass from this.
     32
     33      There may be multiple instances of Messaging per python process, however there should only be one per
     34      network to which a node is a member.  There may be a case where one would want a single Messaging instance
     35      shared between networks however, and as long as the node ids on the networks do not overlap, you shouldn't have
     36      any problems.
     37
     38      You cannot use the acceptMsg decorator with this, for that you have to use the SingletonMessaging class.
     39      To register handlers with instances of Messaging, you must use the registerHandler method on
     40      the Messaging instance.
     41      """
     42 -    def __init__(self, network = None):
     43          self.handlers = {}
     44          self.network = network
     45          if network:
     46              self.network.nmtypes[NMTYPE_MESSAGING] = self
     47
     48 -    def registerHandler(self, msgtype, handler):
     49          handlers = self.handlers.get(msgtype, None)
     50          if handlers:
     51              handlers.append(handler)
     52          else:
     53              self.handlers[msgtype] = [handler]
     54
     55 -    def unregisterHandler(self, msgtype, handler):
     56          handlers = self.handlers.get(msgtype, None)
     57          if handlers:
     58              try:
     59                  handlers.remove(handler)
     60              except:
     61                  pass
     62
     63 -    def handleMessage(self, src, msg):
     64          """
     65          Calls the all handlers for the given message.
     66          @param src: The source node of the message
     67          @param msg: A list containing the message type, and the attached object.
     68          """
     69          handlers = self.handlers.get(msg[0], [])
     70          for handler in handlers:
     71              handler(src, msg[0], msg[1])
     72
     73 -    def setNetwork(self, network):
     74          self.network = network
     75          self.network.nmtypes[NMTYPE_MESSAGING] = self
     76
     77 -    def send(self, dst, messageStr, obj):
     78          """
     79          Sends a Messaging message (not just a NetMessage) to the
     80          destination node.
     81          @param dst: Destination node id
     82          @param messageStr: A string describing the message type.
     83          @param obj: Some object attached the net message.
     84          """
     85          self.network.sendNetMsg(dst, NMTYPE_MESSAGING, (messageStr, obj))
     86
     87
     88 -class SingletonMessaging(Messaging):
     89      """
     90      For convinience this is a singleton version of the Messaging system.
     91      This can be used when the node will only have one Messaging system
     92      throughout the life of the process.  Furthermore, if this is the case
     93      you can use the function/static method decorator acceptMsg with this.
     94      """
     95      singleton = None
     96
     97      @staticmethod
     98 -    def getinstance():
     99          if SingletonMessaging.singleton:
    100              return SingletonMessaging.singleton
    101          else:
    102              SingletonMessaging.singleton = SingletonMessaging()
    103              return SingletonMessaging.singleton
    104
    105      @staticmethod
    106 -    def hasinstance():
    107          return SingletonMessaging.singleton != None
    108
    109 -def send(dst, messageStr, obj):
    110      """
    111      Shortcut for calling send on the SingletonMessaging class.
    112      @param dst: Destination node id
    113      @param messageStr: A string describing the message type.
    114      @param obj: Some object attached the net message.
    115      """
    116      SingletonMessaging.getinstance().send(dst, messageStr, obj)
    117