Back to godoc.org
berty.tech/berty/v2 / go / framework / bertybridge

Package bertybridge

v2.190.2
Latest Go to latest

The latest major version is v2.

Published: 1 day ago | Licenses: Apache-2.0 , MIT | Module: berty.tech/berty/v2

Overview

Package bertybridge is the main gomobile entrypoint, used to generate iOS and Android frameworks.

Example

Code:

package main

import (
	"encoding/base64"
	"fmt"
	"io/ioutil"
	"os"

	"github.com/gogo/protobuf/proto"

	"berty.tech/berty/v2/go/framework/bertybridge"
	"berty.tech/berty/v2/go/framework/bertybridge/internal/bridgepb"
	"berty.tech/berty/v2/go/pkg/bertytypes"
)

func main() {
	tmpdir, err := ioutil.TempDir("", "example")
	checkErr(err)
	defer os.RemoveAll(tmpdir)

	// create and start the bridge
	var bridge *bertybridge.Bridge
	{
		config := bertybridge.NewConfig()
		{
			if false { // disabled in example, but not commented to be sure that compiler performs various checks
				config.SetLifeCycleDriver(nil)
				config.SetLoggerDriver(nil)
				config.SetNotificationDriver(nil)
			}
			config.AppendCLIArg("--log.filters=info+:bty*,-*.grpc warn+:*.grpc error+:*")
			config.AppendCLIArg("--log.format=console")
			config.AppendCLIArg("--node.display-name=")
			config.AppendCLIArg("--node.listeners=/ip4/127.0.0.1/tcp/0/grpcws")
			config.AppendCLIArg("--p2p.ipfs-listeners=/ip4/0.0.0.0/tcp/0,/ip6/0.0.0.0/tcp/0")
			config.AppendCLIArg("--p2p.local-discovery=false")
			config.AppendCLIArg("--p2p.webui-listener=:3000")
			config.AppendCLIArg("--store.dir=" + tmpdir)
		}

		bridge, err = bertybridge.NewBridge(config)
		checkErr(err)
		defer bridge.Close()
		fmt.Println("[+] initialized.")
	}

	fmt.Println("[+] has grpc-web listener:           ", bridge.GRPCWebListenerAddr() != "")       // no, because `--node.listeners` does not contain grpcweb
	fmt.Println("[+] has websocket listener:          ", bridge.GRPCWebSocketListenerAddr() != "") // yes, because `--node.listeners`` contains grpcws

	// unary client call
	{
		// create `InstanceGetConfiguration` Input
		input := &bertytypes.InstanceGetConfiguration_Request{}
		payload, err := proto.Marshal(input)
		checkErr(err)

		// Serialize request
		req := &bridgepb.ClientInvokeUnary_Request{
			MethodDesc: &bridgepb.MethodDesc{
				Name:           "/berty.protocol.v1.ProtocolService/InstanceGetConfiguration",
				IsClientStream: false,
				IsServerStream: false,
			},
			Payload: payload,
		}
		reqb64, err := encodeProtoMessage(req)
		checkErr(err)

		// invoke through bridge client
		ret, err := bridge.InvokeBridgeMethod("/berty.bridge.v1.BridgeService/ClientInvokeUnary", reqb64)
		checkErr(err)

		// deserialize reply
		var res bridgepb.ClientInvokeUnary_Reply
		err = decodeProtoMessage(ret, &res)
		checkErr(err)

		// check for error
		if res.Error != nil {
			fmt.Fprintf(os.Stderr, "bridge client error: `[%s] %s`\n", res.Error.ErrorCode.String(), res.Error.Message)
			panic(err)
		}

		// deserialize output
		var output bertytypes.InstanceGetConfiguration_Reply
		err = proto.Unmarshal(res.Payload, &output)
		checkErr(err)

		fmt.Println("[+] has more than one swarm listener:", len(output.Listeners) > 1)
		// log.Println("ret", godev.PrettyJSON(output))
	}

}

func checkErr(err error) {
	if err != nil {
		fmt.Fprintf(os.Stderr, "%+v\n", err)
		panic(err)
	}
}

func decodeProtoMessage(input string, output proto.Message) error {
	dec, err := base64.StdEncoding.DecodeString(input)
	if err != nil {
		return err
	}

	return proto.Unmarshal(dec, output)
}

func encodeProtoMessage(input proto.Message) (string, error) {
	data, err := proto.Marshal(input)
	if err != nil {
		return "", err
	}

	return base64.StdEncoding.EncodeToString(data), nil
}
[+] initialized.
[+] has grpc-web listener:            false
[+] has websocket listener:           true
[+] has more than one swarm listener: true

Index

Examples

Constants

const (
	AppStateUnknown int = iota
	AppStateActive
	AppStateInactive
	AppStateBackground
)

type Bridge

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

func NewBridge

func NewBridge(config *Config) (*Bridge, error)

func (*Bridge) Close

func (b *Bridge) Close() error

func (*Bridge) GRPCWebListenerAddr

func (b *Bridge) GRPCWebListenerAddr() string

func (b *Bridge) GRPCListenerAddr() string { return b.getGRPCAddrFor("ip4/tcp/grpc") }

func (*Bridge) GRPCWebSocketListenerAddr

func (b *Bridge) GRPCWebSocketListenerAddr() string

func (*Bridge) HandleState

func (b *Bridge) HandleState(appstate int)

func (*Bridge) HandleTask

func (b *Bridge) HandleTask() LifeCycleBackgroundTask

func (*Bridge) InvokeBridgeMethod

func (b *Bridge) InvokeBridgeMethod(method string, b64message string) (string, error)

func (*Bridge) InvokeBridgeMethodWithPromiseBlock

func (b *Bridge) InvokeBridgeMethodWithPromiseBlock(promise PromiseBlock, method string, b64message string)

func (*Bridge) WillTerminate

func (b *Bridge) WillTerminate()

type Config

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

Config is used to build a bridge configuration using only simple types or types returned by the bridge package.

func NewConfig

func NewConfig() *Config

func (*Config) AppendCLIArg

func (c *Config) AppendCLIArg(arg string)

func (*Config) SetLifeCycleDriver

func (c *Config) SetLifeCycleDriver(lc LifeCycleDriver)

func (*Config) SetLoggerDriver

func (c *Config) SetLoggerDriver(dLogger NativeLoggerDriver)

func (*Config) SetNotificationDriver

func (c *Config) SetNotificationDriver(driver NotificationDriver)

type LifeCycleBackgroundTask

type LifeCycleBackgroundTask interface {
	Execute() (success bool)
	Cancel()
}

type LifeCycleDriver

type LifeCycleDriver interface {
	GetCurrentState() int
	RegisterHandler(handler LifeCycleHandler)
}

type LifeCycleHandler

type LifeCycleHandler interface {
	HandleState(appstate int)
	HandleTask() LifeCycleBackgroundTask
	WillTerminate()
}

type LocalNotification

type LocalNotification struct {
	Title    string
	Body     string
	Interval float64
}

type NativeLoggerDriver

type NativeLoggerDriver interface {
	Log(level, namespace, message string) error
	LevelEnabler(level string) bool
}

type NotificationDriver

type NotificationDriver interface {
	Post(notif *LocalNotification) error
}

type PromiseBlock

type PromiseBlock interface {
	CallResolve(reply string)
	CallReject(error error)
}

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier