1 /* Copyright (c) 2003-2005 MySQL AB
2    Use is subject to license terms
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; version 2 of the License.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software
15    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA */
16 
17 #ifndef Transporter_H
18 #define Transporter_H
19 
20 #include <ndb_global.h>
21 
22 #include <SocketClient.hpp>
23 
24 #include <TransporterRegistry.hpp>
25 #include <TransporterCallback.hpp>
26 #include "TransporterDefinitions.hpp"
27 #include "Packer.hpp"
28 
29 #include <NdbMutex.h>
30 #include <NdbThread.h>
31 
32 class Transporter {
33   friend class TransporterRegistry;
34 public:
35   virtual bool initTransporter() = 0;
36 
37   /**
38    * Destructor
39    */
40   virtual ~Transporter();
41 
42   /**
43    * None blocking
44    *    Use isConnected() to check status
45    */
46   bool connect_client();
47   bool connect_client(NDB_SOCKET_TYPE sockfd);
48   bool connect_server(NDB_SOCKET_TYPE socket);
49 
50   /**
51    * Blocking
52    */
53   virtual void doDisconnect();
54 
55   virtual Uint32 * getWritePtr(Uint32 lenBytes, Uint32 prio) = 0;
56   virtual void updateWritePtr(Uint32 lenBytes, Uint32 prio) = 0;
57 
58   /**
59    * Are we currently connected
60    */
61   bool isConnected() const;
62 
63   /**
64    * Remote Node Id
65    */
66   NodeId getRemoteNodeId() const;
67 
68   /**
69    * Local (own) Node Id
70    */
71   NodeId getLocalNodeId() const;
72 
73   /**
74    * Get port we're connecting to (signed)
75    */
get_s_port()76   int get_s_port() { return m_s_port; };
77 
78   /**
79    * Set port to connect to (signed)
80    */
set_s_port(int port)81   void set_s_port(int port) {
82     m_s_port = port;
83     if(port<0)
84       port= -port;
85     if(m_socket_client)
86       m_socket_client->set_port(port);
87   };
88 
89   virtual Uint32 get_free_buffer() const = 0;
90 
91 protected:
92   Transporter(TransporterRegistry &,
93 	      TransporterType,
94 	      const char *lHostName,
95 	      const char *rHostName,
96 	      int s_port,
97 	      bool isMgmConnection,
98 	      NodeId lNodeId,
99 	      NodeId rNodeId,
100 	      NodeId serverNodeId,
101 	      int byteorder,
102 	      bool compression,
103 	      bool checksum,
104 	      bool signalId);
105 
106   /**
107    * Blocking, for max timeOut milli seconds
108    *   Returns true if connect succeded
109    */
110   virtual bool connect_server_impl(NDB_SOCKET_TYPE sockfd) = 0;
111   virtual bool connect_client_impl(NDB_SOCKET_TYPE sockfd) = 0;
112 
113   /**
114    * Blocking
115    */
116   virtual void disconnectImpl() = 0;
117 
118   /**
119    * Remote host name/and address
120    */
121   char remoteHostName[256];
122   char localHostName[256];
123   struct in_addr remoteHostAddress;
124   struct in_addr localHostAddress;
125 
126   int m_s_port;
127 
128   const NodeId remoteNodeId;
129   const NodeId localNodeId;
130 
131   const bool isServer;
132 
133   unsigned createIndex;
134 
135   int byteOrder;
136   bool compressionUsed;
137   bool checksumUsed;
138   bool signalIdUsed;
139   Packer m_packer;
140 
141 private:
142 
143   /**
144    * means that we transform an MGM connection into
145    * a transporter connection
146    */
147   bool isMgmConnection;
148 
149   SocketClient *m_socket_client;
150   struct in_addr m_connect_address;
151 
152 protected:
153   Uint32 getErrorCount();
154   Uint32 m_errorCount;
155   Uint32 m_timeOutMillis;
156 
157 protected:
158   bool m_connected;     // Are we connected
159   TransporterType m_type;
160 
161   TransporterRegistry &m_transporter_registry;
get_callback_obj()162   void *get_callback_obj() { return m_transporter_registry.callbackObj; };
report_disconnect(int err)163   void report_disconnect(int err){m_transporter_registry.report_disconnect(remoteNodeId,err);};
report_error(enum TransporterError err,const char * info=0)164   void report_error(enum TransporterError err, const char *info = 0)
165     { reportError(get_callback_obj(), remoteNodeId, err, info); };
166 };
167 
168 inline
169 bool
isConnected() const170 Transporter::isConnected() const {
171   return m_connected;
172 }
173 
174 inline
175 NodeId
getRemoteNodeId() const176 Transporter::getRemoteNodeId() const {
177   return remoteNodeId;
178 }
179 
180 inline
181 NodeId
getLocalNodeId() const182 Transporter::getLocalNodeId() const {
183   return localNodeId;
184 }
185 
186 inline
187 Uint32
getErrorCount()188 Transporter::getErrorCount()
189 {
190   return m_errorCount;
191 }
192 
193 #endif // Define of Transporter_H
194