Statistics
| Branch: | Tag: | Revision:

root / src / operations.py @ 4a2351a5

History | View | Annotate | Download (1.7 kB)

1
# Copyright 2009 Shikhar Bhushan
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License");
4
# you may not use this file except in compliance with the License.
5
# You may obtain a copy of the License at
6
#
7
#    http://www.apache.org/licenses/LICENSE-2.0
8
#
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS,
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
# See the License for the specific language governing permissions and
13
# limitations under the License.
14

    
15
from listener import Listener
16

    
17
from threading import Event
18

    
19
class OperationError(NETCONFError): pass
20

    
21
class RPC:
22
    pass
23

    
24
class RPCRequest(RPC):
25
    
26
    cur_msg_id = {}
27

    
28
    @cls
29
    def next_id(cls, session_id):
30
        cur_msg_id[session_id] = cur_msg_id.get(session_id, 0) + 1
31
        return cur_msg_id[session_id]
32

    
33
    def __init__(self):
34
        self._reply = None
35
        self._event = Event()
36
        self._async = None
37

    
38
    def get_reply(self, timeout=2.0):
39
        self._event.wait(timeout)
40
        if self._event.isSet():
41
            return self._reply
42

    
43
    def do(self, session, async=False):
44
        self._async = async
45
        
46
    @property
47
    def async(self):
48
        return self._async
49
    
50
class RPCReply(RPC):
51
    pass
52

    
53
class RPCError(OperationError):
54
    pass
55

    
56
class Operation(RPCRequest):
57
    pass
58

    
59
class ReplyListener(Listener):
60
    
61
    def __init__(self):
62
        self._id2op = {}
63
    
64
    def reply(self, msg):
65
        # if all good:
66
        op = id2op[id]
67
        op._reply = parsed_msg
68
        # else:
69
        self._error = True
70
        
71
        op._event.set()
72
        pass
73
    
74
    def error(self, buf):
75
        pass