summaryrefslogtreecommitdiff
path: root/ace/Name_Proxy.cpp
blob: 02dc399a219a56c799a5623a1625fca9b86fe29c (plain)
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
// Name_Proxy.cpp
// $Id$

#define ACE_BUILD_DLL
#include "ace/Name_Proxy.h"

void
ACE_Name_Proxy::dump (void) const
{
  ACE_TRACE ("ACE_Name_Proxy::dump");

  ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
  this->connector_.dump ();
  this->peer_.dump ();
  ACE_DEBUG ((LM_DEBUG, "reactor_ = %x", this->reactor_));
  ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
}

// Default constructor.

ACE_Name_Proxy::ACE_Name_Proxy (void)
  : reactor_ (0)
{
  ACE_TRACE ("ACE_Name_Proxy::ACE_Name_Proxy");
}

// Establish binding with the ACE_Name Server at remote_addr.

int
ACE_Name_Proxy::open (const ACE_INET_Addr &remote_addr,
		      ACE_Synch_Options& options)
{
  ACE_TRACE ("ACE_Name_Proxy::open");
  ACE_Time_Value *timeout = 0;

  if (options[ACE_Synch_Options::USE_TIMEOUT])
    timeout = (ACE_Time_Value *) &ACE_Time_Value::zero;
  else 
    timeout = (ACE_Time_Value *) options.time_value ();

  // Initiate the connection.
  return this->connector_.connect (this->peer_, remote_addr, timeout);
}

// Establish binding with the ACE_Name Server at remote_addr.
 
ACE_Name_Proxy::ACE_Name_Proxy (const ACE_INET_Addr &remote_addr,
				ACE_Synch_Options& options)
{
  ACE_TRACE ("ACE_Name_Proxy::ACE_Name_Proxy");
  if (this->open (remote_addr, options) == -1
      && options[ACE_Synch_Options::USE_TIMEOUT] && errno != EWOULDBLOCK)
    ACE_ERROR ((LM_ERROR, "%p\n", "ACE_Name_Proxy::ACE_Name_Proxy"));
}
 
// Obtain underlying handle.
 
/* VIRTUAL */ ACE_HANDLE
ACE_Name_Proxy::get_handle (void) const
{
  ACE_TRACE ("ACE_Name_Proxy::get_handle");
  return this->peer_.get_handle ();
}
 
int
ACE_Name_Proxy::request_reply (ACE_Name_Request &request)
{
  ACE_TRACE ("ACE_Name_Proxy::request_reply");
  void    *buffer;
  ssize_t length;
 
  if ((length = request.encode (buffer)) == -1)
    ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "encode failed"), -1);
 
  // Transmit request via a blocking send.
 
  if (this->peer_.send_n (buffer, length) != length)
    ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "send_n failed"), -1);
  else
    {
      ACE_Name_Reply reply;
 
      // Receive reply via blocking read.
 
      if (this->peer_.recv (&reply, sizeof reply) == -1)
        ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "recv failed"), -1);
 
      else if (reply.decode () == -1)
        ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "decode failed"), -1);
 
      errno = int (reply.errnum ());
      return reply.status ();
    }
}

int
ACE_Name_Proxy::send_request (ACE_Name_Request &request)
{
  ACE_TRACE ("ACE_Name_Proxy::send_request");
  void *buffer;
  ssize_t length;
 
  if ((length = request.encode (buffer)) == -1)
    ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "encode failed"), -1);
 
  // Transmit request via a blocking send.
 
  else if (this->peer_.send_n (buffer, length) != length)
    ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "send_n failed"), -1);

  return 0;
}

int
ACE_Name_Proxy::recv_reply (ACE_Name_Request &reply)
{
  ACE_TRACE ("ACE_Name_Proxy::recv_reply");
  // Read the first 4 bytes to get the length of the message
  // This implementation assumes that the first 4 bytes are
  // the length of the message.
  ssize_t n = this->peer_.recv ((void *) &reply, sizeof (ACE_UINT32));

  switch (n)
    {
    case -1:
      // FALLTHROUGH 
      ACE_DEBUG ((LM_DEBUG, "****************** recv_reply returned -1\n"));
    default:
      ACE_ERROR ((LM_ERROR, "%p got %d bytes, expected %d bytes\n",
		  "recv failed", n, sizeof (ACE_UINT32)));
      // FALLTHROUGH 
    case 0:
      // We've shutdown unexpectedly
      return -1;
      // NOTREACHED 
    case sizeof (ACE_UINT32):
      {  
        // Transform the length into host byte order.
        ssize_t length = ntohl (reply.length ());

        // Receive the rest of the request message.
        // @@ beware of blocking read!!!.
        n = this->peer_.recv ((void *) (((char *) &reply)
                                        + sizeof (ACE_UINT32)), 
			      length - sizeof (ACE_UINT32));

        // Subtract off the size of the part we skipped over...
        if (n != ssize_t (length - sizeof (ACE_UINT32)))
          {
            ACE_ERROR ((LM_ERROR, "%p expected %d, got %d\n",
			"invalid length", length, n));
            return -1;
          }

        // Decode the request into host byte order.
        if (reply.decode () == -1)
          {
            ACE_ERROR ((LM_ERROR, "%p\n", "decode failed"));
            return -1;
          }
      }  
    }
  return 0;
}

// Close down the connection to the server.
 
ACE_Name_Proxy::~ACE_Name_Proxy (void)
{
  ACE_TRACE ("ACE_Name_Proxy::~ACE_Name_Proxy");
  this->peer_.close ();
}