OpenCores
URL https://opencores.org/ocsvn/udp_ip__core/udp_ip__core/trunk

Subversion Repositories udp_ip__core

[/] [udp_ip__core/] [trunk/] [JAVA_app/] [GigaRxLossy.java] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 NikosAl
/*
2
 * Copyright (C) 2010 Simon A. Berger
3
 *
4
 * This program is free software; you may redistribute it and/or modify its
5
 * under the terms of the GNU General Public License as published by the Free
6
 * Software Foundation; either version 2 of the License, or (at your option)
7
 * any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful, but
10
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
 * for more details.
13
 */
14
 
15
 
16
/*
17
 * This is the code used for the evaluation of the FPGA/PC communication using
18
 * UDP/IP.
19
 * This program can operate in two modes, which correspond to the two evaluations
20
 * in the paper:
21
 * - one way test: receive packets for 10 seconds. Calculate actual number of
22
 *   sent packets from the serial numbers.
23
 * - two way (duplex) test: send a fixed number of packets and count received
24
 *   packets.
25
 *
26
 * The program mode is controlled with the DUPLEX_TEST constant.
27
 *
28
 * To compile this program use "javac GigaRxLossy.java"
29
 * To run the program use "java GigaRxLoss"
30
 */
31
 
32
import java.io.IOException;
33
import java.net.DatagramPacket;
34
import java.net.DatagramSocket;
35
import java.net.InetAddress;
36
import java.net.InetSocketAddress;
37
import java.net.SocketAddress;
38
import java.nio.ByteBuffer;
39
import java.nio.MappedByteBuffer;
40
import java.nio.IntBuffer;
41
import java.nio.DoubleBuffer;
42
 
43
import java.nio.channels.ClosedByInterruptException;
44
import java.nio.channels.DatagramChannel;
45
 
46
 
47
public class GigaRxLossy {
48
        public static boolean isValid( ByteBuffer b, int size ) {
49
                int ref = b.get();
50
 
51
                for( int i = 1; i < size; i++ ) {
52
                        if( b.get() != ref ) {
53
 
54
                                return false;
55
                        }
56
                }
57
 
58
                return true;
59
        }
60
 
61
        public static void main(String[] args) throws IOException, InterruptedException {
62
                final int MTU = 1500;
63
 
64
                final SocketAddress rxaddr = new InetSocketAddress( 21844 );
65
 
66
                final DatagramChannel rxc = DatagramChannel.open();
67
 
68
                rxc.socket().bind(rxaddr);
69
 
70
                final SocketAddress txsendtoaddr = new InetSocketAddress( "192.168.1.1", 21845 );
71
 
72
 
73
                // set this constant to:
74
                // - false for the one-way test
75
                // - true for the duplex test
76
 
77
                boolean DUPLEX_TEST = !true;
78
 
79
 
80
                boolean haveTx;
81
                boolean trigger;
82
                if( DUPLEX_TEST ) {
83
                        haveTx = true;
84
                        trigger = false;
85
 
86
                } else {
87
                        haveTx = false;
88
                        trigger = true;
89
                }
90
 
91
 
92
                final DatagramChannel txc;
93
 
94
                if( haveTx || trigger )
95
                {
96
                        txc = DatagramChannel.open();
97
                        txc.socket().bind(null);
98
                } else {
99
                        txc = null;
100
                }
101
 
102
                final Thread reader = new Thread() {
103
                        // this is the recaiver thread
104
                        @Override
105
                        public void run() {
106
 
107
                                java.nio.ByteBuffer rxb = ByteBuffer.allocateDirect(MTU);
108
 
109
                                boolean first = true;
110
                                int firstser = -1;
111
                                int lastser = -1;
112
                                int nrec = 0;
113
                                long time = System.currentTimeMillis();
114
 
115
                                long rxbytes = 0;
116
                                long txn = 0;
117
                                try {
118
                                        //rxc.connect(rxaddr);
119
 
120
 
121
                                        while( !isInterrupted() ) {
122
 
123
                                                rxb.rewind();
124
                                                rxc.receive(rxb);
125
                                                int rxsize = rxb.position();
126
 
127
                                                rxb.rewind();
128
                                                //int ser = rxb.asIntBuffer().get(0);
129
                                                IntBuffer ib = rxb.asIntBuffer();
130
//                                              DoubleBuffer db = rxb.asDoubleBuffer();                                 
131
 
132
                                                int ser = ib.get()>>>24;
133
 
134
                                                // calculate the number of actually sent packets from the serial
135
                                                // number.
136
                                                if( !first ) {
137
                                                        if( ser < lastser ) {
138
                                                                txn += ser - (lastser - 256 );
139
                                                        } else {
140
                                                                txn += ser - lastser;
141
                                                        }
142
                                                        //System.out.printf( "int %d\n", txn );
143
                                                } else {
144
                                                        first = false;
145
                                                }
146
                                                lastser = ser;
147
 
148
                                                if( firstser == -1 ) {
149
                                                        firstser = ser;
150
                                                }
151
 
152
 
153
                                                // for maximum speed the validity check may be disabled as the current
154
                                                // implementation is fairly inefficient. In our tests we never got any
155
                                                // packet corruption.
156
                                                boolean CHECK_VALID = true;
157
                                                if( CHECK_VALID ) {
158
                                                        if( !isValid( rxb, rxsize ) ) {
159
                                                                System.out.println( "invalid" );
160
                                                        }
161
                                                }
162
//                                              lastser = ser;
163
                                                nrec++;
164
                                                rxbytes+=rxsize;
165
 
166
                                        }
167
                                } catch( ClosedByInterruptException e ) {
168
                                        System.out.printf( "reader: interrupted. bye ...\n" );
169
 
170
                                } catch (IOException e) {
171
 
172
                                        // TODO Auto-generated catch block
173
                                        e.printStackTrace();
174
                                        throw new RuntimeException( "bailing out." );
175
                                }
176
                                long dt = System.currentTimeMillis() - time;
177
                                System.out.printf( "%d bytes in %d ms: %.2f Mb/s\n", rxbytes, dt, rxbytes / (dt * 1000.0) );
178
                                int serrange = (lastser - firstser) + 1;
179
                                System.out.printf( "nrec: %d of %d (%.2f%%)\n", nrec, txn, nrec / (float)txn * 100.0 );
180
                        }
181
                };
182
 
183
                if( !trigger ) {
184
                        reader.start();
185
                }
186
 
187
                if( haveTx ) {
188
                        Thread writer = new Thread() {
189
                                // this is the sender thread.
190
                                @Override
191
                                public void run() {
192
                                        // TODO Auto-generated method stub
193
                                        //java.nio.ByteBuffer txb = MappedByteBuffer.allocate(MTU);
194
                                        java.nio.ByteBuffer txb = java.nio.ByteBuffer.allocateDirect(MTU);
195
                                        int i = 0;
196
                                        long time = System.currentTimeMillis();
197
 
198
                                        long txbytes = 0;
199
                                        long nj = 0;
200
                                        while( i < 1000000 ) {
201
                                                txb.rewind();
202
 
203
                                                txb.asIntBuffer().put(0, i);
204
                                                txb.rewind();
205
                                                try {
206
                                                        txbytes += txc.send(txb, txsendtoaddr);
207
 
208
 
209
 
210
                                                } catch (IOException e) {
211
                                                        // TODO Auto-generated catch block
212
                                                        e.printStackTrace();
213
                                                }
214
                                                i++;
215
 
216
                                                if( i % 10000 == 0 ) {
217
                                                        System.out.printf( "tx -> rx %d\n", i );
218
                                                }
219
 
220
        //                                      if( ack.i != -1 ) {
221
        //                                              i = ack.i;
222
        //                                              ack.i = -1;
223
        //                                      }
224
                                        }
225
                                        long dt = System.currentTimeMillis() - time;
226
                                        System.out.printf( "%d bytes in %d ms: %.2f Mb/s\n", txbytes, dt, txbytes / (dt * 1000.0) );
227
                                }
228
                        };
229
 
230
 
231
 
232
                        writer.start();
233
 
234
                        writer.join();
235
                        reader.interrupt();
236
                } else {
237
                        if( trigger ) {
238
                                java.nio.ByteBuffer txb = java.nio.ByteBuffer.allocateDirect(MTU);
239
                                txc.send(txb, txsendtoaddr);
240
                                Thread.sleep( 1000 );
241
                        }
242
                        reader.start();
243
                        Thread.sleep(10000);
244
                        reader.interrupt();
245
                }
246
        }
247
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.