datahop

package
v0.0.16 Latest Latest
Warning

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

Go to latest
Published: May 3, 2022 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Overview

Package datahop is a mobile client for running a minimalistic datahop ipfslite node.

Datahop ipfslite node uses a persistent repo and a config file same as go-ipfs to save necessary config options. It uses a global "datahop" object to run the ipfslite node.

As this package is built keeping the mobile platform in mind (using gomobile), all the functions deals with string or byte array to support gomobile datatype.

To create the global "datahop" object and a persistent repository the client has to call "Init". It takes the location to create the repository and an "ConnectionManager".

type ConnManager struct{}
func (m ConnManager) PeerConnected(s string) {
	// do something
}
func (m ConnManager) PeerDisconnected(s string) {
	// do something
}

...

type DiscoveryDriver struct{}
func (m DiscoveryDriver) Start(localPID, peerInfo string, scanTime int, interval int) {
	// do nothing
}

func (m DiscoveryDriver) AddAdvertisingInfo(topic string, info string) {
	// do nothing
}

...

type AdvertisingDriver struct{}
func (m AdvertisingDriver) Start(localPID, peerInfo string) {
	// do nothing
}

func (m AdvertisingDriver) AddAdvertisingInfo(topic string, info string) {
	// do nothing
}

func (m AdvertisingDriver) Stop() {
	// do nothing
}

func (m AdvertisingDriver) NotifyNetworkInformation(network string, pass string) {
	// do nothing
}

func (m AdvertisingDriver) NotifyEmptyValue() {
	// do nothing
}

...

type WifiHotspot struct{}
func (m WifiHotspot) Start() {
	// do nothing
}

func (m WifiHotspot) Stop() {
	// do nothing
}

...

type WifiConnection struct{}
	func (m WifiConnection) Connect(network, pass, ip, host string) {
	// do nothing
}

func (m WifiConnection) Disconnect() {
	// do nothing
}

func (m WifiConnection) Host() string {
	return ""
}

...

cm := ConnManager{}
err := Init(root, cm)
if err != nil {
	panic(err)
}

Start the datahop ipfslite node by calling "Start". It takes one parameter to bootstrap the node with default datahop bootstrap node

var shouldBootstrap bool
err := Start(shouldBootstrap)
if err != nil {
	panic(err)
}

To be able to connect datahop ipfslite node to a private network node call "StartPrivate". It takes two parameters "shouldBootstrap" & "secret_string"

var shouldBootstrap bool
var swarmKey []byte
err := StartPrivate(shouldBootstrap, swarmKey)
if err != nil {
	panic(err)
}

To check if node is online

is Online := IsNodeOnline()

BootstrapWithAddress will take a node id and bootstrap with it

err := BootstrapWithAddress("/ip4/52.66.216.67/tcp/4501/p2p/QmcWEJqQD3bPMT5Mr7ijdwVCmVjUh5Z7CysiTQPgr2VZBC")
if err != nil {
	panic(err)
}

BootstrapWithPeerInfo will take peer info of a node in bytes, then bootstrap with it

err := BootstrapWithPeerInfo("some peerinfo in byte array form")
if err != nil {
	panic(err)
}

To check how much storage the ipfslite node has taken up

storageAllocated, err := DiskUsage()
if err != nil {
	panic(err)
}

All the ipfslite node id related information can be obtained by the following functions. See the corresponding function definitions for more information

id := ID()

...

addresses := Addrs()

...

addresses := InterfaceAddrs()

...

addresses := PeerInfo()

"PeerInfo" returns a string of the peer.AddrInfo []byte of the node. we can actually pass this around to connect with other nodes

peerInfo := PeerInfo()

...

// On some other node
err := ConnectWithPeerInfo(peerInfo)
if err != nil {
	panic(err)
}

Clients can connect with each other using peer address aswell

err := ConnectWithAddress(otherPeerAddress)
if err != nil {
	panic(err)
}

To see all the connected peers

connectedPeers := Peers()

To stop the ipfslite node

Stop()

To close the global "datahop"

Close()

To check "package" version

version := Version()

StartDiscovery starts D2D discovery. It takes three boolean values as parameter.

  • "advertising" : To Advertise BLE Scanning

  • "scanning" : To Scan BLE advertisements from other devices

  • "autoDisconnect" : Auto disconnect from a D2D discovered network

    var ( advertising bool scanning bool autoDisconnect bool ) err := StartDiscovery(advertising, scanning, autoDisconnect) if err != nil { panic(err) }

StopDiscovery stops D2D discovery.

err := StopDiscovery()
if err != nil {
	panic(err)
}

UpdateTopicStatus will update the value of a BLE topic

err := UpdateTopicStatus("myTopic", "My new value")
if err != nil {
	panic(err)
}

Add content into the node as well as network. It takes three params

  • "tag": a unique string to tag the given content in the network

  • "content": the content to add in the network in byte array form

  • "passphrase": to encrypt the content. pass "" (blank string) to add content without encryption

    err := Add("myTag", []byte("myContent", "")) if err != nil { panic(err) }

Get content from the network by name tag. It takes two params

  • "tag": a unique string tag for getting content from the network

  • "passphrase": to decrypt the content. pass "" (blank string) to get content without encryption

    err := Get("myTag", "")) if err != nil { panic(err) }

DownloadsInProgress will show how many downloads are currently in progress

GetTags will show available tags in the local node

Matrix will return node connection and content distribution matrix

State will return current bloom filter state for local content

FilterFromState will extract bloom filter string from State

Index

Constants

View Source
const (
	NoPeersConnected = "No Peers connected"
	CRDTStatus       = "datahop-crdt-status"
)
View Source
const (
	FloodSubID = protocol.ID("/hopfloodsub/1.0.0")
	TopicCRDT  = "CRDTStateLine"
)
View Source
const DiscoveryServiceTag = "_datahop-discovery._ble"

Variables

View Source
var (
	ErrNodeNotRunning       = fmt.Errorf("datahop is not initialised yet")
	ErrBlankGroupKey        = fmt.Errorf("private network secret cannot be blank")
	ErrAdvScan              = fmt.Errorf("no advertising and no scanning enabled")
	ErrDisc                 = fmt.Errorf("discovery service is not initialised")
	ErrEncryptionPassphrase = fmt.Errorf("encryption passphrase cannot be blank")
)

Functions

func Add

func Add(tag string, content []byte, passphrase string) error

Add adds a record in the store

func AddDir added in v0.0.16

func AddDir(tag string, path string) error

AddDir adds a directory in the store

func AddMember added in v0.0.16

func AddMember(peerID, groupIDString string) error

AddMember

func Addrs

func Addrs() ([]byte, error)

Addrs Returns a comma(,) separated string of all the possible addresses of a node

func BootstrapWithAddress added in v0.0.13

func BootstrapWithAddress(bootstrapAddress string) error

BootstrapWithAddress bootstraps to a given address of a node

func BootstrapWithPeerInfo added in v0.0.13

func BootstrapWithPeerInfo(peerInfoByteString string) error

BootstrapWithPeerInfo bootstraps to a given peerInfo string of a node

func Close

func Close()

Close the repo and all

func ConnectWithAddress

func ConnectWithAddress(address string) error

ConnectWithAddress Connects to a given peer address

func ConnectWithPeerInfo

func ConnectWithPeerInfo(peerInfoByteString string) error

ConnectWithPeerInfo Connects to a given peerInfo string

func CreateGroup added in v0.0.16

func CreateGroup(name string) (string, error)

CreateGroup

func CreateOpenGroup added in v0.0.16

func CreateOpenGroup(name string) (string, error)

CreateOpenGroup

func DiskUsage

func DiskUsage() (int64, error)

DiskUsage returns number of bytes stored in the datastore

func DownloadsInProgress added in v0.0.12

func DownloadsInProgress() int

func FilterFromState added in v0.0.11

func FilterFromState() (string, error)

FilterFromState returns the bloom filter from state

func Get

func Get(tag string, passphrase string) ([]byte, error)

Get gets a record from the store by given tag

func GetAllGroups added in v0.0.16

func GetAllGroups() ([]byte, error)

GetAllGroups

func GetGroupName added in v0.0.16

func GetGroupName(peerID, groupIDString string) (string, error)

GetGroupName

func GetTags

func GetTags() ([]byte, error)

GetTags gets all the tags from the store

func GroupAdd added in v0.0.16

func GroupAdd(tag string, content []byte, passphrase, groupID string) error

GroupAdd adds a record in a group

func GroupAddDir added in v0.0.16

func GroupAddDir(tag, path, groupID string) error

GroupAddDir adds a directory in a group

func ID

func ID() (string, error)

ID Returns peerId of the node

func Index added in v0.0.16

func Index() (string, error)

Index gets all content metatags from the store

func Init

func Init(
	root string,
	connManager ConnectionManager,
	discDriver DiscoveryDriver,
	advDriver AdvertisingDriver,
	hs WifiHotspot,
	con WifiConnection,
) error

Init Initialises the .datahop repo, if required at the given location with the given swarm port as config. Default swarm port is 4501

func IsNodeOnline

func IsNodeOnline() bool

IsNodeOnline Checks if the node is running

func Matrix added in v0.0.11

func Matrix() (string, error)

Matrix returns matrix measurements

func PeerInfo

func PeerInfo() string

PeerInfo Returns a string of the peer.AddrInfo []byte of the node

func Peers

func Peers() ([]byte, error)

Peers Returns a comma(,) separated string of all the connected peers of a node

func Start

func Start(bootstrap, autoDownload bool) error

Start an ipfslite node in a go routine

func StartDiscovery

func StartDiscovery(advertise, scan, autoDisconnect bool) error

StartDiscovery starts BLE discovery

func StartPrivate added in v0.0.13

func StartPrivate(swarmKey string, autoDownload bool) error

StartPrivate starts an ipfslite node in a private network with provided swarmkey

func State

func State() ([]byte, error)

State returns number of keys in crdt store

func Stop

func Stop()

Stop the node

func StopDiscovery

func StopDiscovery() error

StopDiscovery stops BLE discovery

func UpdateTopicStatus

func UpdateTopicStatus(topic string, value string)

UpdateTopicStatus adds BLE advertising info

func Version

func Version() string

Version of ipfs-lite

Types

type AdvertisementNotifier

type AdvertisementNotifier interface {
	AdvertiserPeerSameStatus()
	AdvertiserPeerDifferentStatus(topic string, value []byte, peerinfo string)
}

func GetAdvertisementNotifier

func GetAdvertisementNotifier() AdvertisementNotifier

GetAdvertisementNotifier returns advertisement notifier

type AdvertisingDriver

type AdvertisingDriver interface {
	// Start the native driver
	Start(localPID, peerinfo string)
	AddAdvertisingInfo(topic string, info string)
	// Stop the native driver
	Stop()
	NotifyNetworkInformation(network string, pass string)
	NotifyEmptyValue()
}

type ConnectionManager

type ConnectionManager interface {
	PeerConnected(string)
	PeerDisconnected(string)
}

ConnectionManager is used by clients to get notified client connection

type DiscoveryDriver

type DiscoveryDriver interface {
	// Start the native driver
	Start(localPID, peerinfo string, scanTime int, interval int)
	AddAdvertisingInfo(topic string, info string)
	// Stop the native driver
	Stop()
}

type DiscoveryNotifier

type DiscoveryNotifier interface {
	DiscoveryPeerSameStatus(device string, topic string)
	DiscoveryPeerDifferentStatus(device string, topic string, network string, pass string, info string)
}

func GetDiscoveryNotifier

func GetDiscoveryNotifier() DiscoveryNotifier

GetDiscoveryNotifier returns discovery notifier

type DiscoveryOpts added in v0.0.14

type DiscoveryOpts struct {
	Advertise      bool
	Scan           bool
	AutoDisconnect bool
}

type Message added in v0.0.12

type Message struct {
	Id        string
	CRDTState string
}

func (*Message) Marshal added in v0.0.12

func (m *Message) Marshal() ([]byte, error)

func (*Message) Unmarshal added in v0.0.12

func (m *Message) Unmarshal(val []byte) error

type Notifee

type Notifee interface {
	HandlePeerFound(string)
}

type Service

type Service interface {
	io.Closer
	RegisterNotifee(Notifee)
	UnregisterNotifee(Notifee)
}

func NewDiscoveryService

func NewDiscoveryService(
	id string,
	discDriver DiscoveryDriver,
	advDriver AdvertisingDriver,
	scanTime int,
	interval int,
	hs WifiHotspot,
	con WifiConnection,
	serviceTag string,
) (Service, error)

type ServiceType added in v0.0.11

type ServiceType string
const (
	ScanAndAdvertise ServiceType = "Both"
	OnlyScan         ServiceType = "OnlyScan"
	OnlyAdv          ServiceType = "OnlyAdv"
)

type StartsOpts added in v0.0.14

type StartsOpts struct {
	ShouldBootstrap bool
}

type WifiConnection

type WifiConnection interface {
	Connect(network, pass, ip, host string)
	Disconnect()
	Host() string
}

type WifiConnectionNotifier

type WifiConnectionNotifier interface {
	OnConnectionSuccess(started, completed int64, rssi, speed, freq int)
	OnConnectionFailure(code int, started, failed int64)
	OnDisconnect()
}

func GetWifiConnectionNotifier

func GetWifiConnectionNotifier() WifiConnectionNotifier

GetWifiConnectionNotifier returns wifi connection notifier

type WifiHotspot

type WifiHotspot interface {
	Start() //(string, string)
	Stop()
}

type WifiHotspotNotifier

type WifiHotspotNotifier interface {
	OnSuccess()
	OnFailure(code int)
	StopOnSuccess()
	StopOnFailure(code int)
	NetworkInfo(network string, password string)
	ClientsConnected(num int)
}

func GetWifiHotspotNotifier

func GetWifiHotspotNotifier() WifiHotspotNotifier

GetWifiHotspotNotifier returns wifi hotspot notifier

Jump to

Keyboard shortcuts

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