SimplyP2P

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 13, 2022 License: MIT Imports: 11 Imported by: 0

README

SimplyP2P

SimplyP2P is a simple Peer-to-peer network that synchronize a common boolean state between all peers and show the current state in a GUI, with some buttons to change its value in all peers. the reference value is the time expressed as unix timestamp, if the value is greater than that of the currently contained value, the status of current node is updated, otherwise the update will be discarded because it is considered obsolete.

Warning

This project is fully working, and the crash problem has been resolved. State updates are instant and are received by all peers.

However, the GUI library used is in a mid alpha state and was chosen because it is simple to use and very light: it's used to show the current state of the system and allow you to modify it.

Requirements

Gtk+-3.0 dev is necessary to run this project. If you have an Ubuntu-based distro and don't know how to install it, use the command below.

sudo apt install libgtk-3-dev

Run and build

Use the localTest.go example in the cmd directory if you want to test this library easily using go run or go build.

The alternative is to make your own main executable importing this module in your project or use its functions as a library. Check the documentation!

Test

True button: Test true

False button: Test false

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AddPeer

type AddPeer struct {
	Peer
}

AddPeer is a packet that adds a new peer to a node. Use an array instead of a slice to use it has map hashed key.

func (*AddPeer) ReadFrom

func (p *AddPeer) ReadFrom(r io.Reader) (n int64, err error)

ReadFrom decodes a Peer packet.

func (AddPeer) WriteTo

func (p AddPeer) WriteTo(w io.Writer) (n int64, err error)

WriteTo encodes a Peer packet.

type ChangeState

type ChangeState struct {
	State bool
	Time  time.Time
}

ChangeState is a packet that change global P2P state.

func (*ChangeState) ReadFrom

func (s *ChangeState) ReadFrom(r io.Reader) (n int64, err error)

ReadFrom decodes a ChangeState packet.

func (ChangeState) WriteTo

func (s ChangeState) WriteTo(w io.Writer) (n int64, err error)

WriteTo encodes a ChangeState packet. If the time hasn't been set, then it corresponds to the current time.

type Connection

type Connection struct {
	// contains filtered or unexported fields
}

Connection is a TCP connection to another peer.

func (*Connection) Close

func (conn *Connection) Close() error

Close closes this connection. Implements the io.Closer interface.

func (*Connection) Connect

func (conn *Connection) Connect(destination string) error

Connect connects to a remote peer specified in the destination argument.

func (*Connection) Receive

func (conn *Connection) Receive(length int) ([]byte, error)

Receive returns a byte slice with its length specified as argument, filled with bytes read from this connection.

func (*Connection) Send

func (conn *Connection) Send(data []byte) error

Send sends a byte slice to the other peer in this connection. Length of the data sent is the length of the data slice.

func (*Connection) Write

func (conn *Connection) Write(p []byte) (n int, err error)

Write implements the io.Writer interface for the Connection type.

type Counter

type Counter int32

Counter is the number of connected peers.

func (*Counter) Decrement

func (c *Counter) Decrement()

Decrement subtracts 1 from the value of the counter.

func (*Counter) Increment

func (c *Counter) Increment()

Increment adds 1 to the value of counter.

func (*Counter) Value

func (c *Counter) Value() int32

Value returns current value of the counter.

type Node

type Node struct {
	// contains filtered or unexported fields
}

Node is current running peer in the network. It has a listener on a specific port and contains connections with other peers.

func NewNode

func NewNode(listenPort string, peers [][]string, wg *sync.WaitGroup) (*Node, error)

NewNode creates a new peer to peer node instance specifying local listening port and a slice of known peers.

func (*Node) Close

func (n *Node) Close() error

Close closes the connection with every peer.

type Peer

type Peer struct {
	// contains filtered or unexported fields
}

Peer is another peer in the P2P network.

func (Peer) GetAddress

func (p Peer) GetAddress() string

GetAddress returns the string representation of IP address of current Peer.

func (Peer) GetAddressAndPort

func (p Peer) GetAddressAndPort() string

GetAddressAndPort returns the string representation of IP address and port of current Peer.

func (Peer) GetPort

func (p Peer) GetPort() string

GetPort returns the string representation of listening port of current Peer.

func (*Peer) SetAddress

func (p *Peer) SetAddress(address string) error

SetAddress parses an address and save it to current Peer.

func (*Peer) SetPort

func (p *Peer) SetPort(port string) error

SetPort parses a port and save it to current Peer.

type Peers

type Peers struct {
	// contains filtered or unexported fields
}

Peers is a map that contains all connected peers.

func (*Peers) Add

func (p *Peers) Add(key Peer, value *Connection)

Add adds a peer to the peers map.

func (*Peers) Broadcast

func (p *Peers) Broadcast(to io.WriterTo)

Broadcast sends a packet to all peers connected.

func (*Peers) Close

func (p *Peers) Close() error

Close closes all connections with other peers.

func (*Peers) Get

func (p *Peers) Get(key Peer) (*Connection, error)

Get returns a peer from the peers map.

func (*Peers) Len

func (p *Peers) Len() int32

Len returns the number of peers connected.

func (*Peers) Remove

func (p *Peers) Remove(key Peer)

Remove removes a peer from the peers map.

type SimplyGui

type SimplyGui struct {
	// contains filtered or unexported fields
}

SimplyGui is a simple graphic interface that show the current state and some buttons to change its value.

type State

type State struct {
	// contains filtered or unexported fields
}

State is the current global P2P state.

func (*State) GetState

func (s *State) GetState() bool

GetState returns current state value.

func (*State) GetTime

func (s *State) GetTime() time.Time

GetTime returns current time value.

func (*State) SetEvent

func (s *State) SetEvent(event func(bool))

SetEvent sets a function to call when the value change.

func (*State) Update

func (s *State) Update(value bool, t time.Time) bool

Update updates the state value if there is a more recent value change. Returns boolean update (true if has been updated).

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL