1# Copyright Maciej Sobczak 2008-2019.
2# This file is part of YAMI4.
3#
4# YAMI4 is free software: you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# YAMI4 is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with YAMI4.  If not, see <http://www.gnu.org/licenses/>.
16
17import sys
18import yami
19
20def update_handler(message):
21    content = message.get_parameters()
22    value = content["value"]
23    print("received update:", value)
24
25
26if len(sys.argv) != 2:
27    print("expecting one parameter: publisher destination")
28    exit()
29
30publisher_address = sys.argv[1]
31
32try:
33    with yami.Agent() as subscriber_agent:
34
35        # prepare subscription update callback
36
37        update_object_name = "update_handler"
38
39        subscriber_agent.register_object(
40            update_object_name, update_handler)
41
42        # subscribe to the producer
43
44        params = {"destination_object":update_object_name}
45
46        subscriber_agent.send_one_way(publisher_address,
47            "random_number", "subscribe", params)
48
49        print("subscribed, waiting for updates")
50
51        # block forever and receive updates in background
52
53        dummy = sys.stdin.read()
54
55except Exception as e:
56    print("error:", e)
57