1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* This program uses a QEMUD pipe to exchange data with a test 18 * server. It's very simple: 19 * 20 * for count in range(0,100): 21 * msg = "Hello Word " + count 22 * qemud_pipe_send(msg) 23 * qemud_pipe_recv(msg2) 24 * if (msg != msg2): 25 * error() 26 * 27 * 28 * See test_host_1.c for the corresponding server code, which simply 29 * sends back anything it receives from the client. 30 */ 31 #include "test_util.h" 32 #include <errno.h> 33 #include <string.h> 34 #include <stddef.h> 35 #include <stdio.h> 36 37 #define PIPE_NAME "pingpong" 38 39 40 int main(void) 41 { 42 Pipe pipe[1]; 43 const int maxCount = 100; 44 int port = 8012; 45 46 #if 0 47 if (pipe_openSocket(pipe, port) < 0) { 48 fprintf(stderr, "Could not open tcp socket!\n"); 49 return 1; 50 } 51 printf("Connected to tcp:host:%d\n", port); 52 #else 53 if (pipe_openQemuPipe(pipe, PIPE_NAME) < 0) { 54 fprintf(stderr, "Could not open '%s' pipe: %s\n", PIPE_NAME, strerror(errno)); 55 return 1; 56 } 57 printf("Connected to '%s' pipe\n", PIPE_NAME); 58 #endif 59 60 char buff[64]; 61 char buff2[64]; 62 int count; 63 double time0 = now_secs(); 64 size_t total = 0; 65 66 for (count = 0; count < maxCount; count++) { 67 /* First, send a small message */ 68 int len = snprintf(buff, sizeof(buff), "Hello World %d\n", count); 69 printf("%4d: Sending %d bytes\n", count, len); 70 int ret = pipe_send(pipe, buff, len); 71 if (ret < 0) { 72 fprintf(stderr,"Sending %d bytes failed: %s\n", len, strerror(errno)); 73 return 1; 74 } 75 76 total += len; 77 78 /* The server is supposed to send the message back */ 79 ret = pipe_recv(pipe, buff2, len); 80 if (ret < 0) { 81 fprintf(stderr, "Receiving failed (ret=%d): %s\n", ret, strerror(errno)); 82 return 3; 83 } 84 printf("%4d: Received %d bytes\n", count, ret); 85 /* Check the message's content */ 86 if (ret != len) { 87 fprintf(stderr, "Message size mismatch sent=%d received=%d\n", len, ret); 88 return 5; 89 } 90 if (memcmp(buff, buff2, len) != 0) { 91 fprintf(stderr, "Message content mismatch!\n"); 92 return 6; 93 } 94 } 95 96 double time1 = now_secs(); 97 98 printf("Closing pipe\n"); 99 pipe_close(pipe); 100 101 printf("Bandwidth: %g MB/s, %g bytes in %g seconds.\n", 102 total*1.0 / (1024.*1024.*(time1-time0)), 1.0*total, time1-time0); 103 104 return 0; 105 } 106