File: Zmsg.py

package info (click to toggle)
czmq 4.2.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,912 kB
  • sloc: ansic: 30,811; cpp: 19,362; sh: 11,873; python: 11,814; pascal: 11,229; ruby: 8,818; java: 4,363; makefile: 299; perl: 151; javascript: 35
file content (300 lines) | stat: -rw-r--r-- 10,843 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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
################################################################################
#  THIS FILE IS 100% GENERATED BY ZPROJECT; DO NOT EDIT EXCEPT EXPERIMENTALLY  #
#  Read the zproject/README.md for information about making permanent changes. #
################################################################################
from . import utils
from . import destructors
libczmq_destructors = destructors.lib

class Zmsg(object):
    """
    working with multipart messages
    """

    def __init__(self):
        """
        Create a new empty message object
        """
        p = utils.lib.zmsg_new()
        if p == utils.ffi.NULL:
            raise MemoryError("Could not allocate person")

        # ffi.gc returns a copy of the cdata object which will have the
        # destructor called when the Python object is GC'd:
        # https://cffi.readthedocs.org/en/latest/using.html#ffi-interface
        self._p = utils.ffi.gc(p, libczmq_destructors.zmsg_destroy_py)

    @staticmethod
    def recv(source):
        """
        Receive message from socket, returns zmsg_t object or NULL if the recv
        was interrupted. Does a blocking recv. If you want to not block then use
        the zloop class or zmsg_recv_nowait or zmq_poll to check for socket input
        before receiving.
        """
        return utils.lib.zmsg_recv(source._p)

    @staticmethod
    def load(file):
        """
        Load/append an open file into new message, return the message.
        Returns NULL if the message could not be loaded.
        """
        return utils.lib.zmsg_load(file)

    @staticmethod
    def decode(frame):
        """
        Decodes a serialized message frame created by zmsg_encode () and returns
        a new zmsg_t object. Returns NULL if the frame was badly formatted or
        there was insufficient memory to work.
        """
        return utils.lib.zmsg_decode(frame._p)

    @staticmethod
    def new_signal(status):
        """
        Generate a signal message encoding the given status. A signal is a short
        message carrying a 1-byte success/failure code (by convention, 0 means
        OK). Signals are encoded to be distinguishable from "normal" messages.
        """
        return utils.lib.zmsg_new_signal(status)

    @staticmethod
    def send(self_p, dest):
        """
        Send message to destination socket, and destroy the message after sending
        it successfully. If the message has no frames, sends nothing but destroys
        the message anyhow. Nullifies the caller's reference to the message (as
        it is a destructor).
        """
        return utils.lib.zmsg_send(utils.ffi.new("zmsg_t **", self_p._p), dest._p)

    @staticmethod
    def sendm(self_p, dest):
        """
        Send message to destination socket as part of a multipart sequence, and
        destroy the message after sending it successfully. Note that after a
        zmsg_sendm, you must call zmsg_send or another method that sends a final
        message part. If the message has no frames, sends nothing but destroys
        the message anyhow. Nullifies the caller's reference to the message (as
        it is a destructor).
        """
        return utils.lib.zmsg_sendm(utils.ffi.new("zmsg_t **", self_p._p), dest._p)

    def size(self):
        """
        Return size of message, i.e. number of frames (0 or more).
        """
        return utils.lib.zmsg_size(self._p)

    def content_size(self):
        """
        Return total size of all frames in message.
        """
        return utils.lib.zmsg_content_size(self._p)

    def routing_id(self):
        """
        Return message routing ID, if the message came from a ZMQ_SERVER socket.
        Else returns zero.
        """
        return utils.lib.zmsg_routing_id(self._p)

    def set_routing_id(self, routing_id):
        """
        Set routing ID on message. This is used if/when the message is sent to a
        ZMQ_SERVER socket.
        """
        utils.lib.zmsg_set_routing_id(self._p, routing_id)

    def prepend(self, frame_p):
        """
        Push frame to the front of the message, i.e. before all other frames.
        Message takes ownership of frame, will destroy it when message is sent.
        Returns 0 on success, -1 on error. Deprecates zmsg_push, which did not
        nullify the caller's frame reference.
        """
        return utils.lib.zmsg_prepend(self._p, utils.ffi.new("zframe_t **", frame_p._p))

    def append(self, frame_p):
        """
        Add frame to the end of the message, i.e. after all other frames.
        Message takes ownership of frame, will destroy it when message is sent.
        Returns 0 on success. Deprecates zmsg_add, which did not nullify the
        caller's frame reference.
        """
        return utils.lib.zmsg_append(self._p, utils.ffi.new("zframe_t **", frame_p._p))

    def pop(self):
        """
        Remove first frame from message, if any. Returns frame, or NULL.
        """
        return utils.lib.zmsg_pop(self._p)

    def pushmem(self, data, size):
        """
        Push block of memory to front of message, as a new frame.
        Returns 0 on success, -1 on error.
        """
        return utils.lib.zmsg_pushmem(self._p, data, size)

    def addmem(self, data, size):
        """
        Add block of memory to the end of the message, as a new frame.
        Returns 0 on success, -1 on error.
        """
        return utils.lib.zmsg_addmem(self._p, data, size)

    def pushstr(self, string):
        """
        Push string as new frame to front of message.
        Returns 0 on success, -1 on error.
        """
        return utils.lib.zmsg_pushstr(self._p, utils.to_bytes(string))

    def addstr(self, string):
        """
        Push string as new frame to end of message.
        Returns 0 on success, -1 on error.
        """
        return utils.lib.zmsg_addstr(self._p, utils.to_bytes(string))

    def pushstrf(self, format, *format_args):
        """
        Push formatted string as new frame to front of message.
        Returns 0 on success, -1 on error.
        """
        return utils.lib.zmsg_pushstrf(self._p, format, *format_args)

    def addstrf(self, format, *format_args):
        """
        Push formatted string as new frame to end of message.
        Returns 0 on success, -1 on error.
        """
        return utils.lib.zmsg_addstrf(self._p, format, *format_args)

    def popstr(self):
        """
        Pop frame off front of message, return as fresh string. If there were
        no more frames in the message, returns NULL.
        """
        return utils.lib.zmsg_popstr(self._p)

    def addmsg(self, msg_p):
        """
        Push encoded message as a new frame. Message takes ownership of
        submessage, so the original is destroyed in this call. Returns 0 on
        success, -1 on error.
        """
        return utils.lib.zmsg_addmsg(self._p, utils.ffi.new("zmsg_t **", msg_p._p))

    def popmsg(self):
        """
        Remove first submessage from message, if any. Returns zmsg_t, or NULL if
        decoding was not successful.
        """
        return utils.lib.zmsg_popmsg(self._p)

    def remove(self, frame):
        """
        Remove specified frame from list, if present. Does not destroy frame.
        """
        utils.lib.zmsg_remove(self._p, frame._p)

    def first(self):
        """
        Set cursor to first frame in message. Returns frame, or NULL, if the
        message is empty. Use this to navigate the frames as a list.
        """
        return utils.lib.zmsg_first(self._p)

    def next(self):
        """
        Return the next frame. If there are no more frames, returns NULL. To move
        to the first frame call zmsg_first(). Advances the cursor.
        """
        return utils.lib.zmsg_next(self._p)

    def last(self):
        """
        Return the last frame. If there are no frames, returns NULL.
        """
        return utils.lib.zmsg_last(self._p)

    def save(self, file):
        """
        Save message to an open file, return 0 if OK, else -1. The message is
        saved as a series of frames, each with length and data. Note that the
        file is NOT guaranteed to be portable between operating systems, not
        versions of CZMQ. The file format is at present undocumented and liable
        to arbitrary change.
        """
        return utils.lib.zmsg_save(self._p, file)

    def encode(self):
        """
        Serialize multipart message to a single message frame. Use this method
        to send structured messages across transports that do not support
        multipart data. Allocates and returns a new frame containing the
        serialized message. To decode a serialized message frame, use
        zmsg_decode ().
        """
        return utils.lib.zmsg_encode(self._p)

    def dup(self):
        """
        Create copy of message, as new message object. Returns a fresh zmsg_t
        object. If message is null, or memory was exhausted, returns null.
        """
        return utils.lib.zmsg_dup(self._p)

    def print_py(self):
        """
        Send message to zsys log sink (may be stdout, or system facility as
        configured by zsys_set_logstream).
        Long messages are truncated.
        """
        utils.lib.zmsg_print(self._p)

    def print_n(self, size):
        """
        Send message to zsys log sink (may be stdout, or system facility as
        configured by zsys_set_logstream).
        Message length is specified; no truncation unless length is zero.
        Backwards compatible with zframe_print when length is zero.
        """
        utils.lib.zmsg_print_n(self._p, size)

    def eq(self, other):
        """
        Return true if the two messages have the same number of frames and each
        frame in the first message is identical to the corresponding frame in the
        other message. As with zframe_eq, return false if either message is NULL.
        """
        return utils.lib.zmsg_eq(self._p, other._p)

    def signal(self):
        """
        Return signal value, 0 or greater, if message is a signal, -1 if not.
        """
        return utils.lib.zmsg_signal(self._p)

    @staticmethod
    def is_py(self):
        """
        Probe the supplied object, and report if it looks like a zmsg_t.
        """
        return utils.lib.zmsg_is(self._p)

    @staticmethod
    def test(verbose):
        """
        Self test of this class.
        """
        utils.lib.zmsg_test(verbose)

################################################################################
#  THIS FILE IS 100% GENERATED BY ZPROJECT; DO NOT EDIT EXCEPT EXPERIMENTALLY  #
#  Read the zproject/README.md for information about making permanent changes. #
################################################################################