webXMPP

Check-in [3ffad81c57]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Counterpart to protocol changes in chore/pong.py, we are now NAT/firewall friendly.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | master | trunk
Files: files | file ages | folders
SHA3-256:3ffad81c57fd111eee9f7e08f74959c0e487c04b824afde6bd8893a5299c31b4
User & Date: vandyswa@gmail.com 2017-07-01 00:00:55
Context
2017-07-01
00:03
Merge branch 'master' of https://github.com/vandys/webXMPP check-in: bbd719910e user: ajv-899-334-8894@vsta.org tags: master, trunk
00:00
Counterpart to protocol changes in chore/pong.py, we are now NAT/firewall friendly. check-in: 3ffad81c57 user: vandyswa@gmail.com tags: master, trunk
2017-06-30
05:09
Merge branch 'master' of https://github.com/vandys/-webXMPP check-in: 04a597a22e user: vandyswa@gmail.com tags: master, trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to notified.py.

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

    # Watch DBus to turn LED's off when screen unlocks
    setup_dbus()

    # Prepare for notifications
    notify2.init("Messsaging")















    # Params; we in particular need to know the server's
    #  intended timeout.
    while True:
        pak = conn.pingpong(conn.msg("params", "get"))
        if pak is not None:
            break


        time.sleep(pong.WAITNET)

    # This is how long they'll hold a notify/get pending before
    #  sending back a null result.
    # If we request one and don't hear back in this amount of
    #  time, we have a lost packet or something.


    tmo = pak.inner["timeout"] * 1.1

    # Server loop
    dest = (cfg.server, cfg.port)
    while True:
        # Always yield for a second, so no matter
        #  what we never CPU spin hard.
        time.sleep(1)

        # Next round of notifications
        # Request events starting at this serial number
        pak = conn.msg("notify", "get", {
            "gen": gen,
            "detail": cfg.detail,
            "nmsg": cfg.nmsg})
        pak.who = dest
        resp = conn.ping_pong(pak, tmo)

        # Failure
        if resp is None:


            time.sleep(pong.WAITNET)
            continue


        # Nothing happened
        if resp.inner["gen"] == gen:
            continue

        # New messages
        notify(resp)
        gen = resp.inner["gen"]

if __name__ == "__main__":
    load_cfg()

    # No arg, just be a service daemon
    if len(sys.argv) == 1:
        run()







>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
|
|
|
>
>
|

|
|
|
|
>
>
|

|
<
|
|
|
|

|
|
|
|
|
|
|
|

|
|
>
>
|
<
>

|
|
|

|
|
|







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
263
264

265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280

    # Watch DBus to turn LED's off when screen unlocks
    setup_dbus()

    # Prepare for notifications
    notify2.init("Messsaging")

    # Our notification server
    dest = (cfg.server, cfg.port)

    # We need a send-receive-send pattern as a minimum to become
    #  an assured/streaming UDP "connection".  This loop starts
    #  a new socket, does a param get/got, and then a timed
    #  get.  At the timeout, the server provides a "got", after
    #  which we send another "get".  This keeps the firewall/NAT
    #  state alive indefinitely, at a cost of one transmit and
    #  one receive every 2.5 minutes.
    # When we hit a network error (usually due to change in our
    #  IP address), we reset the connection and start over with
    #  the param get/got.
    while True:
        # Params; we in particular need to know the server's
        #  intended timeout.
        while True:
            pak = conn.pingpong(conn.msg("params", "get"))
            if pak is not None:
                break
            # Note that on error, the "pong" library will already
            #  have closed out the socket
            time.sleep(pong.WAITNET)

        # This is how long they'll hold a notify/get pending before
        #  sending back a null result.
        # If we request one and don't hear back in this amount of
        #  time, we have a lost packet or something.
        # 1.1 is our 10% slop factor over the expected timeout
        #  from the server.
        tmo = pak.inner["timeout"] * 1.1

        # Server loop

        while True:
            # Always yield for a second, so no matter
            #  what we never CPU spin hard.
            time.sleep(1)

            # Next round of notifications
            # Request events starting at this serial number
            pak = conn.msg("notify", "get", {
                "gen": gen,
                "detail": cfg.detail,
                "nmsg": cfg.nmsg})
            pak.who = dest
            resp = conn.ping_pong(pak, tmo)

            # Failure
            if resp is None:
                # We drop out of the server loop, and start over
                #  with a fresh socket and param get/got
                time.sleep(pong.WAITNET)

                break

            # Nothing happened
            if resp.inner["gen"] == gen:
                continue

            # New messages
            notify(resp)
            gen = resp.inner["gen"]

if __name__ == "__main__":
    load_cfg()

    # No arg, just be a service daemon
    if len(sys.argv) == 1:
        run()