node

package
v0.3.9 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2023 License: MIT Imports: 6 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Discover added in v0.3.6

func Discover(max uint32, timeout time.Duration) ([]cid.Cid, error)
Example
package main

import (
	"fmt"
	"time"

	"github.com/ipfs/go-cid"
	symbols "github.com/taubyte/go-sdk-symbols/p2p/node"
	"github.com/taubyte/go-sdk/p2p/node"
)

var (
	cid1 cid.Cid
	cid2 cid.Cid
	cid3 cid.Cid
)

// Initialize some CIDs test
func init() {
	var err error
	cid1, err = cid.Parse("bafzaajaiaejcatsa2r73dij2iewq47p2c6runxmvht2evx6agmojnk3pjflfcn52")
	if err != nil {
		panic(err)
	}

	cid2, err = cid.Parse("bafzaajaiaejcbiutf54aucf7ej47lrr27fuqarekshylvkbnwzd77p23wc2gzl3s")
	if err != nil {
		panic(err)
	}

	cid3, err = cid.Parse("bafzaajaiaejcbsmuszumjrisswnmvahkpycbjrlzhzrzdjryyqej4kww73kyxnnd")
	if err != nil {
		panic(err)
	}
}

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	symbols.MockData{
		DiscoverMax:     3,
		DiscoverTimeout: uint32(time.Second * 2),
		DiscoverId:      4,
		Peers:           []cid.Cid{cid1, cid2, cid3},
	}.Mock()

	peers, err := node.Discover(3, time.Second*2)
	if err != nil {
		return
	}

	for _, peer := range peers {
		fmt.Println(peer.String())
	}

}
Output:

bafzaajaiaejcatsa2r73dij2iewq47p2c6runxmvht2evx6agmojnk3pjflfcn52
bafzaajaiaejcbiutf54aucf7ej47lrr27fuqarekshylvkbnwzd77p23wc2gzl3s
bafzaajaiaejcbsmuszumjrisswnmvahkpycbjrlzhzrzdjryyqej4kww73kyxnnd

Types

type Command

type Command uint32

A p2p command

func (Command) Send

func (c Command) Send(body []byte) ([]byte, error)

Send returns the command response and an error, used to send data over p2p on given protocol and command.

Example
package main

import (
	"fmt"

	symbols "github.com/taubyte/go-sdk-symbols/p2p/node"
	"github.com/taubyte/go-sdk/event"
	"github.com/taubyte/go-sdk/p2p/node"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	m := symbols.MockData{
		Protocol:     "/test/v1",
		Command:      "someCommand",
		SendResponse: []byte("Hello from the other side!"),
	}.Mock()

	service := node.New(m.Protocol)

	// Instantiate a command `someCommand` to protocol `/test/v1`
	command, err := service.Command(m.Command)
	if err != nil {
		return
	}

	// Send the command with data []byte("Hello, world!")
	response, err := command.Send([]byte("Hello, world!"))
	if err != nil {
		return
	}

	// A function representing the call executed by the above command
	_ = func(e event.Event) uint32 {
		p2pEvent, err := e.P2P()
		if err != nil {
			return 1
		}

		data, err := p2pEvent.Data()
		if err != nil {
			return 1
		}

		if string(data) == "Hello, world!" {
			p2pEvent.Write([]byte("Hello from the other side!"))
			return 0
		}
		return 1
	}

	fmt.Println(string(response))
}
Output:

Hello from the other side!

func (Command) SendTo added in v0.3.6

func (c Command) SendTo(body []byte, cid cid.Cid) ([]byte, error)

SendTo returns the command response and an error, used to send data over p2p on given protocol and command.

Example
// Mocking the calls to the vm for usage in tests and playground
m := symbols.MockData{
	Protocol:     "/test/v1",
	Command:      "someCommand",
	SendResponse: []byte("Hello from the other side!"),
	SendTo:       cid1,
}.Mock()

service := node.New(m.Protocol)

// Instantiate a command `someCommand` to protocol `/test/v1`
command, err := service.Command(m.Command)
if err != nil {
	return
}

// Send the command to cid1 with data []byte("Hello, world!")
response, err := command.SendTo([]byte("Hello, world!"), cid1)
if err != nil {
	return
}

// A function representing the call executed by the above command
_ = func(e event.Event) uint32 {
	p2pEvent, err := e.P2P()
	if err != nil {
		return 1
	}

	data, err := p2pEvent.Data()
	if err != nil {
		return 1
	}

	if string(data) == "Hello, world!" {
		p2pEvent.Write([]byte("Hello from the other side!"))
		return 0
	}
	return 1
}

fmt.Println(string(response))
Output:

Hello from the other side!

type P2PEvent

type P2PEvent uint32

Event sent to a p2p function call

type Service

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

A p2p service with on a given protocol

func New

func New(protocol string) *Service

New returns a p2p Service bound to a given protocol

func (*Service) Command

func (s *Service) Command(command string) (Command, error)

Command returns a p2p Command and an error Used for sending a given command on a protocol

Example
package main

import (
	"fmt"

	symbols "github.com/taubyte/go-sdk-symbols/p2p/node"
	"github.com/taubyte/go-sdk/p2p/node"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	symbols.MockData{
		Protocol:  "/test/v1",
		Command:   "someCommand",
		CommandId: 5,
	}.Mock()

	service := node.New("/test/v1")

	// Instantiate a command `someCommand` to protocol `/test/v1`
	command, err := service.Command("someCommand")
	if err != nil {
		return
	}

	fmt.Println(command)
}
Output:

5

func (*Service) Listen

func (s *Service) Listen() (protocol string, err error)

Listen returns the projectProtocol and an error. Asks a node to listen on a protocol, and returns the protocol the node is set to listen on.

Example
package main

import (
	"fmt"

	symbols "github.com/taubyte/go-sdk-symbols/p2p/node"
	"github.com/taubyte/go-sdk/p2p/node"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	symbols.MockData{
		ListenHash:     "QmZjBpQzR",
		ListenProtocol: "/test/v1",
	}.Mock()

	// Instantiate a service with protocol `/test/v1`
	service := node.New("/test/v1")

	listenProtocol, err := service.Listen()
	if err != nil {
		return
	}

	fmt.Println(listenProtocol)
}
Output:

/QmZjBpQzR/test/v1

func (*Service) Protocol

func (s *Service) Protocol() string

Returns the protocol of a given service

Example
package main

import (
	"fmt"

	symbols "github.com/taubyte/go-sdk-symbols/p2p/node"
	"github.com/taubyte/go-sdk/p2p/node"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	symbols.MockData{
		Protocol: "/test/v1",
	}.Mock()

	service := node.New("/test/v1")

	fmt.Println(service.Protocol())
}
Output:

/test/v1

Jump to

Keyboard shortcuts

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