node

package
v0.1.28 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 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"

	"bitbucket.org/taubyte/go-sdk/event"
	"bitbucket.org/taubyte/go-sdk/p2p/node"
)

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

	// Instantiate a command `someCommand` to protocol `/test/v1`
	command, err := service.Command("someCommand")
	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(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

Example
package main

import (
	"fmt"

	"bitbucket.org/taubyte/go-sdk/p2p/node"
)

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

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

/test/v1

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

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"

	"bitbucket.org/taubyte/go-sdk/p2p/node"
)

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

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

	fmt.Println(projectProtocol)
}
Output:

hash(project + service)/test/v1

func (*Service) Protocol

func (s *Service) Protocol() string

Returns the protocol of a given service

Jump to

Keyboard shortcuts

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