BowlerKernel
BowlerTCPClient.java
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright 2010 Neuron Robotics, LLC
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  ******************************************************************************/
15 package com.neuronrobotics.sdk.network;
16 
17 
18 import java.io.DataInputStream;
19 import java.io.DataOutputStream;
20 import java.io.IOException;
21 import java.net.InetAddress;
22 import java.net.Socket;
23 import java.net.SocketException;
24 import java.net.UnknownHostException;
25 import java.util.ArrayList;
26 
27 import com.neuronrobotics.sdk.common.BowlerAbstractConnection;
28 import com.neuronrobotics.sdk.common.Log;
29 import com.neuronrobotics.sdk.util.ThreadUtil;
30 
31 
32 
33 // TODO: Auto-generated Javadoc
38 
40  private int sleepTime = 5000;
41 
43  private int reconnectRetry = 5;
44 
46  private Socket tcpSock = null;
47 
49  private InetAddress tcpAddr=null;
50 
51 
53  private int port;
54 
55 
59  public BowlerTCPClient(){
61  }
62 
70  public BowlerTCPClient(String addr,int port) throws Exception{
71  this.port = port;
72  if(isConnected())
73  return;
75  Log.info("Bowler TCP connection on: "+addr+":"+port);
76  try {
77  InetAddress address = InetAddress.getByName(addr);
78  setTCPAddress(address);
79  Socket clientSocket =new Socket(address,port);
80 
81  setTCPSocket(clientSocket);
82  } catch (UnknownHostException e) {
83  Log.error("No such host");
84  throw e;
85  } catch (Exception e) {
86  Log.error("Port un-availible");
87  throw e;
88  }
89  }
90 
98  public BowlerTCPClient(InetAddress addr,int port) throws Exception {
99  this(addr.getHostName(),port);
100  }
101 
107  public void setTCPAddress(InetAddress addr){
108  tcpAddr=addr;
109  }
110 
117  public void setTCPPort(int port) throws IOException{
118  if(isConnected())
119  return;
120  if (tcpAddr==null&&(port>0)&&(port<0xffff))
121  throw new IOException("Connection Info Invalid: "+":"+port);
122  setTCPSocket(new Socket(tcpAddr, port));
123  }
124 
130  public void setTCPSocket(Socket sock){
131  if(isConnected())
132  return;
133  Log.info("Setting TCP socket");
134  while(!sock.isBound());
135  tcpSock = sock;
136  try {
137  tcpSock.setSoTimeout(1000);
138  } catch (SocketException e) {
139  // TODO Auto-generated catch block
140  e.printStackTrace();
141  }
142  connect();
143  }
144 
145  /* (non-Javadoc)
146  * @see com.neuronrobotics.sdk.common.BowlerAbstractConnection#connect()
147  */
148  @Override
149  public boolean connect() {
150  if (tcpSock == null)
151  throw new RuntimeException("Can't connect before setting up the socket information");
152  if(!isConnected()){
153  try {
154  setDataIns(new DataInputStream(tcpSock.getInputStream()));
155  setDataOuts(new DataOutputStream(tcpSock.getOutputStream()));
156  setConnected(true);
157  } catch (IOException e) {
158  e.printStackTrace();
159  setConnected(false);
160  }
161  }
162  return isConnected();
163  }
164 
165  /* (non-Javadoc)
166  * @see com.neuronrobotics.sdk.common.BowlerAbstractConnection#disconnect()
167  */
168  @Override
169  public void disconnect() {
170 
171  Log.warning("Disconnecting Tcp Client..");
172  super.disconnect();
173  try {
174  if(!tcpSock.isClosed()){
175  tcpSock.shutdownOutput(); // Sends the 'FIN' on the network
176  while (getDataIns().read() >= 0) ; // "read()" returns '-1' when the 'FIN' is reached
177  tcpSock.close(); // Now we can close the Socket
178  }
179  } catch (Exception e) {
180  e.printStackTrace();
181  }
182  tcpSock = null;
183  setDataIns(null);
184  setDataOuts(null);
185 
186  }
187 
194  public static ArrayList<InetAddress> getAvailableSockets() {
195  ArrayList<InetAddress> available = new ArrayList<InetAddress> ();
197  try {
198  udp = new UDPBowlerConnection();
199  available= udp.getAllAddresses();
200  udp.disconnect();
201  } catch (Exception e) {
202  // TODO Auto-generated catch block
203  e.printStackTrace();
204  }
205  return available;
206  }
207 
208 
209 
210  /* (non-Javadoc)
211  * @see com.neuronrobotics.sdk.common.BowlerAbstractConnection#reconnect()
212  */
218  //@Override
219  public boolean reconnect() {
220  Log.warning("Reconnecting TCP Socket..");
221  disconnect();
223  for(int i=0;i<getReconnectRetry();i++){
224  try {
225  setTCPSocket(new Socket(tcpAddr,port));
226  connect();
227  if(isConnected())
228  return true;
229 
230  } catch (IOException e) {
231  // TODO Auto-generated catch block
232  e.printStackTrace();
233  }
234  disconnect();
235  ThreadUtil.wait(i*10*getSleepTime());
236  Log.error("Reconnect failed, retry: "+i);
237  }
238  return false;
239  }
240 
241  /* (non-Javadoc)
242  * @see com.neuronrobotics.sdk.common.BowlerAbstractConnection#waitingForConnection()
243  */
244  @Override
245  public boolean waitingForConnection() {
246  // TODO Auto-generated method stub
247  return false;
248  }
249 
255  public int getReconnectRetry() {
256  return reconnectRetry;
257  }
258 
265  this.reconnectRetry = reconnectRetry;
266  }
267 
268 }
static void info(String message)
Definition: Log.java:110
static void error(String message)
Definition: Log.java:92
static void warning(String message)
Definition: Log.java:101
static ArrayList< InetAddress > getAvailableSockets()