Documentation ¶
Overview ¶
In addition to a regular plugin that provides an API, this package can be used for plugins that consume an API provided by the main process. To see an example of this, look in the examples/consumer folder.
Index ¶
- func NewConsumer() *rpc.Client
- func NewConsumerCodec(f func(io.ReadWriteCloser) rpc.ClientCodec) *rpc.Client
- func StartProvider(output io.Writer, path string, args ...string) (*rpc.Client, error)
- func StartProviderCodec(f func(io.ReadWriteCloser) rpc.ClientCodec, output io.Writer, path string, ...) (*rpc.Client, error)
- type Server
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewConsumer ¶
NewConsumer returns an rpc.Client that will consume an API from the host process over this application's Stdin and Stdout using gob encoding.
func NewConsumerCodec ¶
func NewConsumerCodec(f func(io.ReadWriteCloser) rpc.ClientCodec) *rpc.Client
NewConsumerCodec returns an rpc.Client that will consume an API from the host process over this application's Stdin and Stdout using the ClientCodec returned by f.
Example ¶
This function should be called from the plugin program that wants to consume an API from the master program.
This example shows the plugin creating a JSON-RPC client talks to the host application.
package main import ( "net/rpc/jsonrpc" "github.com/natefinch/pie" ) func main() { client := pie.NewConsumerCodec(jsonrpc.NewClientCodec) var reply string client.Call("Foo.ToUpper", "something", &reply) }
Output:
func StartProvider ¶
StartProvider start a provider-style plugin application at the given path and args, and returns an RPC client that communicates with the plugin using gob encoding over the plugin's Stdin and Stdout. The writer passed to output will receive output from the plugin's stderr. Closing the RPC client returned from this function will shut down the plugin application.
func StartProviderCodec ¶
func StartProviderCodec( f func(io.ReadWriteCloser) rpc.ClientCodec, output io.Writer, path string, args ...string, ) (*rpc.Client, error)
StartProviderCodec starts a provider-style plugin application at the given path and args, and returns an RPC client that communicates with the plugin using the ClientCodec returned by f over the plugin's Stdin and Stdout. The writer passed to output will receive output from the plugin's stderr. Closing the RPC client returned from this function will shut down the plugin application.
Example ¶
This function should be called from the master program that wants to run plugins to extend its functionality.
This example shows the master program starting a plugin at path "/var/lib/foo", using JSON-RPC, and writing its output to this application's Stderr. The application can then call methods on the rpc client returned using the standard rpc pattern.
package main import ( "log" "os" "net/rpc/jsonrpc" "github.com/natefinch/pie" ) func main() { client, err := pie.StartProviderCodec(jsonrpc.NewClientCodec, os.Stderr, "/var/lib/foo") if err != nil { log.Fatalf("failed to load foo plugin: %s", err) } var reply string client.Call("Foo.ToUpper", "something", &reply) }
Output:
Types ¶
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is a type that represents an RPC server that serves an API over stdin/stdout.
func NewProvider ¶
func NewProvider() Server
NewProvider returns a Server that will serve RPC over this application's Stdin and Stdout. This method is intended to be run by the plugin application.
func StartConsumer ¶
StartConsumer starts a consumer-style plugin application with the given path and args, writing its stderr to output. The plugin consumes an API this application provides. The function returns the Server for this host application, which should be used to register APIs for the plugin to consume.
func (Server) Close ¶
Close closes the connection with the client. If the client is a plugin process, the process will be stopped. Further communication using this Server will fail.
func (Server) Register ¶
Register publishes in the provider the set of methods of the receiver value that satisfy the following conditions:
- exported method
- two arguments, both of exported type
- the second argument is a pointer
- one return value, of type error
It returns an error if the receiver is not an exported type or has no suitable methods. It also logs the error using package log. The client accesses each method using a string of the form "Type.Method", where Type is the receiver's concrete type.
func (Server) RegisterName ¶
RegisterName is like Register but uses the provided name for the type instead of the receiver's concrete type.
func (Server) Serve ¶
func (s Server) Serve()
Serve starts the Server's RPC server, serving via gob encoding. This call will block until the client hangs up.
func (Server) ServeCodec ¶
func (s Server) ServeCodec(f func(io.ReadWriteCloser) rpc.ServerCodec)
ServeCodec starts the Server's RPC server, serving via the encoding returned by f. This call will block until the client hangs up.
Directories ¶
Path | Synopsis |
---|---|
examples
|
|
consumer/master_consumer
Command example_host is an example of a main application that provides an API for plugins to consume.
|
Command example_host is an example of a main application that provides an API for plugins to consume. |
consumer/plugin_consumer
Command example_consumer is a simple example of a consumer-type plugin.
|
Command example_consumer is a simple example of a consumer-type plugin. |
provider/master_provider
Command example_master is a simple example of a master application that runs a standard provider plugin.
|
Command example_master is a simple example of a master application that runs a standard provider plugin. |
provider/plugin_provider
Command example_plugin is an example of a very simple plugin.
|
Command example_plugin is an example of a very simple plugin. |