p2p

package
v0.0.0-...-2e4edee Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2022 License: LGPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

This is Insight 0+0 simple P2P Network.

Index

Constants

View Source
const (
	BROKEN_NODE_COUNT = 10 // The number of consecutive failed attempts, and the node will be delete from nodes table.
)

Variables

This section is empty.

Functions

func NodesDiscover

func NodesDiscover(ip, port string, self SelfNodesInfo) (nt string, err error)

Discover nodes table from one node.

The nt is nodes table file. self is local nodes identity hashid and service port.

Types

type NDiscoverResult

type NDiscoverResult struct {
	Status     P2Poperate
	Err        string
	NodesTable string
}

The nodes discover result.

It's must be zip by cznic/zappy. So you must unzip before use.

func (NDiscoverResult) MarshalBinary

func (o NDiscoverResult) MarshalBinary() (data []byte, err error)

func (*NDiscoverResult) UnmarshalBinary

func (o *NDiscoverResult) UnmarshalBinary(data []byte) (err error)

type NodeStatus

type NodeStatus struct {
	Hash    string   // The node hash.
	Ip      string   // The IP
	Port    string   // The port
	Type    NodeType // The node type
	Failure uint     // The failure count.
}

The node's status.

type NodeType

type NodeType uint

The node type

const (
	NODE_TYPE_NORMAL NodeType = iota // The node type is normal.
	NODE_TYPE_SERVER                 // The node is a server.
	NODE_TYPE_DEAL                   // The node is only for business deal, it will not be remembered in p2p net work node table.
)

type NodesTable

type NodesTable struct {
	Table map[string]*NodeStatus
	File  string // the local nodes table file.
}

The nodes table. The string is node's hash.

func NewNodesTable

func NewNodesTable() *NodesTable

func (*NodesTable) AddOneNode

func (n *NodesTable) AddOneNode(hash, ip, port string, thetype NodeType)

Add one node status to table.

func (*NodesTable) AddToTable

func (n *NodesTable) AddToTable(s string)

Add the node status information to the table.

Ths s is a nodes table file. The s' every line is like this: hash,ip,port,type.

func (*NodesTable) Delete

func (n *NodesTable) Delete(hash string)

Delete node which was not connect (The Failure >= some number).

func (*NodesTable) OutputTable

func (n *NodesTable) OutputTable() (s string)

Return all table nodes status to a string.

It like: hash1111111111111,192.168.1.200,34,NODE_TYPE_SERVER\nhash12221221111111,192.133.1.200,34,NODE_TYPE_NORMAL

func (*NodesTable) Random

func (n *NodesTable) Random(c int) (o map[string]*NodeStatus, err error)

Random return some nodes status.

func (*NodesTable) ReturnCount

func (n *NodesTable) ReturnCount() (count int)

Return the number of nodes in the table.

func (*NodesTable) SetTableFile

func (n *NodesTable) SetTableFile(filename string) (err error)

Set the table file, if the file can not create or access, return error.

type P2PMessageOperator

type P2PMessageOperator interface {
	TableSet(table *NodesTable)
	OperateRequest(data *bytes.Buffer, ce *nst2.ConnExec) (err error)
	OperateResponse(data *bytes.Buffer, ce *nst2.ConnExec) (err error)
}

The message operator interface.

type P2Poperate

type P2Poperate uint
const (
	P2P_OPERATE_NOTHING          P2Poperate = iota // P2P Operate do nothing.
	P2P_OPERATE_OK                                 // P2P Operate status ok.
	P2P_OPERATE_ERR                                // P2P Operate status err.
	P2P_OPERATE_NODES_DISCOVER                     // P2P Operate discover nodes.
	P2P_OPERATE_MESSAGE_REQUEST                    // P2P Operate send one message to nodes.
	P2P_OPERATE_MESSAGE_RESPONSE                   // P2P Operate message response.
)

type Rescipient

type Rescipient struct {
	NodesTable *NodesTable        // The nodes table.
	MessageOp  P2PMessageOperator // The P2P message operator.
}

Rescipient the node's ask an message.

func (*Rescipient) NSTexec

func (o *Rescipient) NSTexec(ce *nst2.ConnExec) (ss nst2.SendStat, err error)

The nst2 ConnExecer interface.

func (*Rescipient) TableSet

func (o *Rescipient) TableSet()

Set the node table to P2P message operator

type SelfNodesInfo

type SelfNodesInfo struct {
	Hash string   // Self identity hashid.
	Port string   // Self service port.
	Type NodeType // The node type.
}

This is local nodes' information.

func (SelfNodesInfo) MarshalBinary

func (o SelfNodesInfo) MarshalBinary() (data []byte, err error)

func (*SelfNodesInfo) UnmarshalBinary

func (o *SelfNodesInfo) UnmarshalBinary(data []byte) (err error)

Jump to

Keyboard shortcuts

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