aboutsummaryrefslogblamecommitdiff
path: root/test/test_p2p.cpp
blob: 85aeca1a44888aeaba18908ea6f83cf7a55d3d90 (plain) (tree)
























                                                                                  
                  

                     
                        







                               
                              

                        

                               


                            





                              
                                  
 
                                                                                                      



                                                                                       
                                                                                                                                            
           


                                                                                                                    



                                                  
                                                                                    

                                           
                                                                                               
             
                                                              

           
 



                                                   








                                                                                           

         
 



                                                                          
 
                          

  









                                                                      
                                                           


                  
 
                                 
          

                                                                                  


                                
         
                                  

                            


                
                                   



                                







                                                        

                   
                                                           



                              
                                                                                  

      
                                   




                                
                                                       












                                       
                                                       

                   








                                                       

      
































                                                                                    
                                                           




                                                           


            
                              



                                              
                                                             



                               

             
/**
 * 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 <cstdint>
#include <string>
#include <functional>
#include <unordered_map>

#include "salticidae/msg.h"
#include "salticidae/event.h"
#include "salticidae/network.h"
#include "salticidae/stream.h"

using salticidae::NetAddr;
using salticidae::DataStream;
using salticidae::PeerNetwork;
using salticidae::htole;
using salticidae::letoh;
using salticidae::EventContext;
using salticidae::ThreadCall;
using std::placeholders::_1;
using std::placeholders::_2;

struct Net {
    uint64_t id;
    EventContext ec;
    ThreadCall tc;
    std::thread th;
    PeerNetwork<uint8_t> *net;
    const std::string listen_addr;

    Net(uint64_t id, uint16_t port): id(id), tc(ec), listen_addr("127.0.0.1:"+ std::to_string(port)) {
        net = new salticidae::PeerNetwork<uint8_t>(
            ec,
            salticidae::PeerNetwork<uint8_t>::Config().conn_timeout(5).ping_period(2));
        net->reg_error_handler([this](const std::exception &err, bool fatal) {
            fprintf(stdout, "net %lu: captured %s error during an async call: %s\n", this->id, fatal ? "fatal" : "recoverable", err.what());
        });
        net->reg_unknown_peer_handler([this](const NetAddr &addr) {
            fprintf(stdout, "net %lu: unknown peer attempts to connnect %s\n", this->id, std::string(addr).c_str());
        });
        th = std::thread([=](){
            try {
                net->start();
                net->listen(NetAddr(listen_addr));
                fprintf(stdout, "net %lu: listen to %s\n", id, listen_addr.c_str());
                ec.dispatch();
            } catch (std::exception &err) {
                fprintf(stdout, "net %lu: got error during a sync call: %s\n", id, err.what());
            }
            fprintf(stdout, "net %lu: main loop ended\n", id);
        });
    }

    void add_peer(const std::string &listen_addr) {
        try {
            net->add_peer(NetAddr(listen_addr));
        } catch (std::exception &err) {
            fprintf(stdout, "net %lu: got error during a sync call: %s\n", id, err.what());
        }
    }

    void del_peer(const std::string &listen_addr) {
        try {
            net->del_peer(NetAddr(listen_addr));
        } catch (std::exception &err) {
            fprintf(stdout, "net %lu: got error during a sync call: %s\n", id, err.what());
        }
    }

    void stop_join() {
        tc.async_call([ec=this->ec](ThreadCall::Handle &) { ec.stop(); });
        th.join();
    }

    ~Net() { delete net; }
};

std::unordered_map<uint64_t, Net *> nets;
std::unordered_map<std::string, std::function<void(char *)> > cmd_map;

int read_int(char *buff) {
    scanf("%64s"