Revision 2acc860a ncclient/session.py

b/ncclient/session.py
17 17
from Queue import Queue
18 18

  
19 19
import content
20
from capabilities import CAPABILITIES
20
from capabilities import Capabilities, CAPABILITIES
21 21
from error import ClientError
22 22
from subject import Subject
23 23

  
......
29 29
    
30 30
    def __init__(self):
31 31
        Thread.__init__(self, name='session')
32
        Subject.__init__(self, listeners=[Session.HelloListener(self)])
32
        Subject.__init__(self, listeners=[HelloListener(self)])
33 33
        self._client_capabilities = CAPABILITIES
34 34
        self._server_capabilities = None # yet
35 35
        self._id = None # session-id
......
42 42
        # start the subclass' main loop
43 43
        self.start()
44 44
        # queue client's hello message for sending
45
        self.send(content.make_hello(self._client_capabilities))
45
        self.send(content.Hello.build(self._client_capabilities))
46 46
        # we expect server's hello message, wait for _init_event to be set by HelloListener
47 47
        self._init_event.wait()
48 48
        # there may have been an error
......
50 50
            self._close()
51 51
            raise self._error
52 52
    
53
    def initialize(self, id, capabilities):
54
        self._id, self._capabilities = id, Capabilities(capabilities)
55
        self._init_event.set()
56
    
57
    def initialize_error(self, err):
58
        self._error = err
59
        self._init_event.set()
60
    
53 61
    def send(self, message):
54
        message = (u'<?xml version="1.0" encoding="UTF-8"?>%s' %
55
                   message).encode('utf-8')
56 62
        logger.debug('queueing message: \n%s' % message)
57 63
        self._q.put(message)
58 64
    
......
61 67

  
62 68
    def run(self):
63 69
        raise NotImplementedError
70
        
71
    def capabilities(self, whose='client'):
72
        if whose == 'client':
73
            return self._client_capabilities
74
        elif whose == 'server':
75
            return self._server_capabilities
64 76
    
65 77
    ### Properties
66

  
78
    
67 79
    @property
68 80
    def client_capabilities(self):
69 81
        return self._client_capabilities
......
79 91
    @property
80 92
    def id(self):
81 93
        return self._id
82
    
83
    class HelloListener:
84
        
85
        def __str__(self):
86
            return 'HelloListener'
87
        
88
        def __init__(self, session):
89
            self._session = session
90
        
91
        def _done(self, err=None):
92
            if err is not None:
93
                self._session._error = err
94
            self._session.remove_listener(self)
95
            self._session._init_event.set()
96
        
97
        def reply(self, data):
98
            err = None
99
            try:
100
                id, capabilities = content.parse_hello(data)
101
                logger.debug('session_id: %s | capabilities: \n%s', id, capabilities)
102
                self._session._id, self._session.capabilities = id, capabilities
103
            except Exception as e:
104
                err = e
105
            finally:
106
                self._done(err)
107
        
108
        def error(self, err):
109
            self._done(err)

Also available in: Unified diff