File: client.py

package info (click to toggle)
circuits 3.1.0%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 9,764 kB
  • sloc: python: 15,945; makefile: 131
file content (72 lines) | stat: -rw-r--r-- 1,495 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python

from optparse import OptionParser

from circuits.node import Node, remote
from circuits import Component, Debugger, handler, Event


__version__ = "0.0.1"

USAGE = "%prog [options]"
VERSION = "%prog v" + __version__


class increment(Event):
    def __init__(self, value):
        Event.__init__(self, value)


def parse_options():
    parser = OptionParser(usage=USAGE, version=VERSION)

    parser.add_option(
        "-i", "--ip",
        action="store", type="string",
        default="127.0.0.1:8000", dest="bind",
        help="Bind to address:[port]"
    )

    parser.add_option(
        "-d", "--debug",
        action="store_true",
        default=False, dest="debug",
        help="Enable debug mode"
    )

    opts, args = parser.parse_args()

    return opts, args


class NodeClient(Component):

    def init(self, args, opts):
        if opts.debug:
            Debugger().register(self)

        if ":" in opts.bind:
            address, port = opts.bind.split(":")
            port = int(port)
        else:
            address, port = opts.bind, 8000

        node = Node().register(self)
        node.add('peer_name', address, port)

    def ready(self, client):
        i = 0
        while True:
            print(i)
            i = (yield self.call(remote(increment(i), 'peer_name'))).value


def main():
    opts, args = parse_options()

    # Configure and "run" the System.
    NodeClient(args, opts).run()


if __name__ == "__main__":
    main()