Skip to content

Commit

Permalink
Re-structured stream interface to use a non-blocking API (event handl…
Browse files Browse the repository at this point in the history
…es to be added soon), and fixed TCP Stream to now work again...
  • Loading branch information
scrossuk committed Jan 11, 2014
1 parent 7ab4c74 commit b612cb8
Show file tree
Hide file tree
Showing 51 changed files with 554 additions and 631 deletions.
28 changes: 6 additions & 22 deletions example/Buffer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,16 @@ int main() {
BufferBuilder builder(buffer);
BinaryOStream outputStream(builder);

bool writeSuccess = true;
writeSuccess &= Binary::WriteInt64(outputStream, a);
writeSuccess &= Binary::WriteUint32(outputStream, b);
writeSuccess &= Binary::WriteUint8(outputStream, c);

if (!writeSuccess) {
printf("Failed to write values.\n");
return -1;
}

int64_t d;
uint32_t e;
uint8_t f;
Binary::WriteInt64(outputStream, a);
Binary::WriteUint32(outputStream, b);
Binary::WriteUint8(outputStream, c);

BufferIterator iterator(buffer);
BinaryIStream inputStream(iterator);

bool readSuccess = true;
readSuccess &= Binary::ReadInt64(inputStream, &d);
readSuccess &= Binary::ReadUint32(inputStream, &e);
readSuccess &= Binary::ReadUint8(inputStream, &f);

if (!readSuccess) {
printf("Failed to read values.\n");
return -1;
}
const int64_t d = Binary::ReadInt64(inputStream);
const uint32_t e = Binary::ReadUint32(inputStream);
const uint8_t f = Binary::ReadUint8(inputStream);

printf("Read (%lld, %llu, %llu) from buffer.\n",
(long long) d,
Expand Down
12 changes: 3 additions & 9 deletions example/Crypt/ECDSA/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ int main() {

std::cout << "Signing..." << std::endl;

if (!OpenP2P::Binary::WriteUint32(binSign, 42)) {
std::cout << "Failed to write to SignStream" << std::endl;
return 0;
}
OpenP2P::Binary::WriteUint32(binSign, 42);

OpenP2P::Buffer signature = signStream.signature();

Expand All @@ -35,12 +32,9 @@ int main() {

std::cout << "Verifying..." << std::endl;

if (!OpenP2P::Binary::WriteUint32(binVerify, 42)) {
std::cout << "Failed to write to SignStream" << std::endl;
return 0;
}
OpenP2P::Binary::WriteUint32(binVerify, 42);

std::cout << "Signature is " << (verifyStream.isValid() ? "valid" : "not valid") << std::endl;
std::cout << "Signature is " << (verifyStream.isSignatureValid() ? "valid" : "not valid") << std::endl;

return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion example/DHT/dht2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <OpenP2P/Kademlia.hpp>
#include <OpenP2P/UDP.hpp>

template <std::size_t IdSize>
template <size_t IdSize>
class TestDatabase: public OpenP2P::Kademlia::Database<IdSize> {
public:
TestDatabase() {
Expand Down
6 changes: 3 additions & 3 deletions example/Futures/futures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ class PromiseThread: public Runnable {

};

std::size_t transform1(std::string string) {
size_t transform1(std::string string) {
std::cout << "Transform 1" << std::endl;
return string.size();
}

std::string transform2(std::size_t size) {
std::string transform2(size_t size) {
std::cout << "Transform 2" << std::endl;
std::ostringstream stream;
stream << size;
Expand All @@ -60,7 +60,7 @@ int main() {

// Compose three times.
std::cout << "1" << std::endl;
Future<std::string>* interFuture = new Future<std::string>(Future<std::string>(promiseThread.promise1_).compose<std::size_t>(transform1).compose<std::string>(transform2));
Future<std::string>* interFuture = new Future<std::string>(Future<std::string>(promiseThread.promise1_).compose<size_t>(transform1).compose<std::string>(transform2));

std::cout << "2" << std::endl;

Expand Down
32 changes: 16 additions & 16 deletions example/HTTP/mainClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@

using namespace OpenP2P;

void output(const uint8_t* data, std::size_t size) {
for (std::size_t i = 0; i < size; i++) {
void output(const uint8_t* data, size_t size) {
for (size_t i = 0; i < size; i++) {
std::cout << (char) data[i];
}
}

class StdOutStream: public OutputStream {
class StdOutStream: public OStream {
public:
StdOutStream() : size_(0) { }

std::size_t size() {
size_t size() {
return size_;
}

std::size_t waitForSpace(Timeout) {
return 1000;
bool isValid() const {
return true;
}

bool write(const uint8_t* data, std::size_t dataSize, Timeout) {
size_t write(const uint8_t* data, size_t dataSize) {
std::cout << "Write of size " << dataSize << ": ";
output(data, dataSize);
std::cout << std::endl;
Expand All @@ -34,26 +34,26 @@ class StdOutStream: public OutputStream {
}

private:
std::size_t size_;
size_t size_;

};

class TextIOStream {
public:
TextIOStream(IOStream& stream) : binaryStream_(stream) { }
TextIOStream(IOStream& stream) : stream_(stream), binaryStream_(stream) { }

TextIOStream& operator<<(const std::string& string) {
binaryStream_.getOutputStream().write((const uint8_t*) string.c_str(), string.size());
binaryStream_.output().writeAll((const uint8_t*) string.c_str(), string.size());
return *this;
}

TextIOStream& operator>>(OutputStream& stream) {
BinaryOStream outStream(stream);
Binary::MoveData(binaryStream_.getInputStream(), outStream);
TextIOStream& operator>>(OStream& stream) {
Binary::MoveData(stream_, stream);
return *this;
}

private:
IOStream& stream_;
BinaryIOStream binaryStream_;

};
Expand All @@ -75,16 +75,16 @@ int main(int argc, char* argv[]) {
boost::optional< std::vector<IP::Endpoint> > endpointList = resolver.resolve(domain, "http", Timeout::Seconds(5.0));

if (!endpointList) {
std::cout << "Failed to resolve" << std::endl;
std::cout << "Failed to resolve." << std::endl;
return 0;
}

std::cout << "Connecting..." << std::endl;

TCP::Stream tcpStream;

if (!tcpStream.connect(*endpointList, Timeout::Seconds(5.0))) {
std::cout << "Failed to connect" << std::endl;
if (!tcpStream.connect(*endpointList)) {
std::cout << "Failed to connect." << std::endl;
return 0;
}

Expand Down
9 changes: 3 additions & 6 deletions example/OFTorrent/create.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class BlockGen: public OFTorrent::OutputStreamGenerator {
public:
BlockGen() : count_(0) { }

OutputStream& getNextOutputStream() {
OStream& getNextOutputStream() {
std::ostringstream s;
s << "out" << count_ << ".txt";
count_++;
Expand All @@ -18,7 +18,7 @@ class BlockGen: public OFTorrent::OutputStreamGenerator {
}

private:
std::size_t count_;
size_t count_;
FileOStream fileStream;

};
Expand All @@ -43,10 +43,7 @@ int main(int argc, char* argv[]) {

OFTorrent::BlockStream blockStream(blockGen, OFTorrent::BLOCKSIZE_512KB);

BinaryIStream binaryInStream(xorStream);
BinaryOStream binaryOutStream(blockStream);

Binary::MoveData(binaryInStream, binaryOutStream);
Binary::MoveData(xorStream, blockStream);

return 0;
}
Expand Down
14 changes: 4 additions & 10 deletions example/TCP/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ int main() {
TCP::Stream tcpStream;
std::cout << "---Connecting" << std::endl;

if (!tcpStream.connect(IP::Endpoint(IP::V4Address::Localhost(), 45556), Timeout::Seconds(5.0))) {
if (!tcpStream.connect(IP::Endpoint(IP::V4Address::Localhost(), 45556))) {
std::cout << "---Failed to connect" << std::endl;
return 0;
}
Expand All @@ -19,17 +19,11 @@ int main() {
BinaryIOStream stream(tcpStream);

for (unsigned int i = 0; i < 1000; i += 2) {
if (!Binary::WriteUint32(stream.getOutputStream(), i)) {
std::cout << "---Failed to write to stream" << std::endl;
return 0;
}
Binary::WriteUint32(stream.output(), i);

uint32_t v = 0;
std::cout << "Sent: " << i << std::endl;

if (!Binary::ReadUint32(stream.getInputStream(), &v)) {
std::cout << "---Failed to read from stream" << std::endl;
return 0;
}
const uint32_t v = Binary::ReadUint32(stream.input());

if (v != (i + 1)) {
std::cout << "Wrong number: " << v << ", Expected: " << (i + 1) << std::endl;
Expand Down
16 changes: 5 additions & 11 deletions example/TCP/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,7 @@ class ClientThread: public Runnable {
BinaryIOStream stream(tcpStream_);

for (unsigned int i = 0; i < 1000; i += 2) {
uint32_t v = 0;

if (!Binary::ReadUint32(stream.getInputStream(), &v)) {
std::cout << "---Failed to read from stream" << std::endl;
return;
}
const uint32_t v = Binary::ReadUint32(stream.input());

if (v != i) {
std::cout << "Wrong number: " << v << ", Expected: " << (i + 1) << " - Terminating connection" << std::endl;
Expand All @@ -55,17 +50,16 @@ class ClientThread: public Runnable {

std::cout << "Received: " << i << std::endl;

if (!Binary::WriteUint32(stream.getOutputStream(), i + 1)) {
std::cout << "---Failed to write to stream" << std::endl;
return;
}
Binary::WriteUint32(stream.output(), i + 1);

std::cout << "Sent: " << (i + 1) << std::endl;
}

std::cout << "---Successfully completed transfer" << std::endl;
}

void cancel() {
tcpStream_.close();
// tcpStream_.close();
}

private:
Expand Down
Loading

0 comments on commit b612cb8

Please sign in to comment.