File: test_newapi11.py

package info (click to toggle)
python-autobahn 17.10.1%2Bdfsg1-7
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,452 kB
  • sloc: python: 22,598; javascript: 2,705; makefile: 497; sh: 3
file content (262 lines) | stat: -rw-r--r-- 7,839 bytes parent folder | download | duplicates (3)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
@coroutine
def main(reactor, session):
    # the session is joined and ready
    result = yield session.call(u'com.example.add2', 2, 3)
    print('result={}'.format(result))
    # as we exit, this signals we are done with the session! the session
    # can be recycled

if __name__ == '__main__':
    client = Client(main=main)
    react(client.run)


@coroutine
def setup(reactor, session):
    # the session is joined and ready also!
    def add2(a, b):
        return a + b
    yield session.register(u'com.example.add2', add2)
    print('procedure registered')
    # as we exit, this signals we are ready! the session must be kept.

if __name__ == '__main__':
    client = Client(setup=setup)
    react(client.run)




@coroutine
def client_main(reactor, client):

    @coroutine
    def transport_main(reactor, transport):

        @coroutine
        def session_main(reactor, session):
            result = yield session.call(u'com.example.add2', 2, 3)
            print('result={}'.format(result))

        # returns when the session_main has finished (!), the session
        # calls leave() or the underlying transport closes
        yield transport.join(session_main, transport)

    # returns when the transport_main won't reconnect
    yield client.connect(transport_main)

if __name__ == '__main__':
    client = Client(client_main=client_main)
    react(client.run)



@coroutine
def session_main(reactor, session):
    result = yield session.call(u'com.example.add2', 2, 3)
    print('result={}'.format(result))


if __name__ == '__main__':
    client = Client(session_main=session_main)
    react(client.run)



@coroutine
def session_main(reactor, session):
    def add2(a, b):
        return a + b
    yield session.register(u'com.example.add2', add2)
    print('procedure registered')
    txaio.return_value(txaio.create_future())


if __name__ == '__main__':
    client = Client(session_main=session_main)
    react(client.run)


@coroutine
def main1(reactor, session, details):

    result = yield session.call(u'com.example.add2', 2, 3)
    print('result={}'.format(result))

    yield session.leave()


if __name__ == '__main__':
    # hooking into on_join is the highest-level API -
    # the user callback will fire with a joined session ready to use
    # both the transport auto-reconnection logic and the session creation
    # defaults in Client are reused
    client = Client(on_join=main1)
    react(client.run)


@coroutine
def main1(reactor, transport, details):
    # transport.join() yields a joined session object when successful
    session = yield transport.join(details.config.realm)

    # the session is joined and can be used
    result = yield session.call(u'com.example.add2', 2, 3)
    print('result={}'.format(result))

    yield session.leave()


if __name__ == '__main__':
    # hooking into on_connect is a medium-level API -
    # the user callback will fire with a connected transport which
    # can be used to create new sessions from. the auto-reconnection
    # logic in Client is reused. user code can reuse a transport while
    # joining/leaving multiple times. with a multiplexing capable transport,
    # user code may even create multiple concurrent sessions.
    client = Client(on_open=main1)
    react(client.run)


@coroutine
def main1(reactor, client, details):
    # client.open() yields a connected transport when successful
    transport = yield client.open()

    # create a session running over the transport
    session = yield transport.join(config.realm)
    result = yield session.call(u'com.example.add2', 2, 3)
    print('result={}'.format(result))
    yield session.leave()
    yield transport.close()


if __name__ == '__main__':
    # hookinh into on_create is a low-level API - the user callback
    # will fire with a created client, and the user code can
    # control the whole transport and session creation, connection and
    # reconnection process.
    client = Client(on_create=main1)
    react(client.run)


@coroutine
def main1(reactor, client, config):
    transport = yield client.open()
    session = yield transport.join(config.realm)
    result = yield session.call(u'com.example.add2', 2, 3)
    print('result={}'.format(result))
    yield session.leave()
    yield transport.close()


if __name__ == '__main__':
    # hookinh into on_create is a low-level API - the user callback
    # will fire with a created client, and the user code can
    # control the whole transport and session creation, connection and
    # reconnection process.
    client = Client(on_create=main1)
    react(client.run)


@coroutine
def main1(reactor, client, config):
    while True:
        delay = client.next_delay()
        if delay:
            yield sleep(delay)
        else:
            break
        try:
            # client.open() yields a connected WAMP transport
            with yield client.open() as transport:
                try:
                    with yield transport.join(config.realm) as session:
                        result = yield session.call(u'com.example.add2', 2, 3)
                        print('result={}'.format(result))
                except Exception as e:
                    pass
        except Exception as e:
            pass


if __name__ == '__main__':
    # hookinh into on_create is a low-level API - the user callback
    # will fire with a created client, and the user code can
    # control the whole transport and session creation, connection and
    # reconnection process.
    client = Client(on_create=main1)
    react(client.run)


@coroutine
def main2(reactor, connection):
    # create a new transport from the connection
    transport = yield connection.open()

    # create a new session running on the transport
    session = yield transport.join(connection.config.realm)

    # now register a procedure
    def add2(a, b):
        return a + b

    yield session.register(u'com.example.add2', add2)

    # and call the procedure
    result = yield session.call(u'com.example.add2', 2, 3)
    print('result={}'.format(result))

    # now leave the realm, which frees the underlying transport
    # but freeze the session
    yield session.leave(freeze=True)

    # .. sleep, but not too long, otherwise router finally kills the session.
    yield sleep(60)

    # create a second, new transport from the connection
    # this might be a 2nd TCP connection or a 2nd logical WAMP transport running
    # over a single, multiplexed connection
    transport2 = yield connection.open()

    # now resume the session on the new transport. using the session token mechanism,
    # the router will resume the session and deliver buffered events/calls to the
    # resumed session
    yield session.resume(transport2)

    # create a 2nd session running over the 1st transport
    session2 = transport.join(connection.config.realm)

    # call the procedure registered on the (resumed) session running on transport2
    result = yield session.call(u'com.example.add2', 2, 3)
    print('result={}'.format(result))

    # if the transport supports multiplexing, multiple session can run
    # concurrently over the underlying transport
    if transport.is_multiplexed:
        session3 = yield transport.join(connection.config.realm)

    # now finally leave sessions ..
    yield session.leave()
    yield session2.leave()

    # .. and close the transports
    yield transport.close()
    yield transport2.close()


if __name__ == '__main__':

    transports = [
        {
            'type': 'rawsocket',
            'serializer': 'msgpack',
            'endpoint': {
                'type': 'unix',
                'path': '/tmp/cb1.sock'
            }
        }
    ]
    config = Config(realm=u'myrealm1')
    connection = Connection(main, transports=transports, config=config)
    react(connection.start)