Show More
@@ -1,59 +1,60 b'' | |||
|
1 | 1 | from __future__ import absolute_import, print_function |
|
2 | 2 | |
|
3 | 3 | import StringIO |
|
4 | ||
|
5 | from mercurial import wireproto | |
|
4 | from mercurial import ( | |
|
5 | wireproto, | |
|
6 | ) | |
|
6 | 7 | |
|
7 | 8 | class proto(object): |
|
8 | 9 | def __init__(self, args): |
|
9 | 10 | self.args = args |
|
10 | 11 | def getargs(self, spec): |
|
11 | 12 | args = self.args |
|
12 | 13 | args.setdefault('*', {}) |
|
13 | 14 | names = spec.split() |
|
14 | 15 | return [args[n] for n in names] |
|
15 | 16 | |
|
16 | 17 | class clientpeer(wireproto.wirepeer): |
|
17 | 18 | def __init__(self, serverrepo): |
|
18 | 19 | self.serverrepo = serverrepo |
|
19 | 20 | |
|
20 | 21 | def _capabilities(self): |
|
21 | 22 | return ['batch'] |
|
22 | 23 | |
|
23 | 24 | def _call(self, cmd, **args): |
|
24 | 25 | return wireproto.dispatch(self.serverrepo, proto(args), cmd) |
|
25 | 26 | |
|
26 | 27 | def _callstream(self, cmd, **args): |
|
27 | 28 | return StringIO.StringIO(self._call(cmd, **args)) |
|
28 | 29 | |
|
29 | 30 | @wireproto.batchable |
|
30 | 31 | def greet(self, name): |
|
31 | 32 | f = wireproto.future() |
|
32 | 33 | yield {'name': mangle(name)}, f |
|
33 | 34 | yield unmangle(f.value) |
|
34 | 35 | |
|
35 | 36 | class serverrepo(object): |
|
36 | 37 | def greet(self, name): |
|
37 | 38 | return "Hello, " + name |
|
38 | 39 | |
|
39 | 40 | def filtered(self, name): |
|
40 | 41 | return self |
|
41 | 42 | |
|
42 | 43 | def mangle(s): |
|
43 | 44 | return ''.join(chr(ord(c) + 1) for c in s) |
|
44 | 45 | def unmangle(s): |
|
45 | 46 | return ''.join(chr(ord(c) - 1) for c in s) |
|
46 | 47 | |
|
47 | 48 | def greet(repo, proto, name): |
|
48 | 49 | return mangle(repo.greet(unmangle(name))) |
|
49 | 50 | |
|
50 | 51 | wireproto.commands['greet'] = (greet, 'name',) |
|
51 | 52 | |
|
52 | 53 | srv = serverrepo() |
|
53 | 54 | clt = clientpeer(srv) |
|
54 | 55 | |
|
55 | 56 | print(clt.greet("Foobar")) |
|
56 | 57 | b = clt.batch() |
|
57 | 58 | fs = [b.greet(s) for s in ["Fo, =;:<o", "Bar"]] |
|
58 | 59 | b.submit() |
|
59 | 60 | print([f.value for f in fs]) |
General Comments 0
You need to be logged in to leave comments.
Login now