3 import java.nio.ByteBuffer;
5 import com.kuka.task.ITaskLogger;
11 DatagramSocket socket = null;
17 InetAddress _address_send = null;
20 long message_counter = 0;
21 long noMessageCounter = 0;
22 long noMessageCounterLimit = 9999999;
27 byte[] recBuf =
new byte[1400];
33 long lastMessageStartTime;
34 long lastMessageElapsedTime;
35 long lastMessageTimeoutMilliseconds = 1000;
37 int retriesAllowed = 3;
38 int retriesAttempted = 0;
40 public UDPManager(String laptopIP, String laptopPort, ITaskLogger errorlogger) {
42 this.logger = errorlogger;
43 _Remote_IP = laptopIP;
44 _Remote_Port = Integer.parseInt(laptopPort);
47 _address_send = InetAddress.getByName(_Remote_IP);
48 }
catch (UnknownHostException e) {
49 logger.error(
"Could not create InetAddress for sending");
62 logger.info(
"Waiting for connection initialization...");
65 socket =
new DatagramSocket();
66 }
catch (SocketException e1) {
67 logger.info(
"failed to create socket.");
73 socket.setSoTimeout(100);
74 }
catch (SocketException e1) {
75 logger.error(
"UDPManager failed to set socket timeout");
78 startTime = System.currentTimeMillis();
81 int newStatesLength = 0;
83 boolean connectionEstablished =
false;
85 while(newStatesLength<1 && newKUKAiiwaStates == null){
87 DatagramPacket packet =
new DatagramPacket(recBuf, recBuf.length);
90 while (!connectionEstablished){
91 connectionEstablished = preConnect();
93 logger.info(
"Stopping program.");
98 if(packet.getLength() > 0){
100 bb = ByteBuffer.wrap(recBuf);
108 logger.info(
"Stopping program.");
113 _currentKUKAiiwaStates = newKUKAiiwaStates;
114 statesLength = newStatesLength;
116 logger.info(
"States initialized...");
118 startTime = System.currentTimeMillis();
119 lastMessageStartTime = startTime;
120 lastMessageElapsedTime = System.currentTimeMillis() - lastMessageStartTime;
127 private boolean preConnect()
131 String dummyMessage =
"Hi";
133 DatagramPacket packetSend=
new DatagramPacket(dummyMessage.getBytes(), dummyMessage.getBytes().length, _address_send, _Remote_Port);
136 socket.send(packetSend);
137 }
catch (IOException e1) {
141 DatagramPacket packet =
new DatagramPacket(recBuf, recBuf.length);
142 socket.receive(packet);
144 }
catch (SocketTimeoutException e) {
147 }
catch (IOException e) {
161 public boolean sendMessage(byte[] msg,
int size)
throws IOException
163 DatagramPacket packet=
new DatagramPacket(msg, size, _address_send , _Remote_Port );
170 boolean haveNextMessage =
false;
171 while(!stop && !haveNextMessage) {
173 DatagramPacket packet =
new DatagramPacket(recBuf, recBuf.length);
175 socket.receive(packet);
177 if(packet.getLength() > 0){
180 bb = ByteBuffer.wrap(recBuf);
184 if(_currentKUKAiiwaStates.statesLength()>0) {
187 if (tmp == null || tmp.armControlState() == null) {
188 noMessageCounter +=1;
189 if (message_counter % 100 == 0) {
190 logger.warn(
"NULL ArmControlState message, main UDP FlatBuffer message is arriving but doesn't contain any data/commands!");
194 _previousKUKAiiwaState = _currentKUKAiiwaState;
195 _currentKUKAiiwaState = tmp;
198 if (_currentKUKAiiwaState == null) {
200 logger.error(
"Missing current state message!");
204 haveNextMessage=
true;
205 noMessageCounter = 0;
206 lastMessageStartTime = System.currentTimeMillis();
208 logger.error(
"got a UDP packet but it isn't a valid FlatBuffer message, this is an unexpected state that shouldn't occur. please debug me.");
212 }
catch (IOException e) {
213 noMessageCounter +=1;
214 if (message_counter % 100 == 0) {
215 logger.warn(
"Failed to receive UDP packet from control computer... Trying to re-establish connection");
222 return _currentKUKAiiwaState;
226 return _currentKUKAiiwaState;
230 return _previousKUKAiiwaState;
244 logger.error(
"socket closed");
256 logger.error(
"Trying to close socket");
258 logger.error(
"Socket Closed");
262 logger.error(
"Could not close socket");
grl.flatbuffer.KUKAiiwaState getPrevMessage()
grl.flatbuffer.KUKAiiwaState getCurrentMessage()
UDPManager(String laptopIP, String laptopPort, ITaskLogger errorlogger)
boolean sendMessage(byte[] msg, int size)
grl.flatbuffer.KUKAiiwaState waitForNextMessage()
void setStop(boolean stop)
static KUKAiiwaStates getRootAsKUKAiiwaStates(ByteBuffer _bb)