Commit 37501d9a authored by Antoine Millet's avatar Antoine Millet
Browse files

Added create_rpc and create_tunnel shortcuts with label auto assignation

parent 2c3b6c7d
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -23,3 +23,11 @@ class SocketRpcError(Exception):
    '''
    Exception used internally to raise a socket fault.
    '''


class NoFreeLabelError(Exception):

    '''
    Exception raised when no more free labels are available for protocol
    allocation.
    '''
+21 −4
Original line number Diff line number Diff line
@@ -11,9 +11,8 @@ import struct
import socket
import logging

from sjrpc.core.protocols.rpc import RpcProtocol
from sjrpc.core.protocols import Protocol
from sjrpc.core.exceptions import RpcError
from sjrpc.core.protocols import Protocol, RpcProtocol, TunnelProtocol
from sjrpc.core.exceptions import RpcError, NoFreeLabelError

import pyev

@@ -54,6 +53,7 @@ class RpcConnection(object):

    MESSAGE_HEADER = '!HL'
    MESSAGE_HEADER_FALLBACK = '!L'
    MAX_LABEL = 2 ** 16
    SHORTCUTS_MAINRPC = ('call', 'async_call')

    def __init__(self, sock, loop=None, enable_tcp_keepalive=False,
@@ -294,7 +294,12 @@ class RpcConnection(object):
        '''
        Register a new protocol for the specified label.
        '''

        if label is None:
            for label in xrange(0, RpcConnection.MAX_LABEL):
                if label not in self._protocols:
                    break
            else:
                raise NoFreeLabelError('No more label number are availables')
        if label in self._protocols:
            raise KeyError('A protocol is already registered for this label')
        elif not isinstance(label, int):
@@ -312,6 +317,18 @@ class RpcConnection(object):
        else:
            raise KeyError('No protocol registered for this label')

    def create_rpc(self, label=None, *args, **kwargs):
        '''
        Shortcut which can be used to create rpc protocols.
        '''
        return self.register_protocol(label, RpcProtocol, *args, **kwargs)

    def create_tunnel(self, label=None, *args, **kwargs):
        '''
        Shortcut which can be used to create tunnels protocols.
        '''
        return self.register_protocol(label, TunnelProtocol, *args, **kwargs)

    def get_protocol(self, label):
        '''
        Get the protocol registered for specified label.