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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [net/] [common/] [current/] [tests/] [tcp_lo_select.c] - Blame information for rev 857

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//      tests/tcp_lo_test.c
4
// 
5
//      Simple TCP throughput test 
6
//
7
//==========================================================================
8
// ####BSDALTCOPYRIGHTBEGIN####                                             
9
// -------------------------------------------                              
10
// Portions of this software may have been derived from FreeBSD, OpenBSD,   
11
// or other sources, and if so are covered by the appropriate copyright     
12
// and license included herein.                                             
13
// -------------------------------------------                              
14
// ####BSDALTCOPYRIGHTEND####                                               
15
//==========================================================================
16
//#####DESCRIPTIONBEGIN####
17
//
18
// Author(s):    sorin@netappi.com 
19
// Contributors: gthomas,sorin@netappi.com 
20
// Date:         2000-05-24
21
 
22
 
23
// Network throughput test code
24
 
25
#include <network.h>
26
 
27
#include <cyg/infra/testcase.h>
28
 
29
#ifndef CYGPKG_LIBC_STDIO
30
#define perror(s) diag_printf(#s ": %s\n", strerror(errno))
31
#endif
32
 
33
#define SOURCE_PORT1 9990
34
#define SOURCE_PORT2   9991
35
 
36
#define NUM_BUF 1024
37
#define MAX_BUF 8192
38
static unsigned char data_buf1[MAX_BUF];
39
static unsigned char data_buf2[MAX_BUF];
40
static unsigned char data_buf_write1[MAX_BUF]="Client 1 is alive. You may continue ....";
41
static unsigned char data_buf_write2[MAX_BUF]="Client 2 is alive. You may continue ....";
42
 
43
 
44
#define STACK_SIZE (CYGNUM_HAL_STACK_SIZE_TYPICAL + 0x10000)
45
 
46
static char stack_server[STACK_SIZE];
47
static cyg_thread server_thread_data;
48
static cyg_handle_t server_thread_handle;
49
 
50
static char stack_client1[STACK_SIZE];
51
static cyg_thread client1_thread_data;
52
static cyg_handle_t client1_thread_handle;
53
 
54
static char stack_client2[STACK_SIZE];
55
static cyg_thread client2_thread_data;
56
static cyg_handle_t client2_thread_handle;
57
 
58
 
59
#define MAIN_THREAD_PRIORITY     CYGPKG_NET_THREAD_PRIORITY-4
60
 
61
void
62
pexit(char *s)
63
{
64
    CYG_TEST_FAIL_FINISH( s );
65
}
66
 
67
 
68
#ifndef max
69
#define max(a,b) (((a) > (b)) ? (a) : (b))
70
#endif
71
 
72
void server(void)
73
{
74
    int s_s1, e_s1, s_s2, e_s2;
75
    struct sockaddr_in e_s1_addr,e_s2_addr,local;
76
    fd_set in_fds;
77
    socklen_t len;
78
    int num;
79
 
80
    char *hello_string=" Hello eCos network \n";
81
    diag_printf("TCP SERVER:");
82
    diag_printf(hello_string);
83
 
84
    s_s1 = socket(AF_INET, SOCK_STREAM, 0);
85
    if (s_s1 < 0) {
86
        pexit("stream socket");
87
    }
88
    memset(&local, 0, sizeof(local));
89
    local.sin_family = AF_INET;
90
    local.sin_len = sizeof(local);
91
    local.sin_port = ntohs(SOURCE_PORT1);
92
    local.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
93
    if(bind(s_s1, (struct sockaddr *) &local, sizeof(local)) < 0) {
94
        pexit("bind /source_1/ error");
95
    }
96
    listen(s_s1, SOMAXCONN);
97
 
98
    s_s2 = socket(AF_INET, SOCK_STREAM, 0);
99
    if (s_s2 < 0) {
100
        pexit("stream socket");
101
    }
102
    memset(&local, 0, sizeof(local));
103
    local.sin_family = AF_INET;
104
    local.sin_len = sizeof(local);
105
    local.sin_port = ntohs(SOURCE_PORT2);
106
    local.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
107
    if(bind(s_s2, (struct sockaddr *) &local, sizeof(local)) < 0) {
108
        pexit("bind /source_2/ error");
109
    }
110
    listen(s_s2, SOMAXCONN);
111
 
112
 
113
    e_s1 = 0; e_s2 = 0;
114
 
115
 
116
    while (true) {
117
        FD_ZERO(&in_fds);
118
        FD_SET(s_s1, &in_fds);
119
        FD_SET(s_s2, &in_fds);
120
        num = select ( max(s_s1,s_s2)+1, &in_fds,0,0,0);
121
        if (FD_ISSET(s_s1,&in_fds)) {
122
                len = sizeof(e_s1_addr);
123
                if ((e_s1 = accept(s_s1,(struct sockaddr *)&e_s1_addr,&len))<0)
124
                        {
125
                        pexit("accept /source_1/");
126
                        }
127
        diag_printf("TCP SERVER connection from %s: %d\n",
128
               inet_ntoa(e_s1_addr.sin_addr),ntohs(e_s1_addr.sin_port));
129
        }
130
        if (FD_ISSET(s_s2,&in_fds)) {
131
                len = sizeof(e_s2_addr);
132
                if ((e_s2 = accept(s_s2,(struct sockaddr *)&e_s2_addr,&len))<0)
133
                        {
134
                        pexit("accept /source_2/");
135
                        }
136
        diag_printf("TCP SERVER connection from %s: %d\n",
137
               inet_ntoa(e_s2_addr.sin_addr), ntohs(e_s2_addr.sin_port));
138
        }
139
 
140
        if ((e_s1 != 0) && ( e_s2 != 0)) {
141
            break;
142
            }
143
 
144
        }   /* while (true) */
145
 
146
     if ((len = read(e_s1, data_buf1, MAX_BUF)) < 0  )
147
        {
148
                perror("I/O error");
149
        }
150
   diag_printf("SERVER : %s\n",data_buf1);
151
 
152
     if ((len = read(e_s2, data_buf2, MAX_BUF)) < 0  )
153
        {
154
                perror("I/O error");
155
        }
156
   diag_printf("SERVER : %s\n",data_buf2);
157
 
158
}
159
 
160
void client1(void)
161
{
162
    int s_source;
163
    struct sockaddr_in local;
164
    int len;
165
 
166
    diag_printf("client 1 :started\n");
167
 
168
    s_source = socket(AF_INET, SOCK_STREAM, 0);
169
    if (s_source < 0) {
170
        pexit("stream socket");
171
    }
172
    memset(&local, 0, sizeof(local));
173
    local.sin_family = AF_INET;
174
    local.sin_len = sizeof(local);
175
    local.sin_port = htons(SOURCE_PORT1);
176
    local.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
177
 
178
    if (connect(s_source, (struct sockaddr *)&local, sizeof(local)) < 0) {
179
        pexit("Can't connect to target");
180
    }
181
 
182
    if ((len = write(s_source,data_buf_write1,40)) < 0)  {
183
        CYG_TEST_FAIL_FINISH("Error writing buffer");
184
    }
185
}
186
 
187
void client2(void)
188
{
189
    int s_source;
190
    struct sockaddr_in local;
191
    int len;
192
 
193
    diag_printf("client 2 :started\n");
194
 
195
    s_source = socket(AF_INET, SOCK_STREAM, 0);
196
    if (s_source < 0) {
197
        pexit("stream socket");
198
    }
199
    memset(&local, 0, sizeof(local));
200
    local.sin_family = AF_INET;
201
    local.sin_len = sizeof(local);
202
    local.sin_port = htons(SOURCE_PORT2);
203
    local.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
204
 
205
    if (connect(s_source, (struct sockaddr *)&local, sizeof(local)) < 0) {
206
        pexit("Can't connect to target");
207
    }
208
 
209
    if ((len = write(s_source,data_buf_write2,40)) < 0) {
210
        CYG_TEST_FAIL_FINISH("Error writing buffer");
211
    }
212
}
213
 
214
 
215
void
216
tcp_server(cyg_addrword_t param)
217
{
218
    init_all_network_interfaces();
219
    diag_printf("Start TCP server - test\n");
220
    cyg_thread_resume(client1_thread_handle);   // Start it
221
    cyg_thread_resume(client2_thread_handle);   // Start it
222
#if NLOOP > 0
223
    server();
224
    CYG_TEST_PASS_FINISH("Server returned OK");
225
#endif
226
    CYG_TEST_NA( "No loopback devs" );
227
}
228
 
229
void
230
tcp_client_1(cyg_addrword_t param)
231
{
232
    diag_printf("Start TCP client 1 - test\n");
233
#if NLOOP > 0
234
    client1();
235
#endif
236
}
237
 
238
void
239
tcp_client_2(cyg_addrword_t param)
240
{
241
    diag_printf("Start TCP client 2 - test\n");
242
#if NLOOP > 0
243
    client2();
244
#endif
245
}
246
 
247
 
248
void
249
cyg_start(void)
250
{
251
    CYG_TEST_INIT();
252
 
253
    cyg_thread_create(MAIN_THREAD_PRIORITY,     // Priority
254
                      tcp_server,               // entry
255
                      0,                        // entry parameter
256
                      "TCP loopback server",    // Name
257
                      &stack_server[0],         // Stack
258
                      STACK_SIZE,               // Size
259
                      &server_thread_handle,    // Handle
260
                      &server_thread_data       // Thread data structure
261
            );
262
    cyg_thread_resume(server_thread_handle);    // Start it
263
 
264
    cyg_thread_create(MAIN_THREAD_PRIORITY,     // Priority
265
                      tcp_client_1,             // entry
266
                      0,                        // entry parameter
267
                      "TCP loopback client1",   // Name
268
                      &stack_client1[0],        // Stack
269
                      STACK_SIZE,               // Size
270
                      &client1_thread_handle,   // Handle
271
                      &client1_thread_data      // Thread data structure
272
            );
273
 
274
    cyg_thread_create(MAIN_THREAD_PRIORITY,     // Priority
275
                      tcp_client_2,             // entry
276
                      0,                        // entry parameter
277
                      "TCP llopback client2",   // Name
278
                      &stack_client2[0],        // Stack
279
                      STACK_SIZE,               // Size
280
                      &client2_thread_handle,   // Handle
281
                      &client2_thread_data      // Thread data structure
282
            );
283
 
284
    cyg_scheduler_start();
285
}
286
 

powered by: WebSVN 2.1.0

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