2017-02-07 10:16:59 -06:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* 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/.
|
|
|
|
*/
|
|
|
|
|
2020-04-18 03:39:50 -05:00
|
|
|
#pragma once
|
2017-02-07 10:16:59 -06:00
|
|
|
|
2021-10-08 15:27:38 -05:00
|
|
|
#include <math.h>
|
|
|
|
#include <chrono>
|
|
|
|
#include <cstring>
|
2022-06-04 16:50:20 -05:00
|
|
|
#include <unordered_map>
|
2021-10-08 15:27:38 -05:00
|
|
|
|
|
|
|
#include "Socket.hpp"
|
|
|
|
#include "WebSocketHandler.hpp"
|
|
|
|
#include <net/Ssl.hpp>
|
|
|
|
#if ENABLE_SSL
|
|
|
|
# include <SslSocket.hpp>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <TraceFile.hpp>
|
|
|
|
#include <wsd/TileDesc.hpp>
|
|
|
|
|
|
|
|
// store buckets of latency
|
|
|
|
struct Histogram {
|
|
|
|
const size_t incLowMs = 10;
|
|
|
|
const size_t maxLowMs = incLowMs * 10;
|
|
|
|
const size_t incHighMs = 100;
|
|
|
|
const size_t maxHighMs = incHighMs * 10;
|
|
|
|
size_t _items;
|
|
|
|
size_t _tooLong;
|
|
|
|
std::vector<size_t> _buckets;
|
|
|
|
|
|
|
|
Histogram() : _items(0), _tooLong(0), _buckets(20)
|
|
|
|
{
|
|
|
|
}
|
2017-02-07 10:16:59 -06:00
|
|
|
|
2021-10-08 15:27:38 -05:00
|
|
|
void addTime(size_t ms)
|
|
|
|
{
|
|
|
|
if (ms < maxLowMs)
|
|
|
|
_buckets[ms/incLowMs]++;
|
|
|
|
else if (ms < maxHighMs)
|
|
|
|
_buckets[(ms - maxLowMs) / maxHighMs]++;
|
|
|
|
else
|
|
|
|
_tooLong++;
|
|
|
|
_items++;
|
|
|
|
}
|
2017-02-07 10:16:59 -06:00
|
|
|
|
2021-10-08 15:27:38 -05:00
|
|
|
void dump(const char *legend)
|
2017-02-07 10:16:59 -06:00
|
|
|
{
|
2021-10-08 15:27:38 -05:00
|
|
|
size_t max = 0;
|
|
|
|
ssize_t firstBucket = -1;
|
|
|
|
for (size_t i = 0; i < _buckets.size(); ++i)
|
2017-02-10 00:04:46 -06:00
|
|
|
{
|
2021-10-08 15:27:38 -05:00
|
|
|
size_t n = _buckets[i];
|
|
|
|
if (n > 0 && firstBucket < 0)
|
|
|
|
firstBucket = i;
|
|
|
|
max = std::max(max, n);
|
|
|
|
}
|
|
|
|
if (firstBucket < 0 || max == 0)
|
|
|
|
return;
|
2017-02-07 10:16:59 -06:00
|
|
|
|
2021-10-08 15:27:38 -05:00
|
|
|
size_t last; // ignore
|
|
|
|
for (last = _buckets.size()-1; last > 0; --last)
|
|
|
|
if (_buckets[last] > 0)
|
|
|
|
break;
|
2017-02-07 10:16:59 -06:00
|
|
|
|
2021-10-08 15:27:38 -05:00
|
|
|
std::cout << legend << " " << _items << " items, max #: " << max << " too long: " << _tooLong << "\n";
|
2017-02-07 10:16:59 -06:00
|
|
|
|
2021-10-08 15:27:38 -05:00
|
|
|
const double chrsPerFreq = 60.0 / max;
|
|
|
|
for (size_t i = firstBucket; i <= last; ++i)
|
2017-02-10 00:04:46 -06:00
|
|
|
{
|
2021-10-08 15:27:38 -05:00
|
|
|
int chrs = ::ceil(chrsPerFreq * _buckets[i]);
|
|
|
|
int ms = i < 10 ? (incLowMs * (i+1)) : (maxLowMs + (i+1-10) * incHighMs);
|
|
|
|
std::cout << "< " << std::setw(4) << ms << " ms |" << std::string(chrs, '-') << "| " << _buckets[i] << "\n";
|
2017-02-10 00:04:46 -06:00
|
|
|
}
|
2017-02-07 10:16:59 -06:00
|
|
|
}
|
2021-10-08 15:27:38 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Stats {
|
|
|
|
Stats() :
|
|
|
|
_start(std::chrono::steady_clock::now()),
|
2022-02-14 10:12:08 -06:00
|
|
|
_bytesSent(0),
|
|
|
|
_bytesRecvd(0),
|
2022-06-07 12:03:49 -05:00
|
|
|
_tileCount(0),
|
|
|
|
_connections(0)
|
2021-10-08 15:27:38 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
std::chrono::steady_clock::time_point _start;
|
2022-02-14 10:12:08 -06:00
|
|
|
size_t _bytesSent;
|
|
|
|
size_t _bytesRecvd;
|
2021-10-08 15:27:38 -05:00
|
|
|
size_t _tileCount;
|
2022-06-07 12:03:49 -05:00
|
|
|
size_t _connections;
|
2021-10-08 15:27:38 -05:00
|
|
|
Histogram _pingLatency;
|
|
|
|
Histogram _tileLatency;
|
2022-06-04 16:50:20 -05:00
|
|
|
|
|
|
|
// message size breakdown
|
|
|
|
struct MessageStat {
|
|
|
|
size_t size;
|
|
|
|
size_t count;
|
|
|
|
};
|
|
|
|
std::unordered_map<std::string, MessageStat> _recvd;
|
2022-06-07 12:03:49 -05:00
|
|
|
std::unordered_map<std::string, MessageStat> _sent;
|
2022-06-04 16:50:20 -05:00
|
|
|
|
2022-06-07 12:03:49 -05:00
|
|
|
void accumulate(std::unordered_map<std::string, MessageStat> &map,
|
|
|
|
const std::string token, size_t size)
|
|
|
|
{
|
|
|
|
auto it = map.find(token);
|
2022-06-04 16:50:20 -05:00
|
|
|
MessageStat st = { 0, 0 };
|
2022-06-07 12:03:49 -05:00
|
|
|
if (it != map.end())
|
2022-06-04 16:50:20 -05:00
|
|
|
st = it->second;
|
|
|
|
st.size += size;
|
|
|
|
st.count++;
|
2022-06-07 12:03:49 -05:00
|
|
|
map[token] = st;
|
2022-06-04 16:50:20 -05:00
|
|
|
}
|
|
|
|
|
2022-06-07 12:03:49 -05:00
|
|
|
void accumulateRecv(const std::string &token, size_t size)
|
2021-10-08 15:27:38 -05:00
|
|
|
{
|
2022-06-07 12:03:49 -05:00
|
|
|
_bytesRecvd += size;
|
|
|
|
accumulate(_recvd, token, size);
|
|
|
|
}
|
2022-06-04 16:50:20 -05:00
|
|
|
|
2022-06-07 12:03:49 -05:00
|
|
|
void accumulateSend(const char* msg, const size_t len, bool /* flush */)
|
|
|
|
{
|
|
|
|
_bytesSent += len;
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < len && msg[i] != ' '; ++i);
|
|
|
|
accumulate(_sent, std::string(msg, std::min(i, size_t(len))), len);
|
|
|
|
}
|
|
|
|
|
|
|
|
void addConnection() { _connections++; }
|
|
|
|
|
|
|
|
void dumpMap(std::unordered_map<std::string, MessageStat> &map)
|
|
|
|
{
|
2022-06-04 16:50:20 -05:00
|
|
|
// how much from each command ?
|
|
|
|
std::vector<std::string> sortKeys;
|
2022-06-07 12:03:49 -05:00
|
|
|
size_t total = 0;
|
2023-06-14 03:21:51 -05:00
|
|
|
for(const auto& it : map)
|
2022-06-07 12:03:49 -05:00
|
|
|
{
|
2022-06-04 16:50:20 -05:00
|
|
|
sortKeys.push_back(it.first);
|
2022-06-07 12:03:49 -05:00
|
|
|
total += it.second.size;
|
|
|
|
}
|
2022-06-04 16:50:20 -05:00
|
|
|
std::sort(sortKeys.begin(), sortKeys.end(),
|
|
|
|
[&](const std::string &a, const std::string &b)
|
2022-06-07 12:03:49 -05:00
|
|
|
{ return map[a].size > map[b].size; } );
|
2022-06-04 16:50:20 -05:00
|
|
|
std::cout << "size\tcount\tcommand\n";
|
|
|
|
for (auto it : sortKeys)
|
2022-06-07 12:03:49 -05:00
|
|
|
{
|
|
|
|
std::cout << map[it].size << "\t"
|
|
|
|
<< map[it].count << "\t" << it << "\n";
|
|
|
|
if (map[it].size < (total / 100))
|
|
|
|
break;
|
|
|
|
}
|
2021-10-08 15:27:38 -05:00
|
|
|
}
|
2022-06-04 16:50:20 -05:00
|
|
|
|
2022-06-07 12:03:49 -05:00
|
|
|
void dump()
|
|
|
|
{
|
|
|
|
const auto now = std::chrono::steady_clock::now();
|
|
|
|
const size_t runMs = std::chrono::duration_cast<std::chrono::milliseconds>(now - _start).count();
|
|
|
|
std::cout << "Stress run took " << runMs << " ms\n";
|
|
|
|
std::cout << " tiles: " << _tileCount << " => TPS: " << ((_tileCount * 1000.0)/runMs) << "\n";
|
|
|
|
_pingLatency.dump("ping latency:");
|
|
|
|
_tileLatency.dump("tile latency:");
|
|
|
|
size_t recvKbps = (_bytesRecvd * 1000) / (_connections * runMs * 1024);
|
|
|
|
size_t sentKbps = (_bytesSent * 1000) / (_connections * runMs * 1024);
|
|
|
|
std::cout << " we sent " << Util::getHumanizedBytes(_bytesSent) <<
|
|
|
|
" (" << sentKbps << " kB/s) " <<
|
|
|
|
" server sent " << Util::getHumanizedBytes(_bytesRecvd) <<
|
2022-06-08 14:53:23 -05:00
|
|
|
" (" << recvKbps << " kB/s) to " << _connections << " connections.\n";
|
2022-06-07 12:03:49 -05:00
|
|
|
|
|
|
|
std::cout << "we sent:\n";
|
|
|
|
dumpMap(_sent);
|
|
|
|
|
|
|
|
std::cout << "server sent us:\n";
|
|
|
|
dumpMap(_recvd);
|
|
|
|
}
|
2021-10-08 15:27:38 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// Avoid a MessageHandler for now.
|
|
|
|
class StressSocketHandler : public WebSocketHandler
|
|
|
|
{
|
2022-03-03 13:55:25 -06:00
|
|
|
SocketPoll &_poll;
|
2021-10-08 15:27:38 -05:00
|
|
|
TraceFileReader _reader;
|
|
|
|
TraceFileRecord _next;
|
|
|
|
std::chrono::steady_clock::time_point _start;
|
|
|
|
std::chrono::steady_clock::time_point _nextPing;
|
|
|
|
bool _connecting;
|
2022-03-02 11:53:23 -06:00
|
|
|
std::string _logPre;
|
2021-10-08 15:27:38 -05:00
|
|
|
std::string _uri;
|
|
|
|
std::string _trace;
|
2017-02-07 10:16:59 -06:00
|
|
|
|
2021-10-08 15:27:38 -05:00
|
|
|
std::shared_ptr<Stats> _stats;
|
|
|
|
std::chrono::steady_clock::time_point _lastTile;
|
2017-02-07 10:16:59 -06:00
|
|
|
|
2021-10-08 15:27:38 -05:00
|
|
|
public:
|
2022-03-03 13:55:25 -06:00
|
|
|
StressSocketHandler(SocketPoll &poll, /* bad style */
|
|
|
|
const std::shared_ptr<Stats> stats,
|
|
|
|
const std::string &uri, const std::string &trace,
|
|
|
|
const int delayMs = 0) :
|
2021-10-08 15:27:38 -05:00
|
|
|
WebSocketHandler(true, true),
|
2022-03-03 13:55:25 -06:00
|
|
|
_poll(poll),
|
2021-10-08 15:27:38 -05:00
|
|
|
_reader(trace),
|
|
|
|
_connecting(true),
|
|
|
|
_uri(uri),
|
|
|
|
_trace(trace),
|
|
|
|
_stats(stats)
|
|
|
|
{
|
2022-03-02 11:53:23 -06:00
|
|
|
static std::atomic<int> number;
|
|
|
|
_logPre = "[" + std::to_string(++number) + "] ";
|
2021-10-08 15:27:38 -05:00
|
|
|
std::cerr << "Attempt connect to " << uri << " for trace " << _trace << "\n";
|
|
|
|
getNextRecord();
|
2022-03-03 13:55:25 -06:00
|
|
|
_start = std::chrono::steady_clock::now() + std::chrono::milliseconds(delayMs);
|
2021-10-08 15:27:38 -05:00
|
|
|
_nextPing = _start + std::chrono::milliseconds((long)(std::rand() * 1000.0) / RAND_MAX);
|
|
|
|
_lastTile = _start;
|
|
|
|
}
|
|
|
|
|
|
|
|
void gotPing(WSOpCode /* code */, int pingTimeUs) override
|
|
|
|
{
|
|
|
|
if (_stats)
|
|
|
|
_stats->_pingLatency.addTime(pingTimeUs/1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
int getPollEvents(std::chrono::steady_clock::time_point now,
|
|
|
|
int64_t &timeoutMaxMicroS) override
|
2017-02-07 10:16:59 -06:00
|
|
|
{
|
2021-10-08 15:27:38 -05:00
|
|
|
if (_connecting)
|
2017-02-10 00:02:32 -06:00
|
|
|
{
|
2022-03-02 11:53:23 -06:00
|
|
|
std::cerr << _logPre << "Waiting for outbound connection to " << _uri <<
|
2021-10-08 15:27:38 -05:00
|
|
|
" to complete for trace " << _trace << "\n";
|
|
|
|
return POLLOUT;
|
2017-02-10 00:02:32 -06:00
|
|
|
}
|
2021-10-08 15:27:38 -05:00
|
|
|
|
|
|
|
int events = WebSocketHandler::getPollEvents(now, timeoutMaxMicroS);
|
|
|
|
|
|
|
|
if (now >= _nextPing)
|
2017-02-10 00:02:32 -06:00
|
|
|
{
|
2021-10-08 15:27:38 -05:00
|
|
|
// ping more frequently
|
|
|
|
sendPing(now, getSocket().lock());
|
|
|
|
_nextPing += std::chrono::seconds(1);
|
2017-02-10 00:02:32 -06:00
|
|
|
}
|
|
|
|
|
2021-10-08 15:27:38 -05:00
|
|
|
int64_t nextTime = -1;
|
|
|
|
while (nextTime <= 0) {
|
|
|
|
nextTime = std::chrono::duration_cast<std::chrono::microseconds>(
|
|
|
|
std::chrono::microseconds((_next.getTimestampUs() - _reader.getEpochStart()) * TRACE_MULTIPLIER)
|
|
|
|
+ _start - now).count();
|
|
|
|
if (nextTime <= 0)
|
|
|
|
{
|
|
|
|
sendTraceMessage();
|
|
|
|
events = WebSocketHandler::getPollEvents(now, timeoutMaxMicroS);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-02-07 10:16:59 -06:00
|
|
|
|
2021-10-08 15:27:38 -05:00
|
|
|
// std::cerr << "next event in " << nextTime << " us\n";
|
|
|
|
if (nextTime < timeoutMaxMicroS)
|
|
|
|
timeoutMaxMicroS = nextTime;
|
|
|
|
|
|
|
|
return events;
|
2017-02-07 10:16:59 -06:00
|
|
|
}
|
|
|
|
|
2021-10-08 15:27:38 -05:00
|
|
|
bool getNextRecord()
|
2017-02-07 10:16:59 -06:00
|
|
|
{
|
2021-10-08 15:27:38 -05:00
|
|
|
bool found = false;
|
|
|
|
while (!found) {
|
|
|
|
_next = _reader.getNextRecord();
|
|
|
|
switch (_next.getDir()) {
|
|
|
|
case TraceFileRecord::Direction::Invalid:
|
|
|
|
case TraceFileRecord::Direction::Incoming:
|
|
|
|
// FIXME: need to subset output quite a bit.
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
found = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return _next.getDir () != TraceFileRecord::Direction::Invalid;
|
2017-02-07 10:16:59 -06:00
|
|
|
}
|
|
|
|
|
2021-10-08 15:27:38 -05:00
|
|
|
void performWrites(std::size_t capacity) override
|
2017-02-07 10:16:59 -06:00
|
|
|
{
|
2021-10-08 15:27:38 -05:00
|
|
|
if (_connecting)
|
2022-03-02 11:53:23 -06:00
|
|
|
std::cerr << _logPre << "Outbound websocket - connected\n";
|
2021-10-08 15:27:38 -05:00
|
|
|
_connecting = false;
|
|
|
|
return WebSocketHandler::performWrites(capacity);
|
2017-02-07 10:16:59 -06:00
|
|
|
}
|
|
|
|
|
2021-10-08 15:27:38 -05:00
|
|
|
void onDisconnect() override
|
2017-02-07 10:16:59 -06:00
|
|
|
{
|
2022-03-02 11:53:23 -06:00
|
|
|
std::cerr << _logPre << "Websocket " << _uri <<
|
|
|
|
" dis-connected, re-trying in 20 seconds\n";
|
2021-10-08 15:27:38 -05:00
|
|
|
WebSocketHandler::onDisconnect();
|
2017-02-07 10:16:59 -06:00
|
|
|
}
|
|
|
|
|
2021-10-08 15:27:38 -05:00
|
|
|
// send outgoing messages
|
|
|
|
void sendTraceMessage()
|
2017-02-07 10:16:59 -06:00
|
|
|
{
|
2021-10-08 15:27:38 -05:00
|
|
|
if (_next.getDir() == TraceFileRecord::Direction::Invalid)
|
|
|
|
return; // shutting down
|
|
|
|
|
|
|
|
std::string msg = rewriteMessage(_next.getPayload());
|
|
|
|
if (!msg.empty())
|
2017-02-07 10:16:59 -06:00
|
|
|
{
|
2022-03-02 11:53:23 -06:00
|
|
|
std::cerr << _logPre << "Send: '" << msg << "'\n";
|
2021-10-08 15:27:38 -05:00
|
|
|
sendMessage(msg);
|
2017-02-07 10:16:59 -06:00
|
|
|
}
|
2021-10-08 15:27:38 -05:00
|
|
|
|
|
|
|
if (!getNextRecord())
|
2017-02-07 10:16:59 -06:00
|
|
|
{
|
2022-03-02 11:53:23 -06:00
|
|
|
std::cerr << _logPre << "Shutdown\n";
|
2021-10-08 15:27:38 -05:00
|
|
|
shutdown();
|
2017-02-07 10:16:59 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-08 15:27:38 -05:00
|
|
|
std::string rewriteMessage(const std::string &msg)
|
2017-02-07 10:16:59 -06:00
|
|
|
{
|
2021-11-18 06:08:14 -06:00
|
|
|
const std::string firstLine = COOLProtocol::getFirstLine(msg);
|
2022-03-29 20:37:57 -05:00
|
|
|
StringVector tokens = StringVector::tokenize(firstLine);
|
2017-02-07 10:16:59 -06:00
|
|
|
|
2021-10-08 15:27:38 -05:00
|
|
|
std::string out = msg;
|
2017-02-07 10:16:59 -06:00
|
|
|
|
2021-10-08 15:27:38 -05:00
|
|
|
if (tokens.equals(0, "tileprocessed"))
|
|
|
|
out = ""; // we do this accurately below
|
2017-02-09 23:54:52 -06:00
|
|
|
|
2021-10-08 15:27:38 -05:00
|
|
|
else if (tokens.equals(0, "load")) {
|
|
|
|
std::string url = tokens[1];
|
|
|
|
assert(!strncmp(url.c_str(), "url=", 4));
|
2017-02-07 10:16:59 -06:00
|
|
|
|
2021-10-08 15:27:38 -05:00
|
|
|
// load url=file%3A%2F%2F%2Ftmp%2Fhello-world.odt deviceFormFactor=desktop
|
|
|
|
out = "load url=" + _uri; // already encoded
|
|
|
|
for (size_t i = 2; i < tokens.size(); ++i)
|
|
|
|
out += " " + tokens[i];
|
2022-03-02 11:53:23 -06:00
|
|
|
std::cerr << _logPre << "msg " << out << "\n";
|
2021-10-08 15:27:38 -05:00
|
|
|
}
|
2017-02-07 10:16:59 -06:00
|
|
|
|
2021-10-08 15:27:38 -05:00
|
|
|
// FIXME: translate mouse events relative to view-port etc.
|
|
|
|
return out;
|
|
|
|
}
|
2017-02-07 10:16:59 -06:00
|
|
|
|
2021-10-08 15:27:38 -05:00
|
|
|
// handle incoming messages
|
|
|
|
void handleMessage(const std::vector<char> &data) override
|
|
|
|
{
|
|
|
|
const auto now = std::chrono::steady_clock::now();
|
2017-02-09 23:54:52 -06:00
|
|
|
|
2021-11-18 06:08:14 -06:00
|
|
|
const std::string firstLine = COOLProtocol::getFirstLine(data.data(), data.size());
|
2022-03-29 20:37:57 -05:00
|
|
|
StringVector tokens = StringVector::tokenize(firstLine);
|
2022-03-02 11:53:23 -06:00
|
|
|
std::cerr << _logPre << "Got msg: " << firstLine << "\n";
|
2021-10-08 15:27:38 -05:00
|
|
|
|
2022-06-07 12:03:49 -05:00
|
|
|
_stats->accumulateRecv(tokens[0], data.size());
|
2022-06-04 16:50:20 -05:00
|
|
|
|
2021-10-08 15:27:38 -05:00
|
|
|
if (tokens.equals(0, "tile:")) {
|
|
|
|
// accumulate latencies
|
|
|
|
if (_stats) {
|
|
|
|
_stats->_tileLatency.addTime(std::chrono::duration_cast<std::chrono::milliseconds>(now - _lastTile).count());
|
|
|
|
_stats->_tileCount++;
|
2017-02-10 00:05:34 -06:00
|
|
|
}
|
2021-10-08 15:27:38 -05:00
|
|
|
_lastTile = now;
|
2017-02-07 10:16:59 -06:00
|
|
|
|
2021-10-08 15:27:38 -05:00
|
|
|
// eg. tileprocessed tile=0:9216:0:3072:3072:0
|
|
|
|
TileDesc desc = TileDesc::parse(tokens);
|
2022-03-02 11:53:23 -06:00
|
|
|
|
2021-10-08 15:27:38 -05:00
|
|
|
sendMessage("tileprocessed tile=" + desc.generateID());
|
2022-03-02 11:53:23 -06:00
|
|
|
std::cerr << _logPre << "Sent tileprocessed tile= " + desc.generateID() << "\n";
|
2022-02-19 16:13:53 -06:00
|
|
|
} if (tokens.equals(0, "error:")) {
|
2022-03-03 13:55:25 -06:00
|
|
|
|
|
|
|
bool reconnect = false;
|
|
|
|
if (firstLine == "error: cmd=load kind=docunloading")
|
|
|
|
{
|
|
|
|
std::cerr << ": wait and try again later ...!\n";
|
|
|
|
reconnect = true;
|
|
|
|
}
|
|
|
|
else if (firstLine == "error: cmd=storage kind=documentconflict")
|
|
|
|
{
|
|
|
|
std::cerr << "Document conflict - need to resolve it first ...\n";
|
|
|
|
sendMessage("closedocument");
|
|
|
|
reconnect = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << _logPre << "Error while processing " << _uri
|
|
|
|
<< " and trace " << _trace << ":\n"
|
|
|
|
<< "'" << firstLine << "'\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reconnect)
|
|
|
|
{
|
|
|
|
shutdown(true, "bye");
|
|
|
|
auto handler = std::make_shared<StressSocketHandler>(
|
|
|
|
_poll, _stats, _uri, _trace, 1000 /* delay 1 second */);
|
|
|
|
_poll.insertNewWebSocketSync(Poco::URI(_uri), handler);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
2022-07-15 22:18:48 -05:00
|
|
|
Util::forcedExit(EX_SOFTWARE);
|
2017-02-07 10:16:59 -06:00
|
|
|
}
|
|
|
|
|
2021-10-08 15:27:38 -05:00
|
|
|
// FIXME: implement code to send new view-ports based
|
|
|
|
// on cursor position etc.
|
|
|
|
}
|
2017-02-07 10:16:59 -06:00
|
|
|
|
2022-02-14 10:12:08 -06:00
|
|
|
/// override ProtocolHandlerInterface piece
|
|
|
|
int sendTextMessage(const char* msg, const size_t len, bool flush = false) const override
|
|
|
|
{
|
2022-06-07 12:03:49 -05:00
|
|
|
_stats->accumulateSend(msg, len, flush);
|
2022-02-14 10:12:08 -06:00
|
|
|
return WebSocketHandler::sendTextMessage(msg, len, flush);
|
|
|
|
}
|
|
|
|
|
2021-10-08 15:27:38 -05:00
|
|
|
static void addPollFor(SocketPoll &poll, const std::string &server,
|
|
|
|
const std::string &filePath, const std::string &tracePath,
|
|
|
|
const std::shared_ptr<Stats> &optStats = nullptr)
|
|
|
|
{
|
|
|
|
std::string file, wrap;
|
|
|
|
std::string fileabs = Poco::Path(filePath).makeAbsolute().toString();
|
|
|
|
Poco::URI::encode("file://" + fileabs, ":/?", file);
|
|
|
|
Poco::URI::encode(file, ":/?", wrap); // double encode.
|
2021-11-15 09:37:59 -06:00
|
|
|
std::string uri = server + "/cool/" + wrap + "/ws";
|
2021-10-08 15:27:38 -05:00
|
|
|
|
2022-03-03 13:55:25 -06:00
|
|
|
auto handler = std::make_shared<StressSocketHandler>(poll, optStats, file, tracePath);
|
2021-10-08 15:27:38 -05:00
|
|
|
poll.insertNewWebSocketSync(Poco::URI(uri), handler);
|
2022-06-07 12:03:49 -05:00
|
|
|
|
|
|
|
if (optStats)
|
|
|
|
optStats->addConnection();
|
2021-10-08 15:27:38 -05:00
|
|
|
}
|
2017-02-07 10:16:59 -06:00
|
|
|
|
2021-10-08 15:27:38 -05:00
|
|
|
/// Attach to @server, load @filePath and replace @tracePath
|
|
|
|
static void replaySync(const std::string &server,
|
|
|
|
const std::string &filePath,
|
|
|
|
const std::string &tracePath)
|
|
|
|
{
|
|
|
|
TerminatingPoll poll("replay");
|
2017-02-07 10:16:59 -06:00
|
|
|
|
2021-10-08 15:27:38 -05:00
|
|
|
addPollFor(poll, server, filePath, tracePath);
|
|
|
|
do {
|
|
|
|
poll.poll(TerminatingPoll::DefaultPollTimeoutMicroS);
|
|
|
|
} while (poll.continuePolling() && poll.getSocketCount() > 0);
|
|
|
|
}
|
2017-02-07 10:16:59 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|