File: sockets.cpp

package info (click to toggle)
mldemos 0.5.1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 32,224 kB
  • ctags: 46,525
  • sloc: cpp: 306,887; ansic: 167,718; ml: 126; sh: 109; makefile: 2
file content (245 lines) | stat: -rw-r--r-- 6,484 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// Copyright (C) 2006  Davis E. King (davis@dlib.net)
// License: Boost Software License   See LICENSE.txt for the full license.


#include <sstream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <dlib/sockets.h>
#include <dlib/server.h>
#include <dlib/misc_api.h>

#include "tester.h"

namespace  {

    using namespace test;
    using namespace dlib;
    using namespace std;

    dlib::mutex gm;
    dlib::signaler gs(gm);
    const char magic_num = 42;
    const int min_bytes_sent = 10000;
    int assigned_port;

    logger dlog("test.sockets");

// ----------------------------------------------------------------------------------------

    class serv : public server::kernel_1a_c
    {
    public:
        serv (
        ) :
            error_occurred (false),
            got_connections(false)
        {}

        void on_listening_port_assigned (
        )
        {
            auto_mutex M(gm);
            assigned_port = get_listening_port();
            gs.broadcast();
        }


        void on_connect (
            connection& con
        )
        {
            dlog << LINFO << "in serv::on_connect(): got new connection";
            int status;
            int count = 0;
            char buf[100];
            while ((status = con.read(buf,sizeof(buf))) > 0)
            {
                for (int i = 0; i < status; ++i)
                {
                    if (buf[i] != magic_num)
                    {
                        tag = 4.0;
                        error_occurred = true;
                    }
                }
                count += status;
            }
            if (count != min_bytes_sent)
            {
                tag = 5.0;
                error_occurred = true;
            }
            got_connections = true;
            dlog << LINFO << "in serv::on_connect(): on_connect ending";
        }

        bool error_occurred;
        bool got_connections;
        double tag;
    };

// ----------------------------------------------------------------------------------------

    class thread_container : public multithreaded_object
    {
    public:

        serv& srv;

        thread_container (
            serv& srv_
        ) : srv(srv_)
        {
            for (int i = 0; i < 10; ++i)
                register_thread(*this, &thread_container::thread_proc);

            // start up the threads
            start();
        }

        ~thread_container ()
        {
            // wait for all threads to terminate
            wait();
        }

        void thread_proc (
        )
        {
            try
            {
                dlog << LTRACE << "enter thread";
                {
                    auto_mutex M(gm);
                    while (assigned_port == 0)
                        gs.wait();
                }

                int status;
                scoped_ptr<connection> con;
                string hostname;
                string ip;
                status = get_local_hostname(hostname);
                if (status)
                {
                    srv.tag = 1.0;
                    srv.error_occurred = true;
                    srv.clear();
                    dlog << LERROR << "leaving thread, line: " << __LINE__;
                    dlog << LERROR << "get_local_hostname() failed";
                    return;
                }

                status = hostname_to_ip(hostname,ip);
                if (status)
                {
                    srv.tag = 2.0;
                    srv.error_occurred = true;
                    srv.clear();
                    dlog << LERROR << "leaving thread, line: " << __LINE__;
                    dlog << LERROR << "hostname_to_ip() failed";
                    return;
                }

                dlog << LTRACE << "try to connect to the server at port " << srv.get_listening_port();
                status = create_connection(con,srv.get_listening_port(),ip);
                if (status)
                {
                    srv.tag = 3.0;
                    srv.error_occurred = true;
                    srv.clear();
                    dlog << LERROR << "leaving thread, line: " << __LINE__;
                    dlog << LERROR << "create_connection() failed";
                    return;
                }

                dlog << LTRACE << "sending magic_num to server";
                int i;
                for (i = 0; i < min_bytes_sent; ++i)
                {
                    con->write(&magic_num,1); 
                }

                dlog << LTRACE << "shutting down connection to server";
                close_gracefully(con);
                dlog << LTRACE << "finished calling close_gracefully() on the connection";
            }
            catch (exception& e)
            {
                srv.error_occurred = true;
                dlog << LERROR << "exception thrown in thread_proc(): " << e.what();
                cout << "exception thrown in thread_proc(): " << e.what();
            }
            dlog << LTRACE << "exit thread";
        }
    };

    void run_server(serv* srv)
    {
        dlog << LTRACE << "calling srv.start()";
        srv->start();
        dlog << LTRACE << "srv.start() just ended.";
    }

    void sockets_test (
    )
    /*!
        requires
            - sockets is an implementation of sockets/sockets_kernel_abstract.h 
              is instantiated with int
        ensures
            - runs tests on sockets for compliance with the specs
    !*/
    {        

        dlog << LTRACE << "starting test";
        serv srv;

        assigned_port = 0;


        dlog << LTRACE << "spawning threads";
        thread_container stuff(srv);



        thread_function thread2(run_server, &srv);

        // wait until all the sending threads have ended
        stuff.wait();

        if (srv.error_occurred)
        {
            dlog << LDEBUG << "tag: " << srv.tag;
        }

        srv.clear();

        dlog << LTRACE << "ending successful test";
        DLIB_TEST( !srv.error_occurred); 
        DLIB_TEST( srv.got_connections); 
    }

// ----------------------------------------------------------------------------------------


    class sockets_tester : public tester
    {
    public:
        sockets_tester (
        ) :
            tester ("test_sockets",
                    "Runs tests on the sockets component.")
        {}

        void perform_test (
        )
        {
            sockets_test();
        }
    } a;

}