summaryrefslogtreecommitdiffstats
path: root/ml/dlib/dlib/test/iosockstream.cpp
blob: c68de7bd31067978c5aea6e079f1aaca9adb20ff (plain)
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
// Copyright (C) 2012  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/iosockstream.h>
#include <dlib/server.h>
#include <vector>

#include "tester.h"

namespace  
{

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


    logger dlog("test.iosockstream");

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

    class serv : public server_iostream
    {
        virtual void on_connect (
            std::istream& in,
            std::ostream& out,
            const std::string& ,
            const std::string& ,
            unsigned short ,
            unsigned short ,
            uint64 
        )
        {
            try
            {
                dlog << LINFO << "serv1: serving connection";

                std::string temp;
                in >> temp;
                DLIB_TEST(temp == "word");
                in >> temp;
                DLIB_TEST(temp == "another");
                out << "yay words ";
                in >> temp;
                DLIB_TEST(temp == "yep");
            }
            catch (error& e)
            {
                error_string = e.what();
            }

        }


    public:
        std::string error_string;

    };

    class serv2 : public server_iostream
    {
        virtual void on_connect (
            std::istream& ,
            std::ostream& out,
            const std::string& ,
            const std::string& ,
            unsigned short ,
            unsigned short ,
            uint64 
        )
        {
            try
            {
                dlog << LINFO << "serv2: serving connection";

                out << "one two three four five";
            }
            catch (error& e)
            {
                error_string = e.what();
            }

        }


    public:
        std::string error_string;

    };

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

    void test1()
    {
        dlog << LINFO << "in test1()";
        serv theserv;
        theserv.set_listening_port(12345);
        theserv.start_async();

        // wait a little bit to make sure the server has started listening before we try 
        // to connect to it.
        dlib::sleep(500);

        for (int i = 0; i < 200; ++i)
        {
            dlog << LINFO << "i: " << i;
            print_spinner();
            iosockstream stream("localhost:12345");

            stream << "word another ";
            std::string temp;
            stream >> temp;
            DLIB_TEST(temp == "yay");
            stream >> temp;
            DLIB_TEST(temp == "words");
            stream << "yep ";
        }

        // Just to make sure the server finishes processing the last connection before
        // we kill it and accidentally trigger a DLIB_TEST().
        dlib::sleep(500);

        if (theserv.error_string.size() != 0)
            throw error(theserv.error_string);
    }

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

    void test2()
    {
        dlog << LINFO << "in test2()";
        serv2 theserv;
        theserv.set_listening_port(12345);
        theserv.start_async();

        // wait a little bit to make sure the server has started listening before we try 
        // to connect to it.
        dlib::sleep(500);

        for (int i = 0; i < 200; ++i)
        {
            dlog << LINFO << "i: " << i;
            print_spinner();
            iosockstream stream("localhost:12345");

            std::string temp;
            stream >> temp; DLIB_TEST(temp == "one");
            stream >> temp; DLIB_TEST(temp == "two");
            stream >> temp; DLIB_TEST(temp == "three");
            stream >> temp; DLIB_TEST(temp == "four");
            stream >> temp; DLIB_TEST(temp == "five");
        }
    }

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

    class test_iosockstream : public tester
    {
    public:
        test_iosockstream (
        ) :
            tester ("test_iosockstream",
                    "Runs tests on the iosockstream component.")
        {}

        void perform_test (
        )
        {
            test1();
            test2();
        }
    } a;

}