Package pyamf :: Package flex :: Module messaging
[hide private]
[frames] | no frames]

Source Code for Module pyamf.flex.messaging

  1  # Copyright (c) 2007-2008 The PyAMF Project. 
  2  # See LICENSE for details. 
  3   
  4  """ 
  5  Flex Messaging implementation. 
  6   
  7  This module contains the message classes used with Flex Data Services. 
  8   
  9  @see: U{RemoteObject on OSFlash (external) 
 10  <http://osflash.org/documentation/amf3#remoteobject>} 
 11   
 12  @author: U{Arnar Birgisson<mailto:arnarbi@gmail.com>} 
 13  @author: U{Thijs Triemstra<mailto:info@collab.nl>} 
 14  @author: U{Nick Joyce<mailto:nick@boxdesign.co.uk>} 
 15   
 16  @since: 0.1.0 
 17  """ 
 18   
 19  import pyamf 
 20   
 21  __all__ = [ 
 22      'RemotingMessage', 
 23      'CommandMessage', 
 24      'AcknowledgeMessage', 
 25      'ErrorMessage' 
 26  ] 
 27   
28 -class AbstractMessage(object):
29 """ 30 Abstract base class for all Flex messages. 31 32 Messages have two customizable sections; headers and data. The headers 33 property provides access to specialized meta information for a specific 34 message instance. The data property contains the instance specific data 35 that needs to be delivered and processed by the decoder. 36 37 @see: U{AbstractMessage on Livedocs (external) 38 <http://livedocs.adobe.com/flex/201/langref/mx/messaging/messages/AbstractMessage.html>} 39 40 @ivar body: Specific data that needs to be delivered to the remote 41 destination. 42 @type body: C{mixed} 43 @ivar clientId: Indicates which client sent the message. 44 @type clientId: C{str} 45 @ivar destination: Message destination. 46 @type destination: C{str} 47 @ivar headers: Message headers. Core header names start with DS. 48 @type headers: C{dict} 49 @ivar messageId: Unique Message ID. 50 @type messageId: C{str} 51 @ivar timeToLive: How long the message should be considered valid and 52 deliverable. 53 @type timeToLive: C{int} 54 @ivar timestamp: Timestamp when the message was generated. 55 @type timestamp: C{int} 56 """ 57 58 #: Each message pushed from the server will contain this header identifying 59 #: the client that will receive the message. 60 DESTINATION_CLIENT_ID_HEADER = "DSDstClientId" 61 #: Messages are tagged with the endpoint id for the channel they are sent 62 #: over. 63 ENDPOINT_HEADER = "DSEndpoint" 64 #: Messages that need to set remote credentials for a destination carry the 65 #: C{Base64} encoded credentials in this header. 66 REMOTE_CREDENTIALS_HEADER = "DSRemoteCredentials" 67 #: The request timeout value is set on outbound messages by services or 68 #: channels and the value controls how long the responder will wait for an 69 #: acknowledgement, result or fault response for the message before timing 70 #: out the request. 71 REQUEST_TIMEOUT_HEADER = "DSRequestTimeout" 72
73 - def __init__(self, *args, **kwargs):
74 self.body = kwargs.get('body', None) 75 self.clientId = kwargs.get('clientId', None) 76 self.destination = kwargs.get('destination', None) 77 self.headers = kwargs.get('headers', {}) 78 self.messageId = kwargs.get('messageId', None) 79 self.timeToLive = kwargs.get('timeToLive', 0) 80 self.timestamp = kwargs.get('timestamp', 0)
81
82 - def __repr__(self):
83 m = '<%s ' % self.__class__.__name__ 84 85 for k, v in self.__dict__.iteritems(): 86 m += ' %s=%s' % (k, v) 87 88 return m + " />"
89
90 -class AsyncMessage(AbstractMessage):
91 """ 92 I am the base class for all asynchronous Flex messages. 93 94 @see: U{AsyncMessage on Livedocs (external) 95 <http://livedocs.adobe.com/flex/201/langref/mx/messaging/messages/AsyncMessage.html>} 96 97 @ivar correlationId: Correlation id of the message. 98 @type correlationId: C{str} 99 """ 100 101 #: Messages that were sent with a defined subtopic property indicate their 102 #: target subtopic in this header. 103 SUBTOPIC_HEADER = "DSSubtopic" 104
105 - def __init__(self, *args, **kwargs):
106 AbstractMessage.__init__(self, *args, **kwargs) 107 108 self.correlationId = kwargs.get('correlationId', None)
109
110 -class AcknowledgeMessage(AsyncMessage):
111 """ 112 I acknowledge the receipt of a message that was sent previously. 113 114 Every message sent within the messaging system must receive an 115 acknowledgement. 116 117 @see: U{AcknowledgeMessage on Livedocs (external) 118 <http://livedocs.adobe.com/flex/201/langref/mx/messaging/messages/AcknowledgeMessage.html>} 119 """ 120 121 #: Used to indicate that the acknowledgement is for a message that 122 #: generated an error. 123 ERROR_HINT_HEADER = "DSErrorHint"
124
125 -class CommandMessage(AsyncMessage):
126 """ 127 Provides a mechanism for sending commands related to publish/subscribe 128 messaging, ping, and cluster operations. 129 130 @see: U{CommandMessage on Livedocs (external) 131 <http://livedocs.adobe.com/flex/201/langref/mx/messaging/messages/CommandMessage.html>} 132 133 @ivar operation: The command 134 @type operation: C{int} 135 @ivar messageRefType: hmm, not sure about this one. 136 @type messageRefType: C{str} 137 """ 138 139 #: The server message type for authentication commands. 140 AUTHENTICATION_MESSAGE_REF_TYPE = "flex.messaging.messages.AuthenticationMessage" 141 #: This is used to test connectivity over the current channel to the remote 142 #: endpoint. 143 PING_OPERATION = 5 144 #: This is used by a remote destination to sync missed or cached messages 145 #: back to a client as a result of a client issued poll command. 146 SYNC_OPERATION = 4 147 #: This is used to request a list of failover endpoint URIs for the remote 148 #: destination based on cluster membership. 149 CLUSTER_REQUEST_OPERATION = 7 150 #: This is used to send credentials to the endpoint so that the user can be 151 #: logged in over the current channel. The credentials need to be C{Base64} 152 #: encoded and stored in the body of the message. 153 LOGIN_OPERATION = 8 154 #: This is used to log the user out of the current channel, and will 155 #: invalidate the server session if the channel is HTTP based. 156 LOGOUT_OPERATION = 9 157 #: This is used to poll a remote destination for pending, undelivered 158 #: messages. 159 POLL_OPERATION = 2 160 #: Subscribe commands issued by a consumer pass the consumer's C{selector} 161 #: expression in this header. 162 SELECTOR_HEADER = "DSSelector" 163 #: This is used to indicate that the client's session with a remote 164 #: destination has timed out. 165 SESSION_INVALIDATE_OPERATION = 10 166 #: This is used to subscribe to a remote destination. 167 SUBSCRIBE_OPERATION = 0 168 #: This is the default operation for new L{CommandMessage} instances. 169 UNKNOWN_OPERATION = 1000 170 #: This is used to unsubscribe from a remote destination. 171 UNSUBSCRIBE_OPERATION = 1 172
173 - def __init__(self, *args, **kwargs):
174 AsyncMessage.__init__(self, *args, **kwargs) 175 176 self.operation = kwargs.get('operation', None) 177 #: Remote destination belonging to a specific service, based upon 178 #: whether this message type matches the message type the service 179 #: handles. 180 self.messageRefType = kwargs.get('messageRefType', None)
181
182 -class ErrorMessage(AcknowledgeMessage):
183 """ 184 I am the Flex error message to be returned to the client. 185 186 This class is used to report errors within the messaging system. 187 188 @see: U{ErrorMessage on Livedocs (external) 189 <http://livedocs.adobe.com/flex/201/langref/mx/messaging/messages/ErrorMessage.html>} 190 """ 191 192 #: If a message may not have been delivered, the faultCode will contain 193 #: this constant. 194 MESSAGE_DELIVERY_IN_DOUBT = "Client.Error.DeliveryInDoubt" 195 #: Header name for the retryable hint header. 196 #: 197 #: This is used to indicate that the operation that generated the error may 198 #: be retryable rather than fatal. 199 RETRYABLE_HINT_HEADER = "DSRetryableErrorHint" 200
201 - def __init__(self, *args, **kwargs):
202 AcknowledgeMessage.__init__(self, *args, **kwargs) 203 #: Extended data that the remote destination has chosen to associate 204 #: with this error to facilitate custom error processing on the client. 205 self.extendedData = kwargs.get('extendedData', {}) 206 #: Fault code for the error. 207 self.faultCode = kwargs.get('faultCode', None) 208 #: Detailed description of what caused the error. 209 self.faultDetail = kwargs.get('faultDetail', None) 210 #: A simple description of the error. 211 self.faultString = kwargs.get('faultString', None) 212 #: Should a traceback exist for the error, this property contains the 213 #: message. 214 self.rootCause = kwargs.get('rootCause', {})
215
216 -class RemotingMessage(AbstractMessage):
217 """ 218 I am used to send RPC requests to a remote endpoint. 219 220 @see: U{RemotingMessage on Livedocs (external) 221 <http://livedocs.adobe.com/flex/201/langref/mx/messaging/messages/RemotingMessage.html>} 222 """ 223
224 - def __init__(self, *args, **kwargs):
225 AbstractMessage.__init__(self, *args, **kwargs) 226 #: Name of the remote method/operation that should be called. 227 self.operation = kwargs.get('operation', None) 228 #: Name of the service to be called including package name. 229 #: This property is provided for backwards compatibility. 230 self.source = kwargs.get('source', None)
231 232 for x in (RemotingMessage, ErrorMessage, CommandMessage, AcknowledgeMessage, AsyncMessage): 233 pyamf.register_class(x, 'flex.messaging.messages.%s' % x.__name__, metadata=['amf3']) 234 del x 235