[Libreoffice-commits] online.git: Branch 'private/Ashod/nonblocking' - net/clientnb.cpp

2017-02-24 Thread Michael Meeks
 net/clientnb.cpp |6 +-
 1 file changed, 5 insertions(+), 1 deletion(-)

New commits:
commit cb76f20350f83f706e8d83741e4acb4817ab2800
Author: Michael Meeks 
Date:   Fri Feb 24 22:18:38 2017 +

Use pseudo-random-ness in test to make strace cleaner.

diff --git a/net/clientnb.cpp b/net/clientnb.cpp
index 0f7c796..311491e 100644
--- a/net/clientnb.cpp
+++ b/net/clientnb.cpp
@@ -222,9 +222,13 @@ struct Client : public Poco::Util::Application
 std::shared_ptr ws = session.getWebSocket();
 
 std::vector res;
+
+std::srand(std::time(0));
+
+std::vector data;
 for (size_t i = 1; i < (1 << 14); ++i)
 {
-const std::vector data = Util::rng::getBytes(i);
+data.push_back((char)(std::rand() / (RAND_MAX/256)));
 ws->sendFrame(data.data(), data.size(), 
WebSocket::SendFlags::FRAME_BINARY);
 
 res.resize(i);
___
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits


[Libreoffice-commits] online.git: Branch 'private/Ashod/nonblocking' - net/clientnb.cpp net/loolnb.cpp

2017-02-19 Thread Ashod Nakashian
 net/clientnb.cpp |1 -
 net/loolnb.cpp   |1 -
 2 files changed, 2 deletions(-)

New commits:
commit a3aba8270ca0321a9accf740b9e9c26fb4c0dab4
Author: Ashod Nakashian 
Date:   Sun Feb 19 21:23:57 2017 -0500

nb: cleanup some logging

Change-Id: I28e211e5a727ea308824651f417a91d7b388a30d
Reviewed-on: https://gerrit.libreoffice.org/34452
Reviewed-by: Ashod Nakashian 
Tested-by: Ashod Nakashian 

diff --git a/net/clientnb.cpp b/net/clientnb.cpp
index 5467fe9..0f7c796 100644
--- a/net/clientnb.cpp
+++ b/net/clientnb.cpp
@@ -224,7 +224,6 @@ struct Client : public Poco::Util::Application
 std::vector res;
 for (size_t i = 1; i < (1 << 14); ++i)
 {
-std::cerr << "\n" << i;
 const std::vector data = Util::rng::getBytes(i);
 ws->sendFrame(data.data(), data.size(), 
WebSocket::SendFlags::FRAME_BINARY);
 
diff --git a/net/loolnb.cpp b/net/loolnb.cpp
index e088001..c6abe72 100644
--- a/net/loolnb.cpp
+++ b/net/loolnb.cpp
@@ -273,7 +273,6 @@ public:
 reply.insert(reply.end(), data.begin(), data.end());
 }
 
-std::cerr << "reply: " << reply.size() << std::endl;
 queueWSMessage(reply);
 }
 
___
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits


[Libreoffice-commits] online.git: Branch 'private/Ashod/nonblocking' - net/clientnb.cpp

2017-02-18 Thread Ashod Nakashian
 net/clientnb.cpp |   24 +++-
 1 file changed, 15 insertions(+), 9 deletions(-)

New commits:
commit fe1fce20cb813a59771e7ffad11e0b77c861bde9
Author: Ashod Nakashian 
Date:   Sat Feb 18 14:49:46 2017 -0500

nb: refactor getResponse into raw string and int versions

Change-Id: I77d8085cc811c7caf4434af823e62fd1333090f1
Reviewed-on: https://gerrit.libreoffice.org/34412
Reviewed-by: Ashod Nakashian 
Tested-by: Ashod Nakashian 

diff --git a/net/clientnb.cpp b/net/clientnb.cpp
index 9877d66..7aadaa1 100644
--- a/net/clientnb.cpp
+++ b/net/clientnb.cpp
@@ -91,17 +91,23 @@ struct Session
 throw;
 }
 }
-int getResponse()
+
+std::string getResponseString()
 {
-int number = 42;
 Poco::Net::HTTPResponse response;
+std::istream& responseStream = _session->receiveResponse(response);
+const std::string 
result(std::istreambuf_iterator(responseStream), {});
+// std::cerr << "Got response '" << result << "'\n";
+return result;
+}
+
+int getResponseInt()
+{
+int number = 42;
 
 try {
 //std::cerr << "try to get response\n";
-std::istream& responseStream = _session->receiveResponse(response);
-
-std::string result(std::istreambuf_iterator(responseStream), 
{});
-//std::cerr << "Got response '" << result << "'\n";
+const std::string result = getResponseString();
 number = std::stoi(result);
 }
 catch (const Poco::Exception )
@@ -136,7 +142,7 @@ struct ThreadWorker : public Runnable
 {
 Session ping(_domain ? _domain : "init", EnableHttps);
 ping.sendPing(i);
-int back = ping.getResponse();
+int back = ping.getResponseInt();
 assert(back == i + 1);
 }
 }
@@ -154,11 +160,11 @@ struct Client : public Poco::Util::Application
 first.sendPing(count);
 second.sendPing(count + 1);
 
-back = first.getResponse();
+back = first.getResponseInt();
 std::cerr << "testPing: " << back << "\n";
 assert (back == count + 1);
 
-back = second.getResponse();
+back = second.getResponseInt();
 std::cerr << "testPing: " << back << "\n";
 assert (back == count + 2);
 }
___
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits


[Libreoffice-commits] online.git: Branch 'private/Ashod/nonblocking' - net/clientnb.cpp net/socket.hpp

2017-02-18 Thread Ashod Nakashian
 net/clientnb.cpp |7 ---
 net/socket.hpp   |   20 ++--
 2 files changed, 10 insertions(+), 17 deletions(-)

New commits:
commit 7c17d4b0d66e6a6ebdce3dab86cf9b3cc568461b
Author: Ashod Nakashian 
Date:   Sat Feb 18 13:01:43 2017 -0500

nb: cosmetics

Change-Id: I3a9f95c0b8edacff69b3c3733e833e3a82c545a9
Reviewed-on: https://gerrit.libreoffice.org/34410
Reviewed-by: Ashod Nakashian 
Tested-by: Ashod Nakashian 

diff --git a/net/clientnb.cpp b/net/clientnb.cpp
index 40ddc2f..9877d66 100644
--- a/net/clientnb.cpp
+++ b/net/clientnb.cpp
@@ -188,6 +188,7 @@ struct Client : public Poco::Util::Application
 
 void testWebsocket()
 {
+std::cerr << "testwebsocket\n";
 Session session("ws", EnableHttps);
 std::shared_ptr ws = session.getWebSocket();
 
@@ -218,9 +219,9 @@ public:
 // Just accept the certificate anyway for testing purposes
 Poco::SharedPtr 
invalidCertHandler = new Poco::Net::AcceptCertificateHandler(false);
 
-Poco::Net::Context::Params sslParams;
-Poco::Net::Context::Ptr sslContext = new 
Poco::Net::Context(Poco::Net::Context::CLIENT_USE, sslParams);
-Poco::Net::SSLManager::instance().initializeClient(nullptr, 
invalidCertHandler, sslContext);
+Poco::Net::Context::Params sslParams;
+Poco::Net::Context::Ptr sslContext = new 
Poco::Net::Context(Poco::Net::Context::CLIENT_USE, sslParams);
+Poco::Net::SSLManager::instance().initializeClient(nullptr, 
invalidCertHandler, sslContext);
 }
 
 testWebsocket();
diff --git a/net/socket.hpp b/net/socket.hpp
index 3f43793..0eec7f0 100644
--- a/net/socket.hpp
+++ b/net/socket.hpp
@@ -47,10 +47,9 @@ public:
 virtual int getPollEvents() = 0;
 
 /// Handle results of events returned from poll
-enum HandleResult { CONTINUE, SOCKET_CLOSED };
+enum class HandleResult { CONTINUE, SOCKET_CLOSED };
 virtual HandleResult handlePoll( int events ) = 0;
 
-
 /// manage latency issues around packet aggregation
 void setNoDelay(bool noDelay = true)
 {
@@ -294,12 +293,7 @@ private:
 class BufferingSocket : public Socket
 {
 public:
-BufferingSocket() :
-Socket()
-{
-}
-
-HandleResult handlePoll( int events ) override
+HandleResult handlePoll(const int events) override
 {
 bool closeSocket = false;
 
@@ -325,10 +319,8 @@ public:
 
 int getPollEvents() override
 {
-int pollFor = POLLIN;
-if (_outBuffer.size() > 0)
-pollFor |= POLLOUT;
-return pollFor;
+// Only poll for read we if we have nothing to write.
+return (_outBuffer.empty() ? POLLIN : POLLIN | POLLOUT);
 }
 
 /// Override to handle read data.
@@ -392,7 +384,7 @@ protected:
 template friend class ServerSocket;
 };
 
-/// A SSL/TSL, non-blocking, data streaming socket.
+/// An SSL/TSL, non-blocking, data streaming socket.
 class SslStreamSocket : public BufferingSocket
 {
 public:
@@ -581,7 +573,7 @@ private:
 return rc;
 }
 
-// fallthrough
+// Fallthrough...
 default:
 {
 // The error is comming from BIO. Find out what happened.
___
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits


[Libreoffice-commits] online.git: Branch 'private/Ashod/nonblocking' - net/clientnb.cpp

2017-02-17 Thread Michael Meeks
 net/clientnb.cpp |   11 +++
 1 file changed, 11 insertions(+)

New commits:
commit 6ac450cb2322cfb4550553e4ccb1fb5a7d0f2bbb
Author: Michael Meeks 
Date:   Sat Feb 18 02:09:43 2017 +

SSL - accept self-signed certificates for tests.

diff --git a/net/clientnb.cpp b/net/clientnb.cpp
index 275938d..40ddc2f 100644
--- a/net/clientnb.cpp
+++ b/net/clientnb.cpp
@@ -212,6 +212,17 @@ public:
 EnableHttps = (args.size() > 0 && args[0] == "ssl");
 std::cerr << "Starting " << (EnableHttps ? "HTTPS" : "HTTP") << " 
client." << std::endl;
 
+if (EnableHttps)
+{
+Poco::Net::initializeSSL();
+// Just accept the certificate anyway for testing purposes
+Poco::SharedPtr 
invalidCertHandler = new Poco::Net::AcceptCertificateHandler(false);
+
+Poco::Net::Context::Params sslParams;
+Poco::Net::Context::Ptr sslContext = new 
Poco::Net::Context(Poco::Net::Context::CLIENT_USE, sslParams);
+Poco::Net::SSLManager::instance().initializeClient(nullptr, 
invalidCertHandler, sslContext);
+}
+
 testWebsocket();
 
 testPing();
___
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits


[Libreoffice-commits] online.git: Branch 'private/Ashod/nonblocking' - net/clientnb.cpp net/loolnb.cpp

2017-02-17 Thread Michael Meeks
 net/clientnb.cpp |   35 ++
 net/loolnb.cpp   |   56 ---
 2 files changed, 56 insertions(+), 35 deletions(-)

New commits:
commit 84be891579089b29070c1506531aec004d69414e
Author: Michael Meeks 
Date:   Sat Feb 18 00:58:49 2017 +

WS: cleanup calculation, masking, short reads and other bits.

diff --git a/net/clientnb.cpp b/net/clientnb.cpp
index 96fdc80..2dc7950 100644
--- a/net/clientnb.cpp
+++ b/net/clientnb.cpp
@@ -167,6 +167,11 @@ struct Client : public Poco::Util::Application
 {
 Session session("ws");
 std::shared_ptr ws = session.getWebSocket();
+
+std::string send = "hello there";
+ws->sendFrame([0], send.length(),
+  WebSocket::SendFlags::FRAME_TEXT);
+
 for (size_t i = 0; i < 10; i++)
 {
 ws->sendFrame(, sizeof(i), WebSocket::SendFlags::FRAME_BINARY);
@@ -184,26 +189,24 @@ public:
 const bool https = (args.size() > 0 && args[0] == "ssl");
 std::cerr << "Starting " << (https ? "HTTPS" : "HTTP") << " client." 
<< std::endl;
 
-if (getenv("WS"))
-testWebsocket();
-else
-{
-Session first("init", https);
-Session second("init", https);
+testWebsocket();
 
-int count = 42, back;
-first.sendPing(count);
-second.sendPing(count + 1);
+Session first("init");
+Session second("init");
 
-back = first.getResponse();
-assert (back == count + 1);
+int count = 42, back;
+first.sendPing(count);
+second.sendPing(count + 1);
 
-back = second.getResponse();
-assert (back == count + 2);
+back = first.getResponse();
+assert (back == count + 1);
+
+back = second.getResponse();
+assert (back == count + 2);
+
+testLadder();
+testParallel();
 
-testLadder();
-testParallel();
-}
 return 0;
 }
 };
diff --git a/net/loolnb.cpp b/net/loolnb.cpp
index 2b5773c..b91e4d2 100644
--- a/net/loolnb.cpp
+++ b/net/loolnb.cpp
@@ -137,50 +137,59 @@ public:
 // websocket fun !
 size_t len = T::_inBuffer.size();
 char *p = ::_inBuffer[0];
-char *data, *mask;
 if (len < 2) // partial read
 return;
 
-bool fin = *p & 0x80;
-WSOpCode code = static_cast(*p & 0x0f);
-p++;
-bool hasMask = *p & 0x80;
-size_t payloadLen = *p & 0x7f;
-p++;
+bool fin = p[0] & 0x80;
+WSOpCode code = static_cast(p[0] & 0x0f);
+bool hasMask = p[1] & 0x80;
+size_t payloadLen = p[1] & 0x7f;
+size_t headerLen = 2;
 
+// normally - 7 bit length.
 if (payloadLen == 126) // 2 byte length
 {
 if (len < 2 + 2)
 return;
 std::cerr << "Implement me 2 byte\n";
-data = p + 2;
-len -= 2;
+headerLen += 2;
 }
 else if (payloadLen == 127) // 8 byte length
 {
 if (len < 2 + 8)
 return;
 std::cerr << "Implement me 8 byte\n";
-data = p + 8;
-len -= 8;
+// FIXME: crop read length to remove top / sign bits.
+headerLen += 8;
 }
-else
+
+char *data, *mask;
+
+if (hasMask)
 {
-data = p;
+mask = p + headerLen;
+headerLen += 4;
+}
+
+if (payloadLen + headerLen > len)
+{ // partial read wait for more data.
+return;
 }
 
+data = p + headerLen;
+
 if (hasMask)
 {
-mask = data;
-data += 4;
-len -= 4;
 for (size_t i = 0; i < len; ++i)
 data[i] = data[i] ^ mask[i % 4];
 
 // FIXME: copy and un-mask at the same time ...
-_wsPayload.insert(_wsPayload.end(), data, data + 
std::min(payloadLen, len));
+_wsPayload.insert(_wsPayload.end(), data, data + payloadLen);
 } else
-_wsPayload.insert(_wsPayload.end(), data, data + 
std::min(payloadLen, len));
+_wsPayload.insert(_wsPayload.end(), data, data + payloadLen);
+
+T::_inBuffer.erase(T::_inBuffer.begin(), T::_inBuffer.begin() + 
headerLen + payloadLen);
+
 // FIXME: fin, aggregating payloads into _wsPayload etc.
 handleWSMessage(fin, code, _wsPayload);
 _wsPayload.clear();
@@ -225,7 +234,16 @@ public:
 
 virtual void handleWSMessage( bool fin, WSOpCode code, std::vector 
)
 {
-std::cerr << "Message: fin? " << fin << " code " << code << " data 
size " << data.size() << "\n";
+std::cerr << "Message: fin? " << fin << " code " << code << " data 
size " << data.size();
+if (code == 

[Libreoffice-commits] online.git: Branch 'private/Ashod/nonblocking' - net/clientnb.cpp net/socket.hpp

2017-02-17 Thread Ashod Nakashian
 net/clientnb.cpp |   16 ++--
 net/socket.hpp   |2 +-
 2 files changed, 11 insertions(+), 7 deletions(-)

New commits:
commit 88b3c174ef800d435550495e44952d285f4e9f49
Author: Ashod Nakashian 
Date:   Fri Feb 17 18:55:10 2017 -0500

nb: support SSL in clientnb

Change-Id: Ia895558c7115a39824474ba7acff060102192c10
Reviewed-on: https://gerrit.libreoffice.org/34394
Reviewed-by: Ashod Nakashian 
Tested-by: Ashod Nakashian 

diff --git a/net/clientnb.cpp b/net/clientnb.cpp
index 2ab1980..96fdc80 100644
--- a/net/clientnb.cpp
+++ b/net/clientnb.cpp
@@ -50,7 +50,8 @@ using Poco::Util::Option;
 using Poco::Util::OptionSet;
 
 const char *HostName = "127.0.0.1";
-constexpr int PortNumber = 9191;
+constexpr int HttpPortNumber = 9191;
+constexpr int SslPortNumber = 9193;
 
 struct Session
 {
@@ -61,9 +62,9 @@ struct Session
 : _session_name(session_name)
 {
 if (https)
-_session = new Poco::Net::HTTPSClientSession(HostName, PortNumber);
+_session = new Poco::Net::HTTPSClientSession(HostName, 
SslPortNumber);
 else
-_session = new Poco::Net::HTTPClientSession(HostName, PortNumber);
+_session = new Poco::Net::HTTPClientSession(HostName, 
HttpPortNumber);
 }
 ~Session()
 {
@@ -178,14 +179,17 @@ struct Client : public Poco::Util::Application
 }
 
 public:
-int main(const std::vector& /* args */) override
+int main(const std::vector& args) override
 {
+const bool https = (args.size() > 0 && args[0] == "ssl");
+std::cerr << "Starting " << (https ? "HTTPS" : "HTTP") << " client." 
<< std::endl;
+
 if (getenv("WS"))
 testWebsocket();
 else
 {
-Session first("init");
-Session second("init");
+Session first("init", https);
+Session second("init", https);
 
 int count = 42, back;
 first.sendPing(count);
diff --git a/net/socket.hpp b/net/socket.hpp
index fae37a2..69d2710 100644
--- a/net/socket.hpp
+++ b/net/socket.hpp
@@ -184,7 +184,7 @@ public:
 if (_pollSockets[i]->handlePoll(_pollFds[i].revents) ==
 Socket::HandleResult::SOCKET_CLOSED)
 {
-std::cout << "Removing: " << _pollFds[i].fd << std::endl;
+std::cout << "Removing client #" << _pollFds[i].fd << 
std::endl;
 _pollSockets.erase(_pollSockets.begin() + i);
 // Don't remove from pollFds; we'll recreate below.
 }
___
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits


[Libreoffice-commits] online.git: Branch 'private/Ashod/nonblocking' - net/clientnb.cpp net/loolnb.cpp

2017-02-16 Thread Michael Meeks
 net/clientnb.cpp |   54 ++
 net/loolnb.cpp   |   14 --
 2 files changed, 46 insertions(+), 22 deletions(-)

New commits:
commit dc52b96aa756198e75d0fe85a7bbb8db1acf7496
Author: Michael Meeks 
Date:   Thu Feb 16 14:14:12 2017 +

Websocket client test with WS=1

diff --git a/net/clientnb.cpp b/net/clientnb.cpp
index aa123e9..de6576c 100644
--- a/net/clientnb.cpp
+++ b/net/clientnb.cpp
@@ -25,6 +25,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -39,6 +40,7 @@
 using Poco::Net::HTTPClientSession;
 using Poco::Net::HTTPRequest;
 using Poco::Net::HTTPResponse;
+using Poco::Net::WebSocket;
 using Poco::Runnable;
 using Poco::Thread;
 using Poco::URI;
@@ -107,6 +109,15 @@ struct Session
 }
 return number;
 }
+
+std::shared_ptr getWebSocket()
+{
+_session->setTimeout(Poco::Timespan(10, 0));
+HTTPRequest request(HTTPRequest::HTTP_GET, "/ws");
+HTTPResponse response;
+return std::shared_ptr(
+new WebSocket(*_session, request, response));
+}
 };
 
 struct ThreadWorker : public Runnable
@@ -151,25 +162,44 @@ struct Client : public Poco::Util::Application
 snakes[i].join();
 }
 
+void testWebsocket()
+{
+Session session("ws");
+std::shared_ptr ws = session.getWebSocket();
+for (size_t i = 0; i < 10; i++)
+{
+ws->sendFrame(, sizeof(i), WebSocket::SendFlags::FRAME_BINARY);
+size_t back[5];
+int flags = 0;
+int recvd = ws->receiveFrame((void *)back, sizeof(back), flags);
+assert(recvd == sizeof(size_t));
+assert(back[0] == i + 1);
+}
+}
+
 public:
 int main(const std::vector& /* args */) override
 {
-Session first("init");
-Session second("init");
-
-int count = 42, back;
-first.sendPing(count);
-second.sendPing(count + 1);
+if (getenv("WS"))
+testWebsocket();
+else
+{
+Session first("init");
+Session second("init");
 
-back = first.getResponse();
-assert (back == count + 1);
+int count = 42, back;
+first.sendPing(count);
+second.sendPing(count + 1);
 
-back = second.getResponse();
-assert (back == count + 2);
+back = first.getResponse();
+assert (back == count + 1);
 
-testLadder();
-testParallel();
+back = second.getResponse();
+assert (back == count + 2);
 
+testLadder();
+testParallel();
+}
 return 0;
 }
 };
diff --git a/net/loolnb.cpp b/net/loolnb.cpp
index 5c61a78..01fc3c5 100644
--- a/net/loolnb.cpp
+++ b/net/loolnb.cpp
@@ -210,15 +210,6 @@ void server(SocketPoll& clientPoller)
 }
 }
 
-/// Poll client sockets and do IO.
-void pollAndComm(SocketPoll& poller, std::atomic& stop)
-{
-while (!stop)
-{
-poller.poll(5000);
-}
-}
-
 int main(int, const char**)
 {
 // Used to poll client sockets.
@@ -227,7 +218,10 @@ int main(int, const char**)
 // Start the client polling thread.
 Thread threadPoll([](std::atomic& stop)
 {
-pollAndComm(poller, stop);
+while (!stop)
+{
+poller.poll(5000);
+}
 });
 
 // Start the server.
___
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits


[Libreoffice-commits] online.git: Branch 'private/Ashod/nonblocking' - net/clientnb.cpp net/loolnb.cpp

2017-02-16 Thread Michael Meeks
 net/clientnb.cpp |   26 ++
 net/loolnb.cpp   |4 
 2 files changed, 26 insertions(+), 4 deletions(-)

New commits:
commit 1a9941c23fa538f7d0e8e5622169f8186cc6d37f
Author: Michael Meeks 
Date:   Thu Feb 16 11:52:22 2017 +

Bang on the server with some threads.

diff --git a/net/clientnb.cpp b/net/clientnb.cpp
index 822804d..aa123e9 100644
--- a/net/clientnb.cpp
+++ b/net/clientnb.cpp
@@ -28,7 +28,6 @@
 #include 
 #include 
 #include 
-#include 
 #include 
 #include 
 #include 
@@ -64,6 +63,11 @@ struct Session
 else
 _session = new Poco::Net::HTTPClientSession(HostName, PortNumber);
 }
+~Session()
+{
+delete _session;
+}
+
 void sendPing(int i)
 {
 Poco::Net::HTTPRequest request(
@@ -108,7 +112,7 @@ struct Session
 struct ThreadWorker : public Runnable
 {
 const char *_domain;
-   ThreadWorker (const char *domain)
+   ThreadWorker (const char *domain = NULL)
 : _domain(domain)
 {
 }
@@ -116,7 +120,7 @@ struct ThreadWorker : public Runnable
 {
 for (int i = 0; i < 100; ++i)
 {
-Session ping(_domain);
+Session ping(_domain ? _domain : "init");
 ping.sendPing(i);
 int back = ping.getResponse();
 assert(back == i + 1);
@@ -128,12 +132,25 @@ struct Client : public Poco::Util::Application
 {
 void testLadder()
 {
-ThreadWorker ladder("init");
+ThreadWorker ladder;
 Thread thread;
 thread.start(ladder);
 thread.join();
 }
 
+void testParallel()
+{
+const int num = 10;
+Thread snakes[num];
+ThreadWorker ladders[num];
+
+for (size_t i = 0; i < num; i++)
+snakes[i].start(ladders[i]);
+
+for (int i = 0; i < num; i++)
+snakes[i].join();
+}
+
 public:
 int main(const std::vector& /* args */) override
 {
@@ -151,6 +168,7 @@ public:
 assert (back == count + 2);
 
 testLadder();
+testParallel();
 
 return 0;
 }
diff --git a/net/loolnb.cpp b/net/loolnb.cpp
index 7c4f23f..5c61a78 100644
--- a/net/loolnb.cpp
+++ b/net/loolnb.cpp
@@ -22,6 +22,8 @@
 #include 
 #include 
 #include 
+#include 
+#include 
 
 using Poco::MemoryInputStream;
 using Poco::StringTokenizer;
@@ -75,6 +77,8 @@ public:
 }
 };
 
+// FIXME: use Poco Thread instead (?)
+
 /// Generic thread class.
 class Thread
 {
___
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits


[Libreoffice-commits] online.git: Branch 'private/Ashod/nonblocking' - net/clientnb.cpp

2017-02-16 Thread Michael Meeks
 net/clientnb.cpp |   35 +--
 1 file changed, 33 insertions(+), 2 deletions(-)

New commits:
commit 0c39cd017feb544f14acb253f6c3788ad435
Author: Michael Meeks 
Date:   Thu Feb 16 11:43:30 2017 +

Thread to poke server.

diff --git a/net/clientnb.cpp b/net/clientnb.cpp
index 87ee8b5..822804d 100644
--- a/net/clientnb.cpp
+++ b/net/clientnb.cpp
@@ -34,6 +34,8 @@
 #include 
 #include 
 #include 
+#include 
+#include 
 
 using Poco::Net::HTTPClientSession;
 using Poco::Net::HTTPRequest;
@@ -86,11 +88,11 @@ struct Session
 Poco::Net::HTTPResponse response;
 
 try {
-std::cerr << "try to get response\n";
+//std::cerr << "try to get response\n";
 std::istream& responseStream = _session->receiveResponse(response);
 
 std::string result(std::istreambuf_iterator(responseStream), 
{});
-std::cerr << "Got response '" << result << "'\n";
+//std::cerr << "Got response '" << result << "'\n";
 number = std::stoi(result);
 }
 catch (const Poco::Exception )
@@ -103,8 +105,35 @@ struct Session
 }
 };
 
+struct ThreadWorker : public Runnable
+{
+const char *_domain;
+   ThreadWorker (const char *domain)
+: _domain(domain)
+{
+}
+   virtual void run()
+{
+for (int i = 0; i < 100; ++i)
+{
+Session ping(_domain);
+ping.sendPing(i);
+int back = ping.getResponse();
+assert(back == i + 1);
+}
+   }
+};
+
 struct Client : public Poco::Util::Application
 {
+void testLadder()
+{
+ThreadWorker ladder("init");
+Thread thread;
+thread.start(ladder);
+thread.join();
+}
+
 public:
 int main(const std::vector& /* args */) override
 {
@@ -121,6 +150,8 @@ public:
 back = second.getResponse();
 assert (back == count + 2);
 
+testLadder();
+
 return 0;
 }
 };
___
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits


[Libreoffice-commits] online.git: Branch 'private/Ashod/nonblocking' - net/clientnb.cpp net/loolnb.cpp

2017-02-16 Thread Michael Meeks
 net/clientnb.cpp |3 ++-
 net/loolnb.cpp   |7 +++
 2 files changed, 5 insertions(+), 5 deletions(-)

New commits:
commit de41fffde6cfeaebdc85a060e2ed8ef92e539a85
Author: Michael Meeks 
Date:   Thu Feb 16 11:24:07 2017 +

Get number response / ping bits working.

diff --git a/net/clientnb.cpp b/net/clientnb.cpp
index a8ec054..87ee8b5 100644
--- a/net/clientnb.cpp
+++ b/net/clientnb.cpp
@@ -91,6 +91,7 @@ struct Session
 
 std::string result(std::istreambuf_iterator(responseStream), 
{});
 std::cerr << "Got response '" << result << "'\n";
+number = std::stoi(result);
 }
 catch (const Poco::Exception )
 {
@@ -118,7 +119,7 @@ public:
 assert (back == count + 1);
 
 back = second.getResponse();
-assert (back == count + 1);
+assert (back == count + 2);
 
 return 0;
 }
diff --git a/net/loolnb.cpp b/net/loolnb.cpp
index 963a631..7c4f23f 100644
--- a/net/loolnb.cpp
+++ b/net/loolnb.cpp
@@ -58,20 +58,19 @@ public:
 // complex algorithmic core:
 number = number + 1;
 
+std::string numberString = std::to_string(number);
 std::ostringstream oss;
 oss << "HTTP/1.1 200 OK\r\n"
 << "Date: Once, Upon a time GMT\r\n" // Mon, 27 Jul 2009 12:28:53 
GMT
 << "Server: madeup string (Linux)\r\n"
-<< "Content-Length: " << _inBuffer.size() << "\r\n"
+<< "Content-Length: " << numberString.size() << "\r\n"
 << "Content-Type: text/plain\r\n"
 << "Connection: Closed\r\n"
 << "\r\n"
+<< numberString;
 ;
 std::string str = oss.str();
 _outBuffer.insert(_outBuffer.end(), str.begin(), str.end());
-
-// append the content we got:
-_outBuffer.insert(_outBuffer.end(), _inBuffer.begin(), 
_inBuffer.end());
 _inBuffer.clear();
 }
 };
___
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits


[Libreoffice-commits] online.git: Branch 'private/Ashod/nonblocking' - net/clientnb.cpp net/common.hpp net/loolnb.cpp net/socket.hpp

2017-02-16 Thread Michael Meeks
 net/clientnb.cpp |8 
 net/common.hpp   |   48 
 net/loolnb.cpp   |   30 --
 net/socket.hpp   |6 +++---
 4 files changed, 23 insertions(+), 69 deletions(-)

New commits:
commit 84094a1d6e582d8caa7946855bfb1b873ae7f593
Author: Michael Meeks 
Date:   Thu Feb 16 11:00:38 2017 +

Lean on Poco for request parsing.

diff --git a/net/clientnb.cpp b/net/clientnb.cpp
index 99bda95..a8ec054 100644
--- a/net/clientnb.cpp
+++ b/net/clientnb.cpp
@@ -35,8 +35,6 @@
 #include 
 #include 
 
-#include "common.hpp"
-
 using Poco::Net::HTTPClientSession;
 using Poco::Net::HTTPRequest;
 using Poco::Net::HTTPResponse;
@@ -104,17 +102,11 @@ struct Session
 }
 };
 
-void testParseHTTP()
-{
-}
-
 struct Client : public Poco::Util::Application
 {
 public:
 int main(const std::vector& /* args */) override
 {
-testParseHTTP();
-
 Session first("init");
 Session second("init");
 
diff --git a/net/common.hpp b/net/common.hpp
deleted file mode 100644
index da1ca25..000
--- a/net/common.hpp
+++ /dev/null
@@ -1,48 +0,0 @@
-/* -*- 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 NB_COMMON_HPP
-#define NB_COMMON_HPP
-
-typedef std::vector HeaderStrings;
-typedef std::vector Payload;
-
-// FIXME: lots of return conditions:
-//partial data, malicious/invalid data, complete data
-//does this belong in a separate method ?
-size_t parseHTTP(const std::vector data,
- HeaderStrings , Payload & /* payload */)
-{
-size_t i, start;
-   for (i = start = 0; i < data.size(); ++i)
-{
-   unsigned char c = data[i];
-   if (c == 0)
-{   // someone doing something cute.
-return -1;
-}
-if (c == '\r' || c == '\n')
-{
-std::string header(reinterpret_cast([start]), i 
- start);
-while (++i < data.size() &&
-   (data[i] == '\n' || data[i] == '\r'))
-{}
-start = i;
-// terminating \r\n
-if (header.size() == 0)
-break;
-headers.push_back(header);
-}
-}
-return i;
-}
-
-#endif
-
-/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/net/loolnb.cpp b/net/loolnb.cpp
index a6a94eb..963a631 100644
--- a/net/loolnb.cpp
+++ b/net/loolnb.cpp
@@ -18,10 +18,15 @@
 #include 
 #include 
 
+#include 
 #include 
+#include 
+#include 
+
+using Poco::MemoryInputStream;
+using Poco::StringTokenizer;
 
 #include "socket.hpp"
-#include "common.hpp"
 
 constexpr int PortNumber = 9191;
 
@@ -36,17 +41,22 @@ public:
 {
 std::cerr << "message had size " << _inBuffer.size() << "\n";
 
-HeaderStrings headers;
-Payload payload;
-size_t skip;
-if ((skip = parseHTTP(_inBuffer, headers, payload) > 0))
+int number = 0;
+MemoryInputStream message(&_inBuffer[0], _inBuffer.size());
+Poco::Net::HTTPRequest req;
+req.read(message);
+
+StringTokenizer tokens(req.getURI(), "/?");
+if (tokens.count() == 4)
 {
-for (auto i = headers.begin(); i != headers.end(); ++i)
-{
-std::cerr << "header '" << *i << "'\n";
-}
+std::string subpool = tokens[2];
+number = std::stoi(tokens[3]);
 }
-// else close socket ? ...
+else
+std::cerr << " unknown tokens " << tokens.count() << std::endl;
+
+// complex algorithmic core:
+number = number + 1;
 
 std::ostringstream oss;
 oss << "HTTP/1.1 200 OK\r\n"
diff --git a/net/socket.hpp b/net/socket.hpp
index 3b55405..f41befc 100644
--- a/net/socket.hpp
+++ b/net/socket.hpp
@@ -282,8 +282,8 @@ public:
 }
 
   protected:
-std::vector< unsigned char > _inBuffer;
-std::vector< unsigned char > _outBuffer;
+std::vector< char > _inBuffer;
+std::vector< char > _outBuffer;
   public:
 
 HandleResult handlePoll( int events ) override
@@ -307,7 +307,7 @@ public:
 bool readIncomingData()
 {
 ssize_t len;
-unsigned char buf[4096];
+char buf[4096];
 do {
 len = ::read(getFD(), buf, sizeof(buf));
 } while (len < 0 && errno == EINTR);
___
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits


[Libreoffice-commits] online.git: Branch 'private/Ashod/nonblocking' - net/clientnb.cpp net/common.hpp net/loolnb.cpp net/socket.hpp

2017-02-16 Thread Michael Meeks
 net/clientnb.cpp |   69 ++-
 net/common.hpp   |   48 ++
 net/loolnb.cpp   |   14 +++
 net/socket.hpp   |1 
 4 files changed, 116 insertions(+), 16 deletions(-)

New commits:
commit 6b3972153e1f5f831074b3e47b1dc0fed0ed883f
Author: Michael Meeks 
Date:   Thu Feb 16 10:14:08 2017 +

Initial http header parsing pieces.

diff --git a/net/clientnb.cpp b/net/clientnb.cpp
index ec7c578..99bda95 100644
--- a/net/clientnb.cpp
+++ b/net/clientnb.cpp
@@ -35,6 +35,8 @@
 #include 
 #include 
 
+#include "common.hpp"
+
 using Poco::Net::HTTPClientSession;
 using Poco::Net::HTTPRequest;
 using Poco::Net::HTTPResponse;
@@ -46,41 +48,48 @@ using Poco::Util::HelpFormatter;
 using Poco::Util::Option;
 using Poco::Util::OptionSet;
 
+const char *HostName = "127.0.0.1";
 constexpr int PortNumber = 9191;
 
-Poco::Net::SocketAddress addr("127.0.0.1", PortNumber);
-
-struct Client : public Poco::Util::Application
+struct Session
 {
-public:
-int main(const std::vector& /* args */) override
+std::string _session_name;
+Poco::Net::HTTPClientSession *_session;
+
+Session(const char *session_name, bool https = false)
+: _session_name(session_name)
 {
-const char *hostname = "127.0.0.1";
-bool https = false;
-Poco::Net::HTTPClientSession *session;
 if (https)
-session = new Poco::Net::HTTPSClientSession(hostname, PortNumber);
+_session = new Poco::Net::HTTPSClientSession(HostName, PortNumber);
 else
-session = new Poco::Net::HTTPClientSession(hostname, PortNumber);
-Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, 
"/ping");
+_session = new Poco::Net::HTTPClientSession(HostName, PortNumber);
+}
+void sendPing(int i)
+{
+Poco::Net::HTTPRequest request(
+Poco::Net::HTTPRequest::HTTP_POST,
+"/ping/" + _session_name + "/" + std::to_string(i));
 try {
 Poco::Net::HTMLForm form;
 form.setEncoding(Poco::Net::HTMLForm::ENCODING_MULTIPART);
 form.prepareSubmit(request);
-form.write(session->sendRequest(request));
+form.write(_session->sendRequest(request));
 }
 catch (const Poco::Exception )
 {
 std::cerr << "Failed to write data: " << e.name() <<
   " " << e.message() << "\n";
-return -1;
+throw;
 }
-
+}
+int getResponse()
+{
+int number = 42;
 Poco::Net::HTTPResponse response;
 
 try {
 std::cerr << "try to get response\n";
-std::istream& responseStream = session->receiveResponse(response);
+std::istream& responseStream = _session->receiveResponse(response);
 
 std::string result(std::istreambuf_iterator(responseStream), 
{});
 std::cerr << "Got response '" << result << "'\n";
@@ -89,8 +98,36 @@ public:
 {
 std::cerr << "Exception converting: " << e.name() <<
   " " << e.message() << "\n";
-return -1;
+throw;
 }
+return number;
+}
+};
+
+void testParseHTTP()
+{
+}
+
+struct Client : public Poco::Util::Application
+{
+public:
+int main(const std::vector& /* args */) override
+{
+testParseHTTP();
+
+Session first("init");
+Session second("init");
+
+int count = 42, back;
+first.sendPing(count);
+second.sendPing(count + 1);
+
+back = first.getResponse();
+assert (back == count + 1);
+
+back = second.getResponse();
+assert (back == count + 1);
+
 return 0;
 }
 };
diff --git a/net/common.hpp b/net/common.hpp
new file mode 100644
index 000..da1ca25
--- /dev/null
+++ b/net/common.hpp
@@ -0,0 +1,48 @@
+/* -*- 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 NB_COMMON_HPP
+#define NB_COMMON_HPP
+
+typedef std::vector HeaderStrings;
+typedef std::vector Payload;
+
+// FIXME: lots of return conditions:
+//partial data, malicious/invalid data, complete data
+//does this belong in a separate method ?
+size_t parseHTTP(const std::vector data,
+ HeaderStrings , Payload & /* payload */)
+{
+size_t i, start;
+   for (i = start = 0; i < data.size(); ++i)
+{
+   unsigned char c = data[i];
+   if (c == 0)
+{   // someone doing something cute.
+return -1;
+}
+if (c == '\r' || c == '\n')
+{
+

[Libreoffice-commits] online.git: Branch 'private/Ashod/nonblocking' - net/clientnb.cpp net/loolnb.cpp net/socket.hpp

2017-02-14 Thread Michael Meeks
 net/clientnb.cpp |3 -
 net/loolnb.cpp   |  125 +++
 net/socket.hpp   |   55 ++--
 3 files changed, 88 insertions(+), 95 deletions(-)

New commits:
commit ded9607faf8e3e1fa122b200353a3465c94347ae
Author: Michael Meeks 
Date:   Tue Feb 14 23:45:24 2017 +

Implement basic buffering.

The socket now buffers input, and output, updates its poll record too.
We pass a simple message from client to server and back using lamers HTTP.
Sub-classed ClientSocket to provide a simple message handler.
   not very convinced by templatization here, but made it consistent.
   more ideal to have some virtual socket pieces.

diff --git a/net/clientnb.cpp b/net/clientnb.cpp
index e4a7fc3..ec7c578 100644
--- a/net/clientnb.cpp
+++ b/net/clientnb.cpp
@@ -82,7 +82,8 @@ public:
 std::cerr << "try to get response\n";
 std::istream& responseStream = session->receiveResponse(response);
 
-std::cerr << "Got response '" << responseStream << "'\n";
+std::string result(std::istreambuf_iterator(responseStream), 
{});
+std::cerr << "Got response '" << result << "'\n";
 }
 catch (const Poco::Exception )
 {
diff --git a/net/loolnb.cpp b/net/loolnb.cpp
index 952c44a..d457471 100644
--- a/net/loolnb.cpp
+++ b/net/loolnb.cpp
@@ -24,6 +24,34 @@
 
 constexpr int PortNumber = 9191;
 
+class SimpleResponseClient : public ClientSocket
+{
+public:
+SimpleResponseClient(const int fd) :
+ClientSocket(fd)
+{
+}
+virtual void handleIncomingMessage() override
+{
+std::cerr << "message had size " << _inBuffer.size() << "\n";
+std::ostringstream oss;
+oss << "HTTP/1.1 200 OK\r\n"
+<< "Date: Once, Upon a time GMT\r\n" // Mon, 27 Jul 2009 12:28:53 
GMT
+<< "Server: madeup string (Linux)\r\n"
+<< "Content-Length: " << _inBuffer.size() << "\r\n"
+<< "Content-Type: text/plain\r\n"
+<< "Connection: Closed\r\n"
+<< "\r\n"
+;
+std::string str = oss.str();
+_outBuffer.insert(_outBuffer.end(), str.begin(), str.end());
+
+// append the content we got:
+_outBuffer.insert(_outBuffer.end(), _inBuffer.begin(), 
_inBuffer.end());
+_inBuffer.clear();
+}
+};
+
 /// Handles non-blocking socket event polling.
 /// Only polls on N-Sockets and invokes callback and
 /// doesn't manage buffers or client data.
@@ -102,7 +130,7 @@ public:
 
 /// Insert a new socket to be polled.
 /// Sockets are removed only when the handler return false.
-void insertNewSocket(const std::shared_ptr& newSocket)
+void insertNewSocket(const std::shared_ptr& newSocket)
 {
 std::lock_guard lock(_mutex);
 
@@ -138,8 +166,7 @@ private:
 for (size_t i = 0; i < size; ++i)
 {
 _pollFds[i].fd = _pollSockets[i]->getFD();
-//TODO: Get from the socket.
-_pollFds[i].events = POLLIN | POLLOUT;
+_pollFds[i].events = _pollSockets[i]->getPollEvents();
 _pollFds[i].revents = 0;
 }
 
@@ -153,10 +180,10 @@ private:
 /// main-loop wakeup pipe
 int _wakeup[2];
 /// The sockets we're controlling
-std::vector _pollSockets;
+std::vector _pollSockets;
 /// Protects _newSockets
 std::mutex _mutex;
-std::vector _newSockets;
+std::vector _newSockets;
 /// The fds to poll.
 std::vector _pollFds;
 };
@@ -197,39 +224,7 @@ private:
 
 Poco::Net::SocketAddress addr("127.0.0.1", PortNumber);
 
-void client(const int timeoutMs)
-{
-const auto client = std::make_shared();
-if (!client->connect(addr, timeoutMs) && errno != EINPROGRESS)
-{
-const std::string msg = "Failed to call connect. (errno: ";
-throw std::runtime_error(msg + std::strerror(errno) + ")");
-}
-
-std::cout << "Connected " << client->getFD() << std::endl;
-
-client->send("1", 1);
-int sent = 1;
-while (sent > 0 && client->pollRead(5000))
-{
-char buf[1024];
-const int recv = client->recv(buf, sizeof(buf));
-if (recv <= 0)
-{
-perror("recv");
-break;
-}
-else
-{
-const std::string msg = std::string(buf, recv);
-const int num = stoi(msg);
-const std::string new_msg = std::to_string(num + 1);
-sent = client->send(new_msg.data(), new_msg.size());
-}
-}
-}
-
-void server(SocketPoll& poller)
+void server(SocketPoll& poller)
 {
 // Start server.
 auto server = std::make_shared();
@@ -250,7 +245,7 @@ void server(SocketPoll& poller)
 {
 if (server->pollRead(3))
 {
-std::shared_ptr clientSocket = server->accept();
+