File: _1.py

package info (click to toggle)
idjc 0.9.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,796 kB
  • sloc: python: 21,706; ansic: 16,528; sh: 5,639; makefile: 206; sed: 16
file content (85 lines) | stat: -rw-r--r-- 2,469 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
73
74
75
76
77
78
79
80
81
82
83
84
85
#   Copyright (C) 2022 Stephen Fairchild (s-fairchild@users.sourceforge.net)
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program in the file entitled COPYING.
#   If not, see <http://www.gnu.org/licenses/>.

__all__ = ["main"]

_description_ = "Launch backend and keep pinging it."

import ctypes
import os
import time
from datetime import datetime

from idjc import FGlobs, PGlobs


def main():
    print(_description_)

    backend = ctypes.CDLL(FGlobs.backend)

    read = ctypes.c_int()
    write = ctypes.c_int()
    if not backend.init_backend(ctypes.byref(read), ctypes.byref(write)):
        print("call to init_backend failed")
        return 5

    print(f"file descriptors for read/write are {read.value}, {write.value}")

    try:
        backend_write = os.fdopen(write.value, "w")
        backend_read = os.fdopen(read.value, "r")
    except OSError:
        "failed to open streams to backend"
        return 5

    print("awaiting reply")

    try:
        line = backend_read.readline()
    except IOError as e:
        print(e)
        return 5

    if line != "idjc backend ready\n":
        print(f"bad reply from backend: {line}")
        return 5

    print(f"backend replies with: {line}")

    def ping():
        print("sending ping")
        try:
            backend_write.write("mx\nACTN=ping\nend\n")
            backend_write.flush()
            reply = backend_read.readline().rstrip() or "(none)"
            print(f"reply: {reply}")
            if reply != "pong":
                raise ValueError
        except IOError as e:
            print(f"unexpected error: {e}")
            reply = str(e)
        except ValueError:
            print(f"normally we expect pong reply: got {reply} instead")

        return reply

    start_time = datetime.now()

    while ping() == "pong":
        print(f"time elapsed = {datetime.now() - start_time}")
        time.sleep(2)

    return 0