2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.lispflowmapping.integrationtest;
10 import static org.junit.Assert.fail;
12 import java.io.IOException;
13 import java.net.DatagramPacket;
14 import java.net.DatagramSocket;
15 import java.net.SocketTimeoutException;
16 import java.util.Arrays;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
21 * Read data from specified socket in standalone thread. Packets are stored to array of buffer. In other words each
22 * packet is stored in standalone buffer. So whenever during existence of instance of this class it is possible to
25 public class SocketReader implements Runnable {
27 private static final Logger LOG = LoggerFactory.getLogger(SocketReader.class);
29 * max number of packets which can be stored to this to buffer
31 private static final int MAX_NUMBER_OF_PACKETS_TO_STORE = 100;
32 private final DatagramSocket socket;
35 * array of buffers where SMR messages are stored
37 private byte[][] buffers = new byte[MAX_NUMBER_OF_PACKETS_TO_STORE][4096];
40 * Index to array of buffers where current writting is done
42 private int currentBufferWriteIndex = 0;
45 * Index to array of buffers from where current reading is done
47 private int currentBufferReaderIndex = 0;
48 private DatagramPacket receivePacket;
49 private boolean readFromSocket = true;
51 private SocketReader(DatagramSocket receivedSocket) {
52 this.socket = receivedSocket;
55 static SocketReader startReadingInStandaloneThread(final DatagramSocket socket) throws SocketTimeoutException {
57 socket.setSoTimeout(0);
58 final SocketReader socketReader = new SocketReader(socket);
59 final Thread thread = new Thread(socketReader);
60 thread.setName("Socket reader - multisite integration test - lispflowmapping");
63 } catch (Throwable t) {
71 while (readFromSocket && currentBufferReaderIndex < MAX_NUMBER_OF_PACKETS_TO_STORE) {
72 receivePacket = new DatagramPacket(buffers[currentBufferWriteIndex], buffers[currentBufferWriteIndex].
75 socket.receive(receivePacket);
76 } catch (IOException e) {
77 LOG.debug("Problem while reading SMR test socket.", e);
79 currentBufferWriteIndex++;
84 readFromSocket = false;
89 * Read from buffers {@code count} number of buffers from current postion.
91 * @param count how many buffer should be returned.
92 * @return array of buffers
94 byte[][] getBuffers(final int count) {
95 final byte[][] subBuffer = Arrays.copyOfRange(buffers, currentBufferReaderIndex, currentBufferReaderIndex +
97 currentBufferReaderIndex = currentBufferReaderIndex + count;