summaryrefslogtreecommitdiff
path: root/tools/Replay.hpp
blob: d9a654bd78e9b9431e7c1798c81b18b72f379055 (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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#ifndef INCLUDED_REPLAY_HPP
#define INCLUDED_REPLAY_HPP

#include <Poco/Net/HTTPRequest.h>
#include <Poco/Net/HTTPResponse.h>

#include <LOOLWebSocket.hpp>

#include "TraceFile.hpp"
#include <test/helpers.hpp>

/// Connection class with WSD.
class Connection
{
public:
    static
    std::shared_ptr<Connection> create(const std::string& serverURI, const std::string& documentURL, const std::string& sessionId)
    {
        try
        {
            Poco::URI uri(serverURI);

            std::unique_lock<std::mutex> lock(Mutex);

            // Load a document and get its status.
            std::cout << "NewSession [" << sessionId << "]: " << uri.toString() << "... ";

            std::string encodedUri;
            Poco::URI::encode(documentURL, ":/?", encodedUri);
            Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, "/lool/" + encodedUri + "/ws");
            Poco::Net::HTTPResponse response;
            std::shared_ptr<LOOLWebSocket> ws = helpers::connectLOKit(uri, request, response, sessionId + ' ');
            std::cout << "Connected to " << serverURI << ".\n";
            return std::shared_ptr<Connection>(new Connection(documentURL, sessionId, ws));
        }
        catch (const std::exception& exc)
        {
            std::cout << "ERROR while connecting to [" << serverURI << "]: " << exc.what() << std::endl;
            return nullptr;
        }
    }

    const std::string& getName() const { return _name; }
    std::shared_ptr<LOOLWebSocket> getWS() const { return _ws; };

    /// Send a command to the server.
    bool send(const std::string& data) const
    {
        try
        {
            helpers::sendTextFrame(_ws, data, _name);
            return true;
        }
        catch (const std::exception& exc)
        {
            std::cout << "Error in " << _name << " while sending ["
                      << data << "]: " << exc.what() << std::endl;
        }

        return false;
    }

    /// Poll socket until expected prefix is fetched, or timeout.
    std::vector<char> recv(const std::string& prefix)
    {
        return helpers::getResponseMessage(_ws, prefix, _name);
    }

    /// Request loading the document and wait for completion.
    bool load()
    {
        send("load url=" + _documentURL);
        return helpers::isDocumentLoaded(_ws, _name);
    }

private:
    Connection(const std::string& documentURL, const std::string& sessionId, std::shared_ptr<LOOLWebSocket>& ws) :
        _documentURL(documentURL),
        _sessionId(sessionId),
        _name(sessionId + ' '),
        _ws(ws)
    {
    }

private:
    const std::string _documentURL;
    const std::string _sessionId;
    const std::string _name;
    std::shared_ptr<LOOLWebSocket> _ws;
    static std::mutex Mutex;
};

/// Main thread class to replay a trace file.
class Replay : public Poco::Runnable
{
public:

    Replay(const std::string& serverUri, const std::string& uri, bool ignoreTiming = true) :
        _serverUri(serverUri),
        _uri(uri),
        _ignoreTiming(ignoreTiming)
    {
    }

    void run() override
    {
        try
        {
            replay();
        }
        catch (const Poco::Exception &e)
        {
            std::cout << "Error: " << e.name() << ' ' << e.message() << std::endl;
        }
        catch (const std::exception &e)
        {
            std::cout << "Error: " << e.what() << std::endl;
        }
    }

protected:

    void replay()
    {
        TraceFileReader traceFile(_uri);

        Poco::Int64 epochFile(traceFile.getEpochStart());
        auto epochCurrent = std::chrono::steady_clock::now();

        const Poco::Int64 replayDuration = (traceFile.getEpochEnd() - epochFile);
        std::cout << "Replaying file [" << _uri << "] of " << replayDuration / 1000000. << " second length." << std::endl;

        for (;;)
        {
            const TraceFileRecord rec = traceFile.getNextRecord();
            if (rec.getDir() == TraceFileRecord::Direction::Invalid)
            {
                // End of trace file.
                break;
            }

            const std::chrono::microseconds::rep deltaCurrent = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - epochCurrent).count();
            const unsigned deltaFile = rec.getTimestampNs() - epochFile;
            const unsigned delay = (_ignoreTiming ? 0 : deltaFile - deltaCurrent);
            if (delay > 0)
            {
                if (delay > 1e6)
                {
                    std::cout << "Sleeping for " << delay / 1000 << " ms.\n";
                }

                std::this_thread::sleep_for(std::chrono::microseconds(delay));
            }

            std::cout << rec.toString() << std::endl;

            if (rec.getDir() == TraceFileRecord::Direction::Event)
            {
                // Meta info about about an event.
                static const std::string NewSession("NewSession: ");
                static const std::string EndSession("EndSession: ");

                if (rec.getPayload().find(NewSession) == 0)
                {
                    const std::string uriOrig = rec.getPayload().substr(NewSession.size());
                    std::string uri;
                    Poco::URI::decode(uriOrig, uri);
                    auto it = _sessions.find(uri);
                    if (it != _sessions.end())
                    {
                        // Add a new session.
                        if (it->second.find(rec.getSessionId()) != it->second.end())
                        {
                            std::cout << "ERROR: session [" << rec.getSessionId() << "] already exists on doc [" << uri << "]\n";
                        }
                        else
                        {
                            std::shared_ptr<Connection> connection = Connection::create(_serverUri, uri, rec.getSessionId());
                            if (connection)
                            {
                                it->second.emplace(rec.getSessionId(), connection);
                            }
                        }
                    }
                    else
                    {
                        std::cout << "New Document: " << uri << "\n";
                        _childToDoc.emplace(rec.getPid(), uri);
                        std::shared_ptr<Connection> connection = Connection::create(_serverUri, uri, rec.getSessionId());
                        if (connection)
                        {
                            _sessions[uri].emplace(rec.getSessionId(), connection);
                        }
                    }
                }
                else if (rec.getPayload().find(EndSession) == 0)
                {
                    const std::string uriOrig = rec.getPayload().substr(EndSession.size());
                    std::string uri;
                    Poco::URI::decode(uriOrig, uri);
                    auto it = _sessions.find(uri);
                    if (it != _sessions.end())
                    {
                        std::cout << "EndSession [" << rec.getSessionId() << "]: " << uri << "\n";

                        it->second.erase(rec.getSessionId());
                        if (it->second.empty())
                        {
                            std::cout << "End Doc [" << uri << "].\n";
                            _sessions.erase(it);
                            _childToDoc.erase(rec.getPid());
                        }
                    }
                    else
                    {
                        std::cout << "ERROR: Doc [" << uri << "] does not exist.\n";
                    }
                }
            }
            else if (rec.getDir() == TraceFileRecord::Direction::Incoming)
            {
                auto docIt = _childToDoc.find(rec.getPid());
                if (docIt != _childToDoc.end())
                {
                    const auto& uri = docIt->second;
                    auto it = _sessions.find(uri);
                    if (it != _sessions.end())
                    {
                        const auto sessionIt = it->second.find(rec.getSessionId());
                        if (sessionIt != it->second.end())
                        {
                            // Send the command.
                            if (!sessionIt->second->send(rec.getPayload()))
                            {
                                it->second.erase(sessionIt);
                            }
                        }
                        else
                        {
                            std::cout << "ERROR: Session [" << rec.getSessionId() << "] does not exist.\n";
                        }
                    }
                    else
                    {
                        std::cout << "ERROR: Doc [" << uri << "] does not exist.\n";
                    }
                }
                else
                {
                    std::cout << "ERROR: Unknown PID [" << rec.getPid() << "] maps to no active document.\n";
                }
            }
            else
            {
                std::cout << "ERROR: Unknown trace file direction [" << static_cast<char>(rec.getDir()) << "].\n";
            }

            epochCurrent = std::chrono::steady_clock::now();
            epochFile = rec.getTimestampNs();
        }
    }

    const std::string& getServerUri() const { return _serverUri; }
    const std::string& getUri() const { return _uri; }

private:
    const std::string _serverUri;
    const std::string _uri;

    /// Should we ignore timing that is saved in the trace file?
    bool _ignoreTiming;

    /// LOK child process PID to Doc URI map.
    std::map<unsigned, std::string> _childToDoc;

    /// Doc URI to _sessions map. _sessions are maps of SessionID to Connection.
    std::map<std::string, std::map<std::string, std::shared_ptr<Connection>>> _sessions;
};

#endif

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */