libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
address.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
3 
4  This file is part of libzmq, the ZeroMQ core engine in C++.
5 
6  libzmq is free software; you can redistribute it and/or modify it under
7  the terms of the GNU Lesser General Public License (LGPL) as published
8  by the Free Software Foundation; either version 3 of the License, or
9  (at your option) any later version.
10 
11  As a special exception, the Contributors give you permission to link
12  this library with independent modules to produce an executable,
13  regardless of the license terms of these independent modules, and to
14  copy and distribute the resulting executable under terms of your choice,
15  provided that you also meet, for each linked independent module, the
16  terms and conditions of the license of that module. An independent
17  module is a module which is not derived from or based on this library.
18  If you modify this library, you must extend this exception to your
19  version of the library.
20 
21  libzmq is distributed in the hope that it will be useful, but WITHOUT
22  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24  License for more details.
25 
26  You should have received a copy of the GNU Lesser General Public License
27  along with this program. If not, see <http://www.gnu.org/licenses/>.
28 */
29 
30 #include "precompiled.hpp"
31 #include "macros.hpp"
32 #include "platform.hpp"
33 #include "address.hpp"
34 #include "ctx.hpp"
35 #include "err.hpp"
36 #include "tcp_address.hpp"
37 #include "udp_address.hpp"
38 #include "ipc_address.hpp"
39 #include "tipc_address.hpp"
40 
41 #if defined ZMQ_HAVE_VMCI
42 #include "vmci_address.hpp"
43 #endif
44 
45 #include <string>
46 #include <sstream>
47 
49  const std::string &protocol_, const std::string &address_, ctx_t *parent_)
50  : protocol (protocol_),
51  address (address_),
52  parent (parent_)
53 {
54  memset (&resolved, 0, sizeof resolved);
55 }
56 
58 {
59  if (protocol == "tcp") {
60  if (resolved.tcp_addr) {
61  LIBZMQ_DELETE(resolved.tcp_addr);
62  }
63  }
64  if (protocol == "udp") {
65  if (resolved.udp_addr) {
66  LIBZMQ_DELETE(resolved.udp_addr);
67  }
68  }
69 #if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
70  else
71  if (protocol == "ipc") {
72  if (resolved.ipc_addr) {
73  LIBZMQ_DELETE(resolved.ipc_addr);
74  }
75  }
76 #endif
77 #if defined ZMQ_HAVE_TIPC
78  else
79  if (protocol == "tipc") {
80  if (resolved.tipc_addr) {
81  LIBZMQ_DELETE(resolved.tipc_addr);
82  }
83  }
84 #endif
85 #if defined ZMQ_HAVE_VMCI
86  else
87  if (protocol == "vmci") {
88  if (resolved.vmci_addr) {
89  LIBZMQ_DELETE(resolved.vmci_addr);
90  }
91  }
92 #endif
93 }
94 
95 int zmq::address_t::to_string (std::string &addr_) const
96 {
97  if (protocol == "tcp") {
98  if (resolved.tcp_addr)
99  return resolved.tcp_addr->to_string (addr_);
100  }
101  if (protocol == "udp") {
102  if (resolved.udp_addr)
103  return resolved.udp_addr->to_string (addr_);
104  }
105 #if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
106  else
107  if (protocol == "ipc") {
108  if (resolved.ipc_addr)
109  return resolved.ipc_addr->to_string (addr_);
110  }
111 #endif
112 #if defined ZMQ_HAVE_TIPC
113  else
114  if (protocol == "tipc") {
115  if (resolved.tipc_addr)
116  return resolved.tipc_addr->to_string (addr_);
117  }
118 #endif
119 #if defined ZMQ_HAVE_VMCI
120  else
121  if (protocol == "vmci") {
122  if (resolved.vmci_addr)
123  return resolved.vmci_addr->to_string (addr_);
124  }
125 #endif
126 
127  if (!protocol.empty () && !address.empty ()) {
128  std::stringstream s;
129  s << protocol << "://" << address;
130  addr_ = s.str ();
131  return 0;
132  }
133  addr_.clear ();
134  return -1;
135 }
#define LIBZMQ_DELETE(p_object)
Definition: macros.hpp:7
const std::string address
Definition: address.hpp:55
address_t(const std::string &protocol_, const std::string &address_, ctx_t *parent_)
Definition: address.cpp:48
const std::string protocol
Definition: address.hpp:54
const char * address
Definition: test_fork.cpp:32
union zmq::address_t::@0 resolved
int to_string(std::string &addr_) const
Definition: address.cpp:95