bifrost

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2019 License: Apache-2.0 Imports: 12 Imported by: 0

README

Bifrost

Build Status License Language Coverage Status

Bifrost is a grpc-based P2P network library

(In Norse mythology, Bifröst is a burning rainbow bridge that reaches between Midgard and Asgard, the realm of the gods.)

Getting Started with Bifrost

Installation
go get -u github.com/DE-labtory/bifrost
Usage

Server

import (
  "github.com/DE-labtory/bifrost/pb"
	"github.com/DE-labtory/bifrost/server"
)

// connection control
ConnectionStore := bifrost.NewConnectionStore()

// server listen
s := server.New(bifrost.KeyOpts{PriKey: priKey, PubKey: pubKey})
server.Listen(_gRPCatewayIP_)

Client

import (
  "github.com/DE-labtory/bifrost/client"
)
connection, err := client.Dial(command.Address, clientOpt, grpcOpt) //흔히 cli 에서 이루어짐
connection.send(payload []byte, protocol string, successCallBack func(interface{}), errCallBack func(error)) //connection GrpcConnection

Document

process buffer

bifrost 가 grpc 서비스를 구축하기 위해 stream.proto file 을 작성하였으며, 이는 protoc 에 의해 compile 되어 go 코드로 변환된다. stream.proto file 에 대해 간단히 소개하자면 StreamService 라는 구조체를 기반으로 BifrostStream이라는 함수를 가지며, Envelope 라는 자료형을 추가로 구성한다.

서버측에서 RegisterStreamServiceServer 를 통해 서버를 등록하고 클라이언트 측에서 NewStreamServiceClient 를 통해 streamServiceClient 객체를 생성하고, streamServiceClient.BifrostStream 을 호출하는 방식으로 Envelop 를 송수신 한다.

data structure

Bifrost Data Structure

Lincese

Bifrost source code files are made available under the Apache License, Version 2.0 (Apache-2.0), located in the LICENSE file.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrConnAlreadyExist = errors.New("connection already exist in store")
View Source
var ErrConnNotExist = errors.New("connection not exist in store")
View Source
var ErrIPv4Format = errors.New("invalid IPv4 format")

Functions

func BuildResponsePeerInfo

func BuildResponsePeerInfo(ip string, pubKey Key, metaData map[string]string) (*pb.Envelope, error)

func RecvWithTimeout

func RecvWithTimeout(timeout time.Duration, stream Stream) (*pb.Envelope, error)

Types

type Address

type Address struct {
	IP string
}

Address to connect other peer

func ToAddress

func ToAddress(ipv4 string) (Address, error)

format should be xxx.xxx.xxx.xxx:xxxx

type CStreamWrapper

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

func (*CStreamWrapper) Close

func (csw *CStreamWrapper) Close()

func (*CStreamWrapper) GetStream

func (csw *CStreamWrapper) GetStream() Stream

func (*CStreamWrapper) Recv

func (csw *CStreamWrapper) Recv() (*pb.Envelope, error)

func (*CStreamWrapper) Send

func (csw *CStreamWrapper) Send(envelope *pb.Envelope) error

type ConnID

type ConnID = string

type Connection

type Connection interface {
	Send(data []byte, protocol string, successCallBack func(interface{}), errCallBack func(error))
	Close()
	GetIP() Address
	GetPeerKey() Key
	GetID() ConnID
	GetMetaData() map[string]string
	Start() error
	Handle(handler Handler)
}

func NewConnection

func NewConnection(ip string, metaData map[string]string, peerKey Key, streamWrapper StreamWrapper, crypto Crypto) (Connection, error)

type ConnectionID

type ConnectionID string

type ConnectionStore

type ConnectionStore struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewConnectionStore

func NewConnectionStore() *ConnectionStore

func (ConnectionStore) AddConnection

func (connStore ConnectionStore) AddConnection(conn Connection) error

func (ConnectionStore) DeleteConnection

func (connStore ConnectionStore) DeleteConnection(connID ConnID) error

func (ConnectionStore) GetConnection

func (connStore ConnectionStore) GetConnection(connID ConnID) (Connection, error)

type Crypto added in v0.2.0

type Crypto struct {
	Signer
	Verifier
	KeyRecoverer
}

type GrpcConnection

type GrpcConnection struct {
	ID ConnID

	sync.RWMutex

	Crypto
	// contains filtered or unexported fields
}

func (*GrpcConnection) Close

func (conn *GrpcConnection) Close()

func (*GrpcConnection) GetID

func (conn *GrpcConnection) GetID() ConnID

func (*GrpcConnection) GetIP

func (conn *GrpcConnection) GetIP() Address

func (*GrpcConnection) GetMetaData added in v0.1.11

func (conn *GrpcConnection) GetMetaData() map[string]string

func (*GrpcConnection) GetPeerKey

func (conn *GrpcConnection) GetPeerKey() Key

func (*GrpcConnection) Handle

func (conn *GrpcConnection) Handle(handler Handler)

func (*GrpcConnection) Send

func (conn *GrpcConnection) Send(payload []byte, protocol string, successCallBack func(interface{}), errCallBack func(error))

func (*GrpcConnection) Start

func (conn *GrpcConnection) Start() error

func (*GrpcConnection) Verify added in v0.2.0

func (conn *GrpcConnection) Verify(envelope *pb.Envelope) bool

type Handler

type Handler interface {
	ServeRequest(msg Message)
}

type Key added in v0.2.1

type Key interface {
	ID() KeyID
	ToByte() ([]byte, error)
	IsPrivate() bool
}

type KeyID added in v0.2.0

type KeyID = string

type KeyOpts

type KeyOpts struct {
	PriKey Key
	PubKey Key
}

type KeyRecoverer added in v0.2.1

type KeyRecoverer interface {
	RecoverKeyFromByte(keyBytes []byte, isPrivateKey bool) (Key, error)
}

type Message

type Message struct {
	Envelope *pb.Envelope
	Data     []byte
	Conn     Connection
}

func (*Message) Respond

func (m *Message) Respond(data []byte, protocol string, successCallBack func(interface{}), errCallBack func(error))

Respond sends a msg to the source that sent the ReceivedMessageImpl

type PeerInfo

type PeerInfo struct {
	IP          string
	PubKeyBytes []byte
	IsPrivate   bool
	MetaData    map[string]string
}

type SStreamWrapper

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

server stream wrapper

func (*SStreamWrapper) Close

func (ssw *SStreamWrapper) Close()

func (*SStreamWrapper) GetStream

func (ssw *SStreamWrapper) GetStream() Stream

func (*SStreamWrapper) Recv

func (ssw *SStreamWrapper) Recv() (*pb.Envelope, error)

func (*SStreamWrapper) Send

func (ssw *SStreamWrapper) Send(envelope *pb.Envelope) error

type Signer added in v0.2.0

type Signer interface {
	Sign(message []byte) ([]byte, error)
}

type Stream

type Stream interface {
	Send(*pb.Envelope) error
	Recv() (*pb.Envelope, error)
}

type StreamWrapper

type StreamWrapper interface {
	Stream
	Close()
	GetStream() Stream
}

func NewClientStreamWrapper

func NewClientStreamWrapper(conn *grpc.ClientConn) (StreamWrapper, error)

client stream wrapper

func NewServerStreamWrapper

func NewServerStreamWrapper(serverStream pb.StreamService_BifrostStreamServer, cancel context.CancelFunc) StreamWrapper

type Verifier added in v0.2.0

type Verifier interface {
	Verify(peerKey Key, signature, message []byte) (bool, error)
}

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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