node

package
Version: v0.1.39 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2022 License: GPL-3.0 Imports: 3 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

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 "bitbucket.org/taubyte/go-sdk-symbols/p2p/node"
	"bitbucket.org/taubyte/go-sdk/event"
	"bitbucket.org/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!

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 "bitbucket.org/taubyte/go-sdk-symbols/p2p/node"
	"bitbucket.org/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 "bitbucket.org/taubyte/go-sdk-symbols/p2p/node"
	"bitbucket.org/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 "bitbucket.org/taubyte/go-sdk-symbols/p2p/node"
	"bitbucket.org/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