source: xplcommon/test/testlocsock.cc@ 18:0490048f256d

Last change on this file since 18:0490048f256d was 18:0490048f256d, checked in by István Váradi <ivaradi@…>, 12 years ago

Implemented basic test program for the sockets

File size: 4.7 KB
Line 
1// Copyright (c) 2012 by István Váradi
2
3// This file is part of libxplcommon, a common utility library for
4// development related to X-Plane
5
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are met:
8
9// 1. Redistributions of source code must retain the above copyright notice, this
10// list of conditions and the following disclaimer.
11// 2. Redistributions in binary form must reproduce the above copyright notice,
12// this list of conditions and the following disclaimer in the documentation
13// and/or other materials provided with the distribution.
14
15// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
26// The views and conclusions contained in the software and documentation are those
27// of the authors and should not be interpreted as representing official policies,
28// either expressed or implied, of the FreeBSD Project.
29
30//------------------------------------------------------------------------------
31
32#include <xplcommon/Thread.h>
33#include <xplcommon/Waiter.h>
34#include <xplcommon/LocalServerSocket.h>
35#include <xplcommon/LocalAcceptor.h>
36#include <xplcommon/LocalSocket.h>
37#include <xplcommon/LocalClientSocket.h>
38#include <xplcommon/Connector.h>
39#include <xplcommon/ReadingBuffer.h>
40#include <xplcommon/WritingBuffer.h>
41
42#include <cstdio>
43
44//------------------------------------------------------------------------------
45
46using xplcommon::Thread;
47using xplcommon::Waiter;
48using xplcommon::LocalServerSocket;
49using xplcommon::LocalAcceptor;
50using xplcommon::LocalSocket;
51using xplcommon::LocalClientSocket;
52using xplcommon::Connector;
53
54//------------------------------------------------------------------------------
55
56class ServerThread : public Thread
57{
58 virtual void run();
59};
60
61//------------------------------------------------------------------------------
62
63void ServerThread::run()
64{
65 printf("ServerThread::run\n");
66
67 Waiter waiter;
68
69 LocalServerSocket serverSocket("test", &waiter);
70 LocalAcceptor& acceptor = serverSocket.getAcceptor();
71
72 while(true) {
73 while(!acceptor.accept()) {
74 if (acceptor.failed()) {
75 printf("ServerThread::run: acceptor failed...\n");
76 return;
77 }
78 printf("ServerThread::run: waiting...\n");
79 waiter.wait();
80 }
81
82 printf("ServerThread::run: waiting done, received connection\n");
83 LocalSocket* socket = acceptor.getSocket();
84 delete socket;
85 break;
86 }
87}
88
89//------------------------------------------------------------------------------
90//------------------------------------------------------------------------------
91
92class ClientThread : public Thread
93{
94public:
95 virtual void run();
96};
97
98//------------------------------------------------------------------------------
99
100void ClientThread::run()
101{
102 printf("ClientThread::run: sleeping\n");
103 sleep(2000);
104 printf("ClientThread::run: connecting\n");
105
106 Waiter waiter;
107
108 LocalClientSocket socket("test", &waiter);
109
110 Connector& connector = socket.getConnector();
111
112 while(!connector.connect()) {
113 if (connector.failed()) {
114 printf("ClientThread::run: connector failed...\n");
115 return;
116 }
117 printf("ClientThread::run: waiting...\n");
118 waiter.wait();
119 }
120
121 printf("ClientThread::run: connected\n");
122}
123
124//------------------------------------------------------------------------------
125//------------------------------------------------------------------------------
126
127int main()
128{
129 ServerThread serverThread;
130 ClientThread clientThread;
131
132 printf("Starting threads\n");
133 serverThread.start();
134 clientThread.start();
135
136 printf("Waiting for the client thread\n");
137 clientThread.join();
138 printf("Waiting for the server thread\n");
139 serverThread.join();
140 printf("Both threads returned\n");
141
142 return 0;
143}
144
145//------------------------------------------------------------------------------
146
147// Local Variables:
148// mode: C++
149// c-basic-offset: 4
150// indent-tabs-mode: nil
151// End:
Note: See TracBrowser for help on using the repository browser.