Git Product home page Git Product logo

simulated-internet's Introduction

Simulated-Internet

This project lets you simulate internet so you can test network applications and blockchain systems.

Project Implementation: https://github.com/BrandonHilde/Simulated-Internet-Example

Code Examples:

  public List<NetworkMember> GenerateNetworks()
  {
      List<NetworkMember> mems = new List<NetworkMember>
      {
          new NetworkMember { SimulationID = "bob" },
          new NetworkMember { SimulationID = "fred" },
          new NetworkMember { SimulationID = "sally" },
          new NetworkMember { SimulationID = "mary" },
          new NetworkMember { SimulationID = "john" }
      };
      
      return mems;
  }

  public void CreateSimulation()
  {
      InternetManager testInternet = new InternetManager();

      List<NetworkMember> members = GenerateNetworks(); // you need to create a method for creating your network members

      foreach (NetworkMember net in members)
      {
          net.SimulatedInternet = testInternet; 
      }   

      foreach (NodeRelay net in members) testInternet.ConnectToInternet(net);

      foreach(NodeRelay net in members)
      {
          DetailPackage dp = new DetailPackage();
          foreach (string id in ids)
          {
              if (net.SimulationID != id)
              {
                  dp = testInternet.LookUpMember(id);
                  net.Peers.Add(dp.IP);
              }
          }
      }
      
      testInternet.StartNetworkSimulation();
      
      foreach(NodeRelay net in members) net.Run();     
  }
   
   /////////////////////////////////////////////////////////////////////////////////////////////////////////////
   
  // How To Set Up Your Network Application Class
  public class NetworkApplication:NetworkMember
  {
    public enum NetworkType { Live = 22222, Simulated = 1337 };
    
    public NetworkType Network = NetworkType.Simulated;
    
    public InternetManager SimulatedInternet { get; set; }
    
    private ConcurrentQueue<byte[]> dataRecieved = new ConcurrentQueue<byte[]>();
    
    private List<byte[]> RecievedBytes = new List<byte[]>();
    
    public void Run()
    {
        new Thread(() =>
        {
            Thread.CurrentThread.IsBackground = true;
           
            while (!dataRecieved.IsEmpty)
            {
                byte[] nextRecieved = { };
                
                if (dataRecieved.TryDequeue(out nextRecieved))
                {
                    RecievedBytes.Add(nextRecieved);
                }
            }
            
            /// add application core logic here
            
            
        }).Start();
    }
    
    public override void DataListener(byte[] data, string SourceIP)
    {
        dataRecieved.Enqueue(data);
        
        // use the same function that handles reception of data from your internet client class
        // this way you can code the applcation in the exact same way without having to build your
        // code around the simulation. If you do this you can seamlessly test your application
        // without having to change any code to swap between simulation and the live test.
    }
    
    public void SendData(byte[] data, string DestinationIP, NetworkMember Self)
    {
    
        //use this method to determine where to send the data
        if(Network == NetworkType.Simulated)
        {
            SimulatedInternet.SendBytesToIP(
                                    this,
                                    data,
                                    DestinationIP,
                                    (ushort)Network); // Live = 22222, Simulated = 1337
        }
        else
        {
            //internetClient.Send(data, DestinationIP);
        }
    }
  }
 

simulated-internet's People

Contributors

brandonhilde avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.