Remove handleMapRegister(MapRegister, boolean)
[lispflowmapping.git] / integrationtest / src / test / java / org / opendaylight / lispflowmapping / integrationtest / SocketReader.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.lispflowmapping.integrationtest;
9
10 import static org.junit.Assert.fail;
11
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;
19
20 /**
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
23  * access red packets.
24  */
25 public class SocketReader implements Runnable {
26
27     private static final Logger LOG = LoggerFactory.getLogger(SocketReader.class);
28     /**
29      * max number of packets which can be stored to this to buffer
30      */
31     private static final int MAX_NUMBER_OF_PACKETS_TO_STORE = 100;
32     private final DatagramSocket socket;
33
34     /**
35      * array of buffers where SMR messages are stored
36      */
37     private byte[][] buffers = new byte[MAX_NUMBER_OF_PACKETS_TO_STORE][4096];
38
39     /**
40      * Index to array of buffers where current writting is done
41      */
42     private int currentBufferWriteIndex = 0;
43
44     /**
45      * Index to array of buffers from where current reading is done
46      */
47     private int currentBufferReaderIndex = 0;
48     private DatagramPacket receivePacket;
49     private boolean readFromSocket = true;
50
51     private SocketReader(DatagramSocket receivedSocket) {
52         this.socket = receivedSocket;
53     }
54
55     static SocketReader startReadingInStandaloneThread(final DatagramSocket socket) throws SocketTimeoutException {
56         try {
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");
61             thread.start();
62             return socketReader;
63         } catch (Throwable t) {
64             fail();
65             return null;
66         }
67     }
68
69     @Override
70     public void run() {
71         while (readFromSocket && currentBufferReaderIndex < MAX_NUMBER_OF_PACKETS_TO_STORE) {
72             receivePacket = new DatagramPacket(buffers[currentBufferWriteIndex], buffers[currentBufferWriteIndex].
73                     length);
74             try {
75                 socket.receive(receivePacket);
76             } catch (IOException e) {
77                 LOG.debug("Problem while reading SMR test socket.", e);
78             }
79             currentBufferWriteIndex++;
80         }
81     }
82
83     void stopReading() {
84         readFromSocket = false;
85         socket.close();
86     }
87
88     /**
89      * Read from buffers {@code count} number of buffers from current postion.
90      *
91      * @param count how many buffer should be returned.
92      * @return array of buffers
93      */
94     byte[][] getBuffers(final int count) {
95         final byte[][] subBuffer = Arrays.copyOfRange(buffers, currentBufferReaderIndex, currentBufferReaderIndex +
96                 count);
97         currentBufferReaderIndex = currentBufferReaderIndex + count;
98         return subBuffer;
99     }
100
101 }