/**
* Copyright (c) 2018 Cornell University.
*
* Author: Ted Yin <tederminant@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <cstdio>
#include <string>
#include <functional>
#include <openssl/rand.h>
#include "salticidae/msg.h"
#include "salticidae/event.h"
#include "salticidae/network.h"
#include "salticidae/stream.h"
using salticidae::NetAddr;
using salticidae::DataStream;
using salticidae::MsgNetwork;
using salticidae::ConnPool;
using salticidae::TimerEvent;
using salticidae::EventContext;
using salticidae::htole;
using salticidae::letoh;
using salticidae::bytearray_t;
using salticidae::uint256_t;
using salticidae::static_pointer_cast;
using salticidae::Config;
using salticidae::ThreadCall;
using salticidae::BoxObj;
using std::placeholders::_1;
using std::placeholders::_2;
struct MsgRand {
static const uint8_t opcode = 0x0;
DataStream serialized;
bytearray_t bytes;
MsgRand(size_t size) {
bytearray_t bytes;
bytes.resize(size);
RAND_bytes(&bytes[0], size);
serialized << std::move(bytes);
}
MsgRand(DataStream &&s) { bytes = s; }
};
struct MsgAck {
static const uint8_t opcode = 0x1;
uint256_t hash;
DataStream serialized;
MsgAck(const uint256_t &hash) { serialized << hash; }
MsgAck(DataStream &&s) { s >> hash; }
};
const uint8_t MsgRand::opcode;
const uint8_t MsgAck::opcode;
using MyNet = salticidae::PeerNetwork<uint8_t>;
std::vector<NetAddr> addrs;
struct TestContext {
TimerEvent timer;
int state;
uint256_t hash;
size_t ncompleted;
TestContext(): ncompleted(0) {}
};
struct AppContext {
NetAddr addr;
EventContext ec;
BoxObj<MyNet> net;
BoxObj<ThreadCall> tcall;
std::unordered_map<NetAddr, TestContext> tc;
};
void install_proto(AppContext &app, const size_t &seg_buff_size) {
auto &ec = app.ec;
auto &net = *app.net;
auto send_rand = [&](int size, const MyNet::conn_t &conn) {
auto &tc = app.tc[conn->get_addr()];
MsgRand msg(size);
tc.hash = msg.serialized.get_hash();
net.send_msg(std::move(msg), conn);
};
net.reg_conn_handler([&, send_rand](const ConnPool::conn_t &conn, bool connected) {
if (connected)
{
if (conn->get_mode() == ConnPool::Conn::ACTIVE)
{
auto &tc = app.tc[conn->get_addr()];
tc.state = 1;
SALTICIDAE_LOG_INFO("increasing phase");
send_rand(tc.state, static_pointer_cast<MyNet::Conn>