diff options
author | winsvega <winsvega@mail.ru> | 2015-02-13 03:27:33 +0800 |
---|---|---|
committer | winsvega <winsvega@mail.ru> | 2015-02-13 03:27:33 +0800 |
commit | 5ea03fd533fcc4206bc8a71b8c71ede177f1f86a (patch) | |
tree | 7ac933e138559cf326896028d3715bd58d1f6819 /whisperTopic.cpp | |
parent | ca42ee5e7a8af645ca489c21034e801808996ba1 (diff) | |
parent | 5eae51be12f82da95506593f65c75419b4bdff4e (diff) | |
download | dexon-solidity-5ea03fd533fcc4206bc8a71b8c71ede177f1f86a.tar dexon-solidity-5ea03fd533fcc4206bc8a71b8c71ede177f1f86a.tar.gz dexon-solidity-5ea03fd533fcc4206bc8a71b8c71ede177f1f86a.tar.bz2 dexon-solidity-5ea03fd533fcc4206bc8a71b8c71ede177f1f86a.tar.lz dexon-solidity-5ea03fd533fcc4206bc8a71b8c71ede177f1f86a.tar.xz dexon-solidity-5ea03fd533fcc4206bc8a71b8c71ede177f1f86a.tar.zst dexon-solidity-5ea03fd533fcc4206bc8a71b8c71ede177f1f86a.zip |
Merge branch 'develop' of https://github.com/ethereum/cpp-ethereum into develop
Diffstat (limited to 'whisperTopic.cpp')
-rw-r--r-- | whisperTopic.cpp | 159 |
1 files changed, 89 insertions, 70 deletions
diff --git a/whisperTopic.cpp b/whisperTopic.cpp index 11937336..be93174e 100644 --- a/whisperTopic.cpp +++ b/whisperTopic.cpp @@ -36,27 +36,31 @@ BOOST_AUTO_TEST_CASE(topic) auto oldLogVerbosity = g_logVerbosity; g_logVerbosity = 0; + Host host1("Test", NetworkPreferences(30303, "127.0.0.1", false, true)); + auto whost1 = host1.registerCapability(new WhisperHost()); + host1.start(); + + while (!host1.isStarted()) + this_thread::sleep_for(chrono::milliseconds(2)); + bool started = false; unsigned result = 0; std::thread listener([&]() { setThreadName("other"); - - Host ph("Test", NetworkPreferences(50303, "", false, true)); - auto wh = ph.registerCapability(new WhisperHost()); - ph.start(); + started = true; /// Only interested in odd packets - auto w = wh->installWatch(BuildTopicMask("odd")); + auto w = whost1->installWatch(BuildTopicMask("odd")); started = true; set<unsigned> received; for (int iterout = 0, last = 0; iterout < 200 && last < 81; ++iterout) { - for (auto i: wh->checkWatch(w)) + for (auto i: whost1->checkWatch(w)) { - Message msg = wh->envelope(i).open(wh->fullTopic(w)); + Message msg = whost1->envelope(i).open(whost1->fullTopic(w)); last = RLP(msg.payload()).toInt<unsigned>(); if (received.count(last)) continue; @@ -66,22 +70,28 @@ BOOST_AUTO_TEST_CASE(topic) } this_thread::sleep_for(chrono::milliseconds(50)); } + }); + + Host host2("Test", NetworkPreferences(30300, "127.0.0.1", false, true)); + auto whost2 = host2.registerCapability(new WhisperHost()); + host2.start(); + + while (!host2.isStarted()) + this_thread::sleep_for(chrono::milliseconds(2)); + + this_thread::sleep_for(chrono::milliseconds(100)); + host2.addNode(host1.id(), "127.0.0.1", 30303, 30303); + + this_thread::sleep_for(chrono::milliseconds(500)); while (!started) - this_thread::sleep_for(chrono::milliseconds(50)); - - Host ph("Test", NetworkPreferences(50300, "", false, true)); - shared_ptr<WhisperHost> wh = ph.registerCapability(new WhisperHost()); - this_thread::sleep_for(chrono::milliseconds(500)); - ph.start(); - this_thread::sleep_for(chrono::milliseconds(500)); - ph.connect("127.0.0.1", 50303); + this_thread::sleep_for(chrono::milliseconds(2)); KeyPair us = KeyPair::create(); for (int i = 0; i < 10; ++i) { - wh->post(us.sec(), RLPStream().append(i * i).out(), BuildTopic(i)(i % 2 ? "odd" : "even")); + whost2->post(us.sec(), RLPStream().append(i * i).out(), BuildTopic(i)(i % 2 ? "odd" : "even")); this_thread::sleep_for(chrono::milliseconds(250)); } @@ -96,7 +106,15 @@ BOOST_AUTO_TEST_CASE(forwarding) cnote << "Testing Whisper forwarding..."; auto oldLogVerbosity = g_logVerbosity; g_logVerbosity = 0; - + + // Host must be configured not to share peers. + Host host1("Listner", NetworkPreferences(30303, "", false, true)); + host1.setIdealPeerCount(0); + auto whost1 = host1.registerCapability(new WhisperHost()); + host1.start(); + while (!host1.isStarted()) + this_thread::sleep_for(chrono::milliseconds(2)); + unsigned result = 0; bool done = false; @@ -105,22 +123,16 @@ BOOST_AUTO_TEST_CASE(forwarding) { setThreadName("listener"); - // Host must be configured not to share peers. - Host ph("Listner", NetworkPreferences(50303, "", false, true)); - ph.setIdealPeerCount(0); - auto wh = ph.registerCapability(new WhisperHost()); - ph.start(); - startedListener = true; /// Only interested in odd packets - auto w = wh->installWatch(BuildTopicMask("test")); + auto w = whost1->installWatch(BuildTopicMask("test")); for (int i = 0; i < 200 && !result; ++i) { - for (auto i: wh->checkWatch(w)) + for (auto i: whost1->checkWatch(w)) { - Message msg = wh->envelope(i).open(wh->fullTopic(w)); + Message msg = whost1->envelope(i).open(whost1->fullTopic(w)); unsigned last = RLP(msg.payload()).toInt<unsigned>(); cnote << "New message from:" << msg.from().abridged() << RLP(msg.payload()).toInt<unsigned>(); result = last; @@ -129,6 +141,16 @@ BOOST_AUTO_TEST_CASE(forwarding) } }); + + // Host must be configured not to share peers. + Host host2("Forwarder", NetworkPreferences(30305, "", false, true)); + host2.setIdealPeerCount(1); + auto whost2 = host2.registerCapability(new WhisperHost()); + host2.start(); + while (!host2.isStarted()) + this_thread::sleep_for(chrono::milliseconds(2)); + + Public fwderid; bool startedForwarder = false; std::thread forwarder([&]() { @@ -137,26 +159,19 @@ BOOST_AUTO_TEST_CASE(forwarding) while (!startedListener) this_thread::sleep_for(chrono::milliseconds(50)); - // Host must be configured not to share peers. - Host ph("Forwarder", NetworkPreferences(50305, "", false, true)); - ph.setIdealPeerCount(0); - auto wh = ph.registerCapability(new WhisperHost()); - this_thread::sleep_for(chrono::milliseconds(500)); - ph.start(); - this_thread::sleep_for(chrono::milliseconds(500)); - ph.connect("127.0.0.1", 50303); + host2.addNode(host1.id(), "127.0.0.1", 30303, 30303); startedForwarder = true; /// Only interested in odd packets - auto w = wh->installWatch(BuildTopicMask("test")); + auto w = whost2->installWatch(BuildTopicMask("test")); while (!done) { - for (auto i: wh->checkWatch(w)) + for (auto i: whost2->checkWatch(w)) { - Message msg = wh->envelope(i).open(wh->fullTopic(w)); + Message msg = whost2->envelope(i).open(whost2->fullTopic(w)); cnote << "New message from:" << msg.from().abridged() << RLP(msg.payload()).toInt<unsigned>(); } this_thread::sleep_for(chrono::milliseconds(50)); @@ -166,13 +181,13 @@ BOOST_AUTO_TEST_CASE(forwarding) while (!startedForwarder) this_thread::sleep_for(chrono::milliseconds(50)); - Host ph("Sender", NetworkPreferences(50300, "", false, true)); - ph.setIdealPeerCount(0); + Host ph("Sender", NetworkPreferences(30300, "", false, true)); + ph.setIdealPeerCount(1); shared_ptr<WhisperHost> wh = ph.registerCapability(new WhisperHost()); - this_thread::sleep_for(chrono::milliseconds(500)); ph.start(); - this_thread::sleep_for(chrono::milliseconds(500)); - ph.connect("127.0.0.1", 50305); + ph.addNode(host2.id(), "127.0.0.1", 30305, 30305); + while (!ph.isStarted()) + this_thread::sleep_for(chrono::milliseconds(10)); KeyPair us = KeyPair::create(); wh->post(us.sec(), RLPStream().append(1).out(), BuildTopic("test")); @@ -194,32 +209,33 @@ BOOST_AUTO_TEST_CASE(asyncforwarding) unsigned result = 0; bool done = false; + + // Host must be configured not to share peers. + Host host1("Forwarder", NetworkPreferences(30305, "", false, true)); + host1.setIdealPeerCount(1); + auto whost1 = host1.registerCapability(new WhisperHost()); + host1.start(); + while (!host1.isStarted()) + this_thread::sleep_for(chrono::milliseconds(2)); bool startedForwarder = false; std::thread forwarder([&]() { setThreadName("forwarder"); - - // Host must be configured not to share peers. - Host ph("Forwarder", NetworkPreferences(50305, "", false, true)); - ph.setIdealPeerCount(0); - auto wh = ph.registerCapability(new WhisperHost()); + this_thread::sleep_for(chrono::milliseconds(500)); - ph.start(); - - this_thread::sleep_for(chrono::milliseconds(500)); - ph.connect("127.0.0.1", 50303); +// ph.addNode("127.0.0.1", 30303, 30303); startedForwarder = true; /// Only interested in odd packets - auto w = wh->installWatch(BuildTopicMask("test")); + auto w = whost1->installWatch(BuildTopicMask("test")); while (!done) { - for (auto i: wh->checkWatch(w)) + for (auto i: whost1->checkWatch(w)) { - Message msg = wh->envelope(i).open(wh->fullTopic(w)); + Message msg = whost1->envelope(i).open(whost1->fullTopic(w)); cnote << "New message from:" << msg.from().abridged() << RLP(msg.payload()).toInt<unsigned>(); } this_thread::sleep_for(chrono::milliseconds(50)); @@ -227,30 +243,33 @@ BOOST_AUTO_TEST_CASE(asyncforwarding) }); while (!startedForwarder) - this_thread::sleep_for(chrono::milliseconds(50)); - + this_thread::sleep_for(chrono::milliseconds(2)); + { - Host ph("Sender", NetworkPreferences(50300, "", false, true)); - ph.setIdealPeerCount(0); - shared_ptr<WhisperHost> wh = ph.registerCapability(new WhisperHost()); - this_thread::sleep_for(chrono::milliseconds(500)); - ph.start(); - this_thread::sleep_for(chrono::milliseconds(500)); - ph.connect("127.0.0.1", 50305); - + Host host2("Sender", NetworkPreferences(30300, "", false, true)); + host2.setIdealPeerCount(1); + shared_ptr<WhisperHost> whost2 = host2.registerCapability(new WhisperHost()); + host2.start(); + while (!host2.isStarted()) + this_thread::sleep_for(chrono::milliseconds(2)); + host2.addNode(host1.id(), "127.0.0.1", 30305, 30305); + + while (!host2.peerCount()) + this_thread::sleep_for(chrono::milliseconds(5)); + KeyPair us = KeyPair::create(); - wh->post(us.sec(), RLPStream().append(1).out(), BuildTopic("test")); + whost2->post(us.sec(), RLPStream().append(1).out(), BuildTopic("test")); this_thread::sleep_for(chrono::milliseconds(250)); } { - Host ph("Listener", NetworkPreferences(50300, "", false, true)); - ph.setIdealPeerCount(0); + Host ph("Listener", NetworkPreferences(30300, "", false, true)); + ph.setIdealPeerCount(1); shared_ptr<WhisperHost> wh = ph.registerCapability(new WhisperHost()); - this_thread::sleep_for(chrono::milliseconds(500)); ph.start(); - this_thread::sleep_for(chrono::milliseconds(500)); - ph.connect("127.0.0.1", 50305); + while (!ph.isStarted()) + this_thread::sleep_for(chrono::milliseconds(2)); + ph.addNode(host1.id(), "127.0.0.1", 30305, 30305); /// Only interested in odd packets auto w = wh->installWatch(BuildTopicMask("test")); |