plugins

package module
v0.0.0-...-2894f46 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2017 License: MIT Imports: 14 Imported by: 0

README

plugins

Package plugins provides support for creating plugin-based applications.

Following workflow is supposed:

  1. main application creates RPC Host service;
  2. plugin starts and connects to the RPC Host:
    1. plugin informs RPC Host about service, provided by that plugin and requests port to serve on;
    2. RPC Host checks if such service is not provided by other connected plugin and generates port for plugin to serve on;
    3. plugin starts RPC server on port, provided by RPC Host, and informs RPC Host about it;
    4. RPC Host registers plugin service;
  3. when plugin terminates, it informs RPC Host; RPC Host unregisters plugin service;
  4. when RPC Host terminates, it informs all connected plugins; plugins terminates;
  5. when main application need to invoke some service method, RPC Host is used for dispatch it;

For handling remote calls net/rpc package usage is supposed. A plugin must register object(s) that will be used for handling remote calls.

Documentation

Overview

Package plugins provides support for creating plugin-based applications.

Following workflow is supposed:

  1. main application creates RPC Host service;
  2. plugin starts and connects to the RPC Host: 2.1) plugin informs RPC Host about service, provided by that plugin and requests port to serve on; 2.2) RPC Host checks if such service is not provided by other connected plugin and generates port for plugin to serve on; 2.3) plugin starts RPC server on port, provided by RPC Host, and informs RPC Host about it; 2.4) RPC Host registers plugin service;
  3. when plugin terminates, it informs RPC Host; RPC Host unregisters plugin service;
  4. when RPC Host terminates, it informs all connected plugins; plugins terminates;
  5. when main application need to invoke some service method, RPC Host is used for dispatch it;

For handling remote calls net/rpc package usage is supposed. A plugin must register object(s) that will be used for handling remote calls.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrServiceAlreadyRegistered = errors.New("service is already registered")

ErrServiceAlreadyRegistered throws when trying to register already registered service

View Source
var ErrServiceNotRegistered = errors.New("service is not registered")

ErrServiceNotRegistered returns when trying to access non-registered service

Functions

This section is empty.

Types

type Host

type Host interface {
	Addr() net.Addr
	Serve()
	Services() []string
	OnStop(func())
	Logger() *log.Logger
	OnConnectPlugin(f PluginNotifyFunc)
	OnDisconnectPlugin(f PluginNotifyFunc)
	Call(serviceName string, serviceMethod string, args interface{}, reply interface{}) error
}

A Host is a manager that plugins connects to. Host uses connected plugins for handling main application calls.

func NewHost

func NewHost(name string, options ...*Options) (Host, error)

NewHost creates a new Host instance.

Example
package main

import (
	"github.com/kyuri/plugins"
)

func main() {
	if h, err := plugins.NewHost("RPC host", &plugins.Options{tcpPort: 5000}); err == nil {
		h.Serve()
	}
}
Output:

type Options

type Options struct {
	Address string      // TCP or unix socket address for serving connection or connecting to the remote server
	Log     *log.Logger // a object for logging operations
}

Options for RPC client/server.

type Plugin

type Plugin interface {
	Serve()
	Stop()
	Call(serviceMethod string, args interface{}, reply interface{}) error
	Logger() *log.Logger
	OnServe(func())
	OnStop(func())
}

Plugin is used for serving requests from main application.

func NewPlugin

func NewPlugin(pluginName string, serviceName string, options ...*Options) (Plugin, error)

NewPlugin creates a new Plugin instance.

Example
package main

import (
	"github.com/kyuri/plugins"
)

func main() {
	if p, err := plugins.NewPlugin("calculator", "calcService", &plugins.Options{tcpPort: 5000}); err == nil {
		p.Serve()
	}
}
Output:

type PluginInfo

type PluginInfo struct {
	Name        string // Human-readable name of plugin
	ServiceName string // Name of service provided by plugin
	Address     string // Address plugin serves on
}

PluginInfo describes a plugin.

type PluginNotifyFunc

type PluginNotifyFunc func(PluginInfo)

PluginNotifyFunc is a callback function to receive notifications

type RPCHost

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

RPCHost implements Host RPC service. Handles plugins requests.

func (*RPCHost) ConnectPlugin

func (rh *RPCHost) ConnectPlugin(info *PluginInfo, _ *int) error

ConnectPlugin handles plugin connection to host.

func (*RPCHost) DisconnectPlugin

func (rh *RPCHost) DisconnectPlugin(info *PluginInfo, _ *int) error

DisconnectPlugin handles plugin disconnection from host.

func (*RPCHost) GetListenAddr

func (rh *RPCHost) GetListenAddr(info *PluginInfo, listenAddr *string) error

GetListenAddr generates port for plugin to serve on.

type RPCPlugin

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

RPCPlugin implements Plugin RPC service. Handles RPC Host requests.

func (*RPCPlugin) Terminate

func (rp *RPCPlugin) Terminate(_ int, _ *int) error

Terminate handles RPC Host termination.

Directories

Path Synopsis
Mainapp is host application for "calculator" plugin.
Mainapp is host application for "calculator" plugin.

Jump to

Keyboard shortcuts

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