BowlerKernel
BowlerUDPServer.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.DatagramPacket;
22 import java.net.DatagramSocket;
23 import java.net.InetAddress;
24 import java.net.SocketException;
25 
26 import com.neuronrobotics.sdk.common.BowlerAbstractConnection;
27 import com.neuronrobotics.sdk.common.BowlerDatagram;
28 import com.neuronrobotics.sdk.common.BowlerDatagramFactory;
29 import com.neuronrobotics.sdk.common.ByteList;
30 import com.neuronrobotics.sdk.common.Log;
31 
32 
33 // TODO: Auto-generated Javadoc
38 
40  private int sleepTime = 1000;
41 
43  private InetAddress IPAddressSet=null;
44 
47 
49  private DatagramSocket udpSock = null;
50 
51  //private UDPStream udp = null;
52 
54  private int port = 1865;
55 
57  private int destinationPort=port;
58 
62  public BowlerUDPServer(){
64  setChunkSize(5210);
65  }
66 
72  public BowlerUDPServer(int port){
74  setChunkSize(5210);
75  this.port=port;
76  }
77 
78  /* (non-Javadoc)
79  * @see com.neuronrobotics.sdk.common.BowlerAbstractConnection#disconnect()
80  */
81  public void disconnect(){
82  if(udpSock != null)
83  udpSock.close();
84  udpSock=null;
85  setConnected(false);
86  }
87 
88  /* (non-Javadoc)
89  * @see com.neuronrobotics.sdk.common.BowlerAbstractConnection#connect()
90  */
91  @Override
92  public boolean connect() {
93  if(isConnected())
94  return true;
95  try {
96  udpSock = new DatagramSocket(port);
97  setConnected(true);
98 
99  } catch (SocketException e) {
100  // TODO Auto-generated catch block
101  e.printStackTrace();
102  }
103  return isConnected();
104  }
105 
106  /* (non-Javadoc)
107  * @see com.neuronrobotics.sdk.common.BowlerAbstractConnection#reconnect()
108  */
115  //@Override
116  public boolean reconnect() throws IOException {
117  disconnect();
118  connect();
119  return true;
120  }
121 
122  /* (non-Javadoc)
123  * @see com.neuronrobotics.sdk.common.BowlerAbstractConnection#waitingForConnection()
124  */
125  @Override
126  public boolean waitingForConnection() {
127  return false;
128  }
129 
130 
131 
138  @Override
139  public DataInputStream getDataIns() throws NullPointerException{
140  new RuntimeException("This method should not be called").printStackTrace();
141  while(true);
142  }
143 
150  @Override
151  public DataOutputStream getDataOuts() throws NullPointerException{
152  new RuntimeException("This method should not be called").printStackTrace();
153  while(true);
154  }
155 
162  //private ByteList outgoing = new ByteList();
163  public void write(byte[] data) throws IOException {
165  setLastWrite(System.currentTimeMillis());
166 
167  DatagramPacket sendPacket = new DatagramPacket(data, data.length, IPAddressSet, destinationPort);
168  Log.info("Sending UDP packet: "+sendPacket);
169  udpSock.send(sendPacket);
170 
171  }
172 
173  /* (non-Javadoc)
174  * @see com.neuronrobotics.sdk.common.BowlerAbstractConnection#loadPacketFromPhy(com.neuronrobotics.sdk.common.ByteList)
175  */
176  @Override
177  public BowlerDatagram loadPacketFromPhy(ByteList bytesToPacketBuffer) throws NullPointerException, IOException{
178  byte[] receiveData=new byte[4096];
179 
180  DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
181  //Log.info("Waiting for UDP packet");
182 
183  try{
184  udpSock.receive(receivePacket);
185  }catch(SocketException ex){
186  // disconnect called
187  Log. warning("Receive bailed out because of close");
188  return null;
189  }
190 
191  IPAddressSet=(receivePacket.getAddress());
192  destinationPort = receivePacket.getPort();
193 
194  Log.info("Got UDP packet from "+IPAddressSet+" : "+destinationPort);
195 
196  byte [] data = receivePacket.getData();
197 
198  for (int i=0;i<receivePacket.getLength();i++){
199  internalReceiveBuffer.add(data[i]);
200  }
201  BowlerDatagram bd =null;
202 
203  while(internalReceiveBuffer.size()>0){
204  bytesToPacketBuffer.add(internalReceiveBuffer.pop());
205  if (bd==null) {
206  bd = BowlerDatagramFactory.build(bytesToPacketBuffer);
207  }
208  }
209 
210  return bd;
211  }
212 
213 
214 }
synchronized boolean add(byte data)
Definition: ByteList.java:149
static void info(String message)
Definition: Log.java:110
BowlerDatagram loadPacketFromPhy(ByteList bytesToPacketBuffer)