Turi Create  4.0
publish_socket.hpp
1 /* Copyright © 2017 Apple Inc. All rights reserved.
2  *
3  * Use of this source code is governed by a BSD-3-clause license that can
4  * be found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
5  */
6 #ifndef FAULT_SOCKETS_PUBLISH_SOCKET_HPP
7 #define FAULT_SOCKETS_PUBLISH_SOCKET_HPP
8 #include <string>
9 #include <vector>
10 #include <set>
11 #include <core/system/nanosockets/zmq_msg_vector.hpp>
12 #include <core/parallel/mutex.hpp>
13 #include <core/export.hpp>
14 
15 namespace turi {
16 namespace nanosockets {
17 
18 /**
19  * \ingroup nanosockets
20  *
21  * Constructs a Nanomsg publish socket.
22  *
23  * The publish socket is bound to a zeromq style endpoint address
24  * Endpoints are standard Zeromq style endpoint addresses , for instance,
25  * tcp://[ip]:[port], or ipc://[filename] (interprocess socket) or
26  * inproc://[handlename] (inprocess socket).
27  *
28  * Subscribe sockets (\ref subscribe_socket) can then attach to the endpoint
29  * and listen for published messages. Note that publish-subscribe is not
30  * necessarily reliable; i.e. subscribers may not receive all published data.
31  *
32  * \code
33  * publish_socket pubsock("ipc:///tmp/publish");
34  * pubsock.send("hello world")
35  * \endcode
36  */
37 class EXPORT publish_socket {
38  public:
39  /**
40  * Constructs a publish socket.
41  * The request will be sent to the current owners of the key
42  *
43  * \param bind_address this will be address to bind to.
44  */
45  publish_socket(std::string bind_address);
46 
47  /**
48  * Closes this socket. Once closed, the socket cannot be used again.
49  */
50  void close();
51 
52 
53  /**
54  * Sends a message. All subscribers which match the message (by prefix)
55  * will receive a copy. This function is not safe to call in parallel.
56  */
57  void send(const std::string& message);
58 
59  ~publish_socket();
60 
61  /**
62  * Returns the address the socket is bound to
63  */
64  std::string get_bound_address();
65 
66  private:
67  int z_socket;
68  mutex z_mutex;
69  std::string local_address;
70 };
71 
72 
73 } // namespace fault
74 }
75 #endif