-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipe_client_ravelled
More file actions
executable file
·114 lines (104 loc) · 3.11 KB
/
Copy pathpipe_client_ravelled
File metadata and controls
executable file
·114 lines (104 loc) · 3.11 KB
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
#!/usr/bin/python3
#+
# DBussy example -- demonstrate how to pass file descriptors
# over D-Bus. This is the client side, which sends requests
# to the server to create either a transmit or receive pipe
# matching a given key value. Two clients specifying the
# same key can then exchange messages over the pipe.
#
# Invoke this script as either
#
# pipe_client_ravelled «path» write «message»
# or
# pipe_client_ravelled «path» read
#
# The “write” form will open the write end of the pipe identified
# by «path», which must be a valid D-Bus object path, and write
# the given «message» text to it. The “read” form will open the read
# end of the pipe identified by «path», and read and display a
# message written to it.
#
# Copyright 2018 by Lawrence D'Oliveiro <ldo@geek-central.gen.nz>. This
# script is licensed CC0
# <https://creativecommons.org/publicdomain/zero/1.0/>; do with it
# what you will.
#-
import sys
import os
import getopt
import dbussy as dbus
from dbussy import \
DBUS, \
Introspection
import ravel
my_bus_name = "com.example.pipes"
my_iface_name = "com.example.pipes"
loop = dbus.get_event_loop()
#+
# Server proxy interface
#-
Server = ravel.def_proxy_interface \
(
ravel.INTERFACE.CLIENT,
name = "Server",
introspected =
Introspection.Interface
(
name = my_iface_name,
methods =
(
Introspection.Interface.Method
(
name = "get",
args =
(
Introspection.Interface.Method.Arg
(
name = "writing",
type = chr(DBUS.TYPE_BOOLEAN),
direction = Introspection.DIRECTION.IN,
),
Introspection.Interface.Method.Arg
(
type = chr(DBUS.TYPE_UNIX_FD),
direction = Introspection.DIRECTION.OUT,
),
),
),
)
),
is_async = True
)
#+
# Mainline
#-
if len(sys.argv) not in (3, 4) :
raise getopt.GetoptError("usage: %s «path» read|write [«message»]" % sys.argv[0])
#end if
path = sys.argv[1]
writing = {"read" : False, "write" : True}[sys.argv[2]]
if len(sys.argv) != 3 + writing :
raise getopt.GetoptError("message arg required for write, not read")
#end if
if writing :
message = sys.argv[3]
#end if
bus = ravel.session_bus()
bus.attach_asyncio(loop)
server = Server \
(
connection = bus.connection,
dest = my_bus_name
)
async def mainline() :
global message
pipe, = await server[path].get(writing = writing)
if writing :
os.write(pipe, message.encode())
else :
message = os.read(pipe, 80)
sys.stdout.write("message: %s\n" % repr(message))
#end if
os.close(pipe)
#end mainline
loop.run_until_complete(mainline())