BowlerKernel
UDPBowlerConnection.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 import java.io.DataInputStream;
18 import java.io.DataOutputStream;
19 import java.io.IOException;
20 import java.net.DatagramPacket;
21 import java.net.DatagramSocket;
22 import java.net.InetAddress;
23 import java.net.SocketException;
24 import java.net.SocketTimeoutException;
25 import java.net.UnknownHostException;
26 import java.util.ArrayList;
27 
28 import com.neuronrobotics.sdk.commands.bcs.core.PingCommand;
29 import com.neuronrobotics.sdk.common.BowlerAbstractConnection;
30 import com.neuronrobotics.sdk.common.BowlerDatagram;
31 import com.neuronrobotics.sdk.common.BowlerDatagramFactory;
32 import com.neuronrobotics.sdk.common.ByteList;
33 import com.neuronrobotics.sdk.common.Log;
34 import com.neuronrobotics.sdk.common.MACAddress;
35 
36 // TODO: Auto-generated Javadoc
41 
43  private int sleepTime = 5000;
44 
46  private int port = 1865;
47 
48 
50  private InetAddress IPAddressSet=null;
51 
53  private ArrayList<InetAddress> addrs=null;
54 
56  //private ByteList internalReceiveBuffer= new ByteList();
57  private DatagramSocket udpSock = null;
58 
63  init();
64  }
65 
71  public UDPBowlerConnection(InetAddress set){
72  init();
73  setAddress(set);
74  }
75 
82  public UDPBowlerConnection(InetAddress set,int port){
83  this.port=port;
84  init();
85  setAddress(set);
86  }
87 
93  public void setAddress(InetAddress set){
94  IPAddressSet=set;
95  }
96 
103  this.port=port;
104  init();
105  }
106 
107 
114  @Override
115  public DataInputStream getDataIns() throws NullPointerException{
116  new RuntimeException("This method should not be called").printStackTrace();
117  while(true);
118  }
119 
126  @Override
127  public DataOutputStream getDataOuts() throws NullPointerException{
128  new RuntimeException("This method should not be called").printStackTrace();
129  while(true);
130  }
131 
138  //private ByteList outgoing = new ByteList();
139  public void write(byte[] data) throws IOException {
140  //waitForConnectioToBeReady();
141  setLastWrite(System.currentTimeMillis());
142 
143  DatagramPacket sendPacket = new DatagramPacket(data, data.length, IPAddressSet, port);
144  //Log.info("Sending UDP packet: "+sendPacket);
145  udpSock.send(sendPacket);
146 
147  }
148 
150  byte[] receiveData=new byte[4096];
151 
153  DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
154 
155  /* (non-Javadoc)
156  * @see com.neuronrobotics.sdk.common.BowlerAbstractConnection#loadPacketFromPhy(com.neuronrobotics.sdk.common.ByteList)
157  */
158  @Override
159  public BowlerDatagram loadPacketFromPhy(ByteList bytesToPacketBuffer) throws NullPointerException, IOException{
160 
161  long start = System.currentTimeMillis();
162  //Log.info("Waiting for UDP packet");
163  udpSock.setSoTimeout(1);// Timeout the socket after 1 ms
164  //System.err.println("Timeout set "+(System.currentTimeMillis()-start));
165  start = System.currentTimeMillis();
166  try{
167  udpSock.receive(receivePacket);
168 
169  }catch(SocketTimeoutException ste){
170  return null;
171  }catch(Exception ex){
172  // disconnect called
173  //Log. warning("Receive bailed out because of close");
174  ex.printStackTrace();
175  return null;
176  }
177  //System.err.println("Recv "+(System.currentTimeMillis()-start));
178  start = System.currentTimeMillis();
179  Log.info("Got UDP packet");
180  if(addrs== null)
181  addrs=new ArrayList<InetAddress>();
182  getAllAddresses().add(receivePacket.getAddress());
183 
184  byte [] data = receivePacket.getData();
185 
186  for (int i=0;i<receivePacket.getLength();i++){
187  bytesToPacketBuffer.add(data[i]);
188  }
189  //System.err.println("copy "+(System.currentTimeMillis()-start));
190  start = System.currentTimeMillis();
191  BowlerDatagram bd= BowlerDatagramFactory.build(bytesToPacketBuffer);
192  //System.err.println("build "+(System.currentTimeMillis()-start));
193  return bd;
194  }
195 
196 
197 
198 
202  private void init(){
204  setChunkSize(5210);
205  try {
206  if(IPAddressSet == null)
207  IPAddressSet=InetAddress.getByAddress(new byte[]{(byte) 255,(byte) 255,(byte) 255,(byte) 255});
208  } catch (UnknownHostException e1) {
209  // TODO Auto-generated catch block
210  e1.printStackTrace();
211  }
212  if(connect()){
213 
214  }else{
215  Log.error("Connection failed");
216  throw new RuntimeException("UDP Connection failed");
217  }
218  }
219 
220  /* (non-Javadoc)
221  * @see com.neuronrobotics.sdk.common.BowlerAbstractConnection#disconnect()
222  */
223  public void disconnect(){
224  if(udpSock!=null){
225  udpSock.close();
226  udpSock=null;
227  }
228  setConnected(false);
229  }
230 
231  /* (non-Javadoc)
232  * @see com.neuronrobotics.sdk.common.BowlerAbstractConnection#connect()
233  */
234  @Override
235  public boolean connect() {
236  if(isConnected()){
237  Log.info("already connected..");
238  return true;
239  }
240  setConnected(false);
241  try {
242  udpSock = new DatagramSocket();
243 
244  setConnected(true);
245  } catch (Exception e) {
246  // TODO Auto-generated catch block
247  e.printStackTrace();
248  setConnected(false);
249  }
250  return isConnected();
251  }
252 
253  /* (non-Javadoc)
254  * @see com.neuronrobotics.sdk.common.BowlerAbstractConnection#reconnect()
255  */
262  //@Override
263  public boolean reconnect() throws IOException {
264  disconnect();
265  connect();
266  return true;
267  }
268 
269  /* (non-Javadoc)
270  * @see com.neuronrobotics.sdk.common.BowlerAbstractConnection#waitingForConnection()
271  */
272  @Override
273  public boolean waitingForConnection() {
274  // TODO Auto-generated method stub
275  return false;
276  }
277 
278 
279 
285  public ArrayList<InetAddress> getAllAddresses(){
286  if(addrs== null){
287  addrs=new ArrayList<InetAddress>();
288  try {
289 
290  //Generate a ping command
292  ping.setUpstream(false);
293  Log.info("Sending synchronization ping: \n"+ping);
294  //send it to the UDP socket
295  write(ping.getBytes());
296  //wait for all devices to report back
297  try {Thread.sleep(3000);} catch (InterruptedException e) {}
298  } catch (IOException e) {
299  // TODO Auto-generated catch block
300  e.printStackTrace();
301  }
302  }
303  return addrs;
304  }
305 
311  public void setAddress(String address) {
312  for (InetAddress in: getAllAddresses()) {
313  if(in.getHostAddress().contains(address)) {
314  setAddress(in);
315  return;
316  }
317 
318  }
319  throw new RuntimeException("Unknown address");
320  }
321 }
static void info(String message)
Definition: Log.java:110
static void error(String message)
Definition: Log.java:92
BowlerDatagram loadPacketFromPhy(ByteList bytesToPacketBuffer)