forked from vg/netsukuku
/
test_microrpc.py
151 lines (115 loc) · 3.45 KB
/
test_microrpc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
##
# This file is part of Netsukuku
# (c) Copyright 2008 Andrea Lo Pumo aka AlpT <alpt@freaknet.org>
#
# This source code is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation; either version 2 of the License,
# or (at your option) any later version.
#
# This source code is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# Please refer to the GNU Public License for more details.
#
# You should have received a copy of the GNU Public License along with
# this source code; if not, write to:
# Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
##
#
# Test suite for rpc.py
#
import sys
sys.path.append('..')
from ntk.wrap.sock import Sock
socket=Sock()
import logging
import ntk.lib.rpc as rpc
from ntk.lib.micro import micro, allmicro_run, micro_block
from random import randint
PORT=randint(8880, 8889)
# Logging option
LOG_LEVEL = logging.DEBUG
LOG_FILE = ''
log_config = {
'level': LOG_LEVEL,
'format': '%(levelname)s %(message)s'
}
if LOG_FILE == '':
log_config['stream'] = sys.stdout
else:
log_config['filename'] = LOG_FILE
logging.basicConfig(**log_config)
#
### The server remotable functions
#
class MyNestedMod:
def __init__(self):
self.remotable_funcs = [self.add]
def add(self, x,y): return x+y
class MyMod:
def __init__(self):
self.nestmod = MyNestedMod()
self.remotable_funcs = [self.square, self.mul, self.caller_test,
self.void_func_caller, self.void_func]
def square(self, x): return x*x
def mul(self, x, y): return x*y
def private_func(self): pass
def caller_test(self, _rpc_caller, x, y):
c = _rpc_caller
logging.debug("caller test: "+str([c.ip, c.port, c.dev, c.socket]))
return (x,y)
def void_func_caller(self, _rpc_caller):
c = _rpc_caller
logging.debug("void func caller: "+str([c.ip, c.port, c.dev, c.socket]))
def void_func(self):
logging.debug("void_func")
mod = MyMod()
#
#### TCP client
#
def tcp_client():
client = rpc.TCPClient(port=PORT)
x=5
xsquare = client.square(x)
assert xsquare == 25
xmul7 = client.mul(x, 7)
assert xmul7 == 35
xadd9 = client.nestmod.add(x, 9)
assert xadd9 == 14
# something trickier
n, nn = client, client.nestmod
result = n.square(n.mul(x, nn.add(x, 10)))
assert (1,2) == client.caller_test(1,2)
try:
# should crash now
client.private_func()
except Exception, e:
logging.debug(str(e))
#
### Bcast client
#
def udp_client():
client = rpc.BcastClient(devs=['lo'], port=PORT)
print "calling void func"
client.void_func()
client.void_func_caller()
print "udp_client end"
def run_test_tcp():
print 'Starting tcp server...'
rpc.MicroTCPServer(mod, ('localhost', PORT))
micro(tcp_client)
allmicro_run()
def run_test_udp():
print 'Starting udp server...'
rpc.MicroUDPServer(mod, ('', PORT))
micro(udp_client)
allmicro_run()
if __name__ == '__main__':
if len(sys.argv) == 1:
print "specify udp or tcp"
sys.exit(1)
if sys.argv[1]== 'tcp':
run_test_tcp()
if sys.argv[1]== 'udp':
run_test_udp()