Module bjsonrpc.connection

class bjsonrpc.connection.Connection(sck, address=None, handler_factory=None)

Represents a communiation tunnel between two parties.

sck
Connected socket to use. Should be an instance of socket.socket or something compatible.
address
Address of the other peer in (host,port) form. It is only used to inform handlers about the peer address.
handler_factory
Class type inherited from BaseHandler which holds the public methods. It defaults to NullHandler meaning no public methods will be avaliable to the other end.

Members:

call
Synchronous Proxy. It forwards your calls to it to the other end, waits the response and returns the value
method
Asynchronous Proxy. It forwards your calls to it to the other end and inmediatelly returns a request.Request instance.
notify
Notification Proxy. It forwards your calls to it to the other end and tells the server to not response even if there’s any error in the call. Returns None.
addrequest(request)

Adds a request to the queue of requests waiting for response.

close()

Close the connection and the socket.

dispatch_item_single(item)

Given a JSON item received from socket, determine its type and process the message.

dispatch_item_threaded(item)

If threaded mode is activated, this function creates a new thread per each item received and returns without blocking.

dispatch_until_empty()

Calls read_and_dispatch method until there are no more messages to dispatch in the buffer.

Returns the number of operations that succeded.

This method will never block waiting. If there aren’t any more messages that can be processed, it returns.

dump_object(obj)

Helper function to convert classes and functions to JSON objects.

Given a incompatible object called obj, dump_object returns a JSON hinted object that represents the original parameter.

Parameters:

obj
Object, class, function,etc which is incompatible with JSON serialization.
(return value)
A valid serialization for that object using JSON class hinting.
get_id()

Retrieves a new ID counter. Each connection has a exclusive ID counter.

It is mainly used to create internal id’s for calls.

classmethod getmaxtimeout(operation)

Get the maximum timeout in seconds for operation operation.

Parameters:

operation
The operation which has to be configured. Can be either ‘read’ or ‘write’.
(return value)
The timeout in seconds as a floating number or None.
load_object(obj)

Helper function for JSON loads. Given a dictionary (javascript object) returns an apropiate object (a specific class) in certain cases.

It is mainly used to convert JSON hinted classes back to real classes.

Parameters:

obj
Dictionary-like object to test.
(return value)
Either the same dictionary, or a class representing that object.
proxy(sync_type, name, args, kwargs)

Call method on server.

sync_type ::
= 0 .. call method, wait, get response. = 1 .. call method, inmediate return of object. = 2 .. call notification and exit.
read(timeout=None)

Standard function to read from the socket which by default points to read_line

read_and_dispatch(timeout=None, thread=True, condition=None)

Read one message from socket (with timeout specified by the optional argument timeout) and dispatches that message.

Parameters:

timeout = None
Timeout in seconds of the read operation. If it is None (or ommitted) then the read will wait until new data is available.
(return value)
True, in case of the operation has suceeded and one message has been dispatched. False, if no data or malformed data has been received.
read_line()

Read a line of data from socket. It removes the n at the end before returning the value.

If the original packet contained n, the message will be decoded as two or more messages.

Returns the line of data received from the socket.

serve()

Basic function to put the connection serving. Usually is better to use server.Server class to do this, but this would be useful too if it is run from a separate Thread.

classmethod setmaxtimeout(operation, value)

Set the maximum timeout in seconds for operation operation.

Parameters:

operation
The operation which has to be configured. Can be either ‘read’ or ‘write’.
value
The timeout in seconds as a floating number. If is None, will block until succeed. If is 0, will be nonblocking.
settimeout(operation, timeout)

configures a timeout for the connection for a given operation. operation is one of “read” or “write”

socket

public property that holds the internal socket used.

write(data, timeout=None)
write_line(data)

Write a line data to socket. It appends a newline at the end of the data before sending it.

The string MUST NOT contain newline otherwise an AssertionError will raise.

Parameters:

data
String containing the data to be sent.
write_now(data, timeout=None)

Standard function to write to the socket which by default points to write_line

write_thread()
class bjsonrpc.connection.RemoteObject(conn, obj)

Represents a object in the server-side (or client-side when speaking from the point of view of the server) . It remembers its name in the server-side to allow calls to the original object.

Parameters:

conn
Connection object which holds the socket to the other end of the communications
obj
JSON object (Python dictionary) holding the values recieved. It is used to retrieve the properties to create the remote object. (Initially only used to get object name)

Example:

list = conn.call.newList()
for i in range(10): list.notify.add(i)

print list.call.getitems()

Attributes:

name
name of the object in the server-side
call
Synchronous Proxy. It forwards your calls to it to the other end, waits the response and returns the value.
method
Asynchronous Proxy. It forwards your calls to it to the other end and inmediatelly returns a request.Request instance.
notify
Notification Proxy. It forwards your calls to it to the other end and tells the server to not response even if there’s any error in the call. Returns None.
close()

Closes/deletes the remote object. The server may or may not delete it at this time, but after this call we don’t longer have any access to it.

This method is automatically called when Python deletes this instance.

connection

Public property to get the internal connection object.

Previous topic

Module bjsonrpc.server

Next topic

Module bjsonrpc.request

This Page