source: mainline/uspace/srv/net/tcp/test.c@ 08bb73b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 08bb73b was a2e3ee6, checked in by Martin Decky <martin@…>, 12 years ago

use new network address infrastructure (towards IPv6 support)

  • Property mode set to 100644
File size: 3.6 KB
RevLine 
[c5808b41]1/*
2 * Copyright (c) 2011 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup tcp
30 * @{
31 */
32
33/**
[032bbe7]34 * @file Internal TCP test
[c5808b41]35 */
36
37#include <async.h>
38#include <errno.h>
39#include <stdio.h>
[88a6819]40#include <fibril.h>
[32105348]41#include <str.h>
[c5808b41]42#include "tcp_type.h"
[762b48a]43#include "ucall.h"
[c5808b41]44
45#include "test.h"
46
[d9ce049]47#define RCV_BUF_SIZE 64
48
[88a6819]49static int test_srv(void *arg)
[c5808b41]50{
51 tcp_conn_t *conn;
[0ac2158]52 tcp_sock_t lsock;
53 tcp_sock_t fsock;
[d9ce049]54 char rcv_buf[RCV_BUF_SIZE + 1];
55 size_t rcvd;
56 xflags_t xflags;
[c5808b41]57
58 printf("test_srv()\n");
[a2e3ee6]59
60 inet_addr(&lsock.addr, 127, 0, 0, 1);
[0ac2158]61 lsock.port = 80;
[a2e3ee6]62
63 inet_addr(&fsock.addr, 127, 0, 0, 1);
[0ac2158]64 fsock.port = 1024;
[a2e3ee6]65
[eea65f4]66 printf("S: User open...\n");
[ae481e0]67 tcp_uc_open(&lsock, &fsock, ap_passive, 0, &conn);
[7cf7ded]68 conn->name = (char *) "S";
[d9ce049]69
70 while (true) {
[eea65f4]71 printf("S: User receive...\n");
[d9ce049]72 tcp_uc_receive(conn, rcv_buf, RCV_BUF_SIZE, &rcvd, &xflags);
[8c7a054]73 if (rcvd == 0) {
[6896409c]74 printf("S: End of data reached.\n");
[8c7a054]75 break;
76 }
[d9ce049]77 rcv_buf[rcvd] = '\0';
[eea65f4]78 printf("S: User received %zu bytes '%s'.\n", rcvd, rcv_buf);
[d9ce049]79
80 async_usleep(1000*1000*2);
81 }
[8c7a054]82
[7cf7ded]83 async_usleep(/*10**/1000*1000);
[6e88fea]84
[eea65f4]85 printf("S: User close...\n");
[6e88fea]86 tcp_uc_close(conn);
87
[8c7a054]88 printf("test_srv() terminating\n");
[88a6819]89 return 0;
[c5808b41]90}
91
[88a6819]92static int test_cli(void *arg)
[c5808b41]93{
94 tcp_conn_t *conn;
[0ac2158]95 tcp_sock_t lsock;
96 tcp_sock_t fsock;
[32105348]97 const char *msg = "Hello World!";
[c5808b41]98
99 printf("test_cli()\n");
[a2e3ee6]100
101 inet_addr(&lsock.addr, 127, 0, 0, 1);
[0ac2158]102 lsock.port = 1024;
[a2e3ee6]103
104 inet_addr(&fsock.addr, 127, 0, 0, 1);
[0ac2158]105 fsock.port = 80;
[8c7a054]106
[c5808b41]107 async_usleep(1000*1000*3);
[eea65f4]108 printf("C: User open...\n");
[ae481e0]109 tcp_uc_open(&lsock, &fsock, ap_active, 0, &conn);
[7cf7ded]110 conn->name = (char *) "C";
[32105348]111
112 async_usleep(1000*1000*10);
[eea65f4]113 printf("C: User send...\n");
[32105348]114 tcp_uc_send(conn, (void *)msg, str_size(msg), 0);
[8c7a054]115
[1812a0d]116 async_usleep(1000*1000*20/**20*2*/);
[eea65f4]117 printf("C: User close...\n");
[8c7a054]118 tcp_uc_close(conn);
[88a6819]119
120 return 0;
[c5808b41]121}
122
123void tcp_test(void)
124{
[88a6819]125 fid_t srv_fid;
126 fid_t cli_fid;
[c5808b41]127
128 printf("tcp_test()\n");
129
[1812a0d]130 async_usleep(1000*1000);
131
[a4ee3ab2]132 if (0) {
[88a6819]133 srv_fid = fibril_create(test_srv, NULL);
134 if (srv_fid == 0) {
135 printf("Failed to create server fibril.\n");
[a4ee3ab2]136 return;
137 }
[88a6819]138
139 fibril_add_ready(srv_fid);
[c5808b41]140 }
141
[04cd242]142 if (0) {
[88a6819]143 cli_fid = fibril_create(test_cli, NULL);
144 if (cli_fid == 0) {
145 printf("Failed to create client fibril.\n");
[04cd242]146 return;
147 }
[88a6819]148
149 fibril_add_ready(cli_fid);
[c5808b41]150 }
151}
152
153/**
154 * @}
155 */
Note: See TracBrowser for help on using the repository browser.