mdnsrpc

package module
v0.0.0-...-67b3275 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2014 License: GPL-2.0 Imports: 12 Imported by: 5

README

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Publish

func Publish(name string, service interface{}) (unpublish chan struct{}, err error)

Publish will serve service registered as "rpc" using net/rpc on a randomly chosen port on 127.0.0.1, publishing it using mDNS as _name_._tcp.

func Service

func Service(service interface{}) (addr *net.TCPAddr, shutdown chan struct{}, err error)

Service will serve service registered as "rpc" using net/rpc on a randomly chosen port on 127.0.0.1.

Types

type Client

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

A wrapper for *rpc.Client that removes the wrapped *rpc.Client from the cache if it produces errors.

func Connect

func Connect(addr string) (result *Client, err error)

Connect returns a *Client to the addr.

func LookupOne

func LookupOne(name string) (result *Client, err error)

LookupOne will use mDNS to lookup exactly one service named _name._tcp and return a *Client for it.

If a lookup has already been made earlier, the cached results will be returned and then a new lookup made in the background.

Example
package main

import (
	"fmt"
)

type Calculator struct{}

type Numbers []int

func (self *Calculator) Add(numbers Numbers, result *int) (err error) {
	for _, num := range numbers {
		*result += num
	}
	return
}

func main() {
	calc := &Calculator{}
	if _, err := Publish("Calculator", calc); err != nil {
		panic(err)
	}
	cli, err := LookupOne("Calculator")
	if err != nil {
		panic(err)
	}
	res := 0
	if err := cli.Call("Add", Numbers{1, 2, 3}, &res); err != nil {
		panic(err)
	}
	fmt.Println("Sum is", res)
}
Output:

Sum is 6

func (*Client) Call

func (self *Client) Call(service string, input, output interface{}) (err error)

Call works just like http://golang.org/pkg/net/rpc/#Client.Call except that it prepends "rpc." to the serviceMethod, and also removes the client from the cache if an error is returned.

func (*Client) Go

func (self *Client) Go(serviceMethod string, args interface{}, reply interface{}, done chan *rpc.Call) (result *rpc.Call)

Go works just like http://golang.org/pkg/net/rpc/#Client.Go except that it prepends "rpc." to the serviceMethod, and also removes the client from the cache if an error is returned.

type Clients

type Clients []*Client

func LookupAll

func LookupAll(name string) (result Clients, err error)

LookupAll will use mDNS to lookup all services named _name._tcp and return a slice of *Client for them.

If a lookup has already been made earlier, the cached results will be returned and then a new lookup made in the background.

Example
package main

import "fmt"

type Generator int

func (self Generator) Generate(unused struct{}, result *int) (err error) {
	*result = int(self)
	return
}

func main() {
	gen1 := Generator(1)
	if _, err := Publish("Generator", gen1); err != nil {
		panic(err)
	}
	gen2 := Generator(2)
	if _, err := Publish("Generator", gen2); err != nil {
		panic(err)
	}
	gen3 := Generator(3)
	if _, err := Publish("Generator", gen3); err != nil {
		panic(err)
	}
	generators, err := LookupAll("Generator")
	if err != nil {
		panic(err)
	}
	res := 0
	for _, client := range generators {
		n := 0
		if err := client.Call("Generate", struct{}{}, &n); err != nil {
			panic(err)
		}
		res += n
	}
	fmt.Println("Sum is", res)
}
Output:

Sum is 6

func (Clients) Equals

func (self Clients) Equals(o Clients) bool

func (Clients) Len

func (self Clients) Len() int

func (Clients) Less

func (self Clients) Less(i, j int) bool

func (Clients) Swap

func (self Clients) Swap(i, j int)

type NoSuchService

type NoSuchService string

NoSuchService is returned when LookupAll fails to find a single service.

func (NoSuchService) Error

func (self NoSuchService) Error() string

type NotOneService

type NotOneService struct {
	Name  string
	Found map[string]struct{}
}

NotOneService is returned when LookupOne fails to find exactly one service.

func (NotOneService) Error

func (self NotOneService) Error() string

Jump to

Keyboard shortcuts

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