mqttRpc

package module
v0.0.0-...-ef531ab Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2016 License: GPL-3.0 Imports: 8 Imported by: 0

README

go-mqtt-rpc

Go MQTT RPC using native golang jsonrpc and eclipse MQTT packages

Examples

Server (see full example here)

package main

import (
	"log"
	"github.com/elgutierrez/go-mqtt-rpc"
	mqtt "github.com/eclipse/paho.mqtt.golang"
)

func main() {
	//Get mqtt client instance from somewhere
  mqtt := NewMqttClient()
  config := &mqttRpc.ServerConfig{
    Instance: "device1",
    Prefix: "$rpc",
    QoS: 0,
  }

  errors := make(chan error, 1)
  rpcServer := mqttRpc.NewServer(mqtt, config)
  //Register exposed methods
  rpcServer.Register(new(ServerExposed))
  rpcServer.Start(errors)
}


type PingArgs struct{
}

type PingReply struct{
	Done bool
}

//CLass that exposes the Ping method
type ServerExposed struct{
}

func (r *ServerExposed) Ping(args *PingArgs, reply *PingReply) error{
	log.Println("Received PING request; Sending PONG...")
	reply.Done = true
	return nil
}

Client (see full example here)


package main

import(
	"log"
	"github.com/elgutierrez/go-mqtt-rpc"
	mqtt "github.com/eclipse/paho.mqtt.golang"
)

func main() {
  clientInstance := "client1"
  instanceTo := "device1"
  log.Println("Starting MQTT RPC client: instance", clientInstance, " to ", instanceTo)

  err := doPing(clientInstance, instanceTo)
  if err != nil {
    log.Fatal("Ping error", err)
  }
}

type PingArgs struct{
}

type PingReply struct{
		Done bool
}

func doPing(instance string, instanceTo string) error{
	//Get a connected MQTT client instance from somewhere
  mqtt := NewMqttClient()
  config := &mqttRpc.ClientConfig{
    Instance: instance,
    Prefix: "$rpc",
    QoS: 0,
  }
  rpcClient := mqttRpc.NewClient(mqtt, config)
  
  //Connect to the server instance with a 5000ms timeout
  err := rpcClient.Connect(instanceTo, 5000)
  if err != nil {
    log.Fatal("RPC Cant connect", err)
    return err
  }

  var pingReply PingReply
  log.Println("Calling RemoteRpc.Ping to ", instanceTo)
  //Call the method ServerExposed.Ping with 3000ms timeout
  err = rpcClient.CallTimeout("ServerExposed.Ping", &PingArgs{}, &pingReply, 3000)
  if err != nil {
    log.Fatal("RPC Ping failed", err)
    return err
  }
  log.Println("Received pong ", pingReply)

  return nil
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	Mqtt   mqtt.Client
	Client *rpc.Client
	Config *ClientConfig
}

func NewClient

func NewClient(client mqtt.Client, config *ClientConfig) *Client

func (*Client) Call

func (s *Client) Call(method string, params interface{}, reply interface{}) error

func (*Client) CallTimeout

func (s *Client) CallTimeout(method string, params interface{}, reply interface{}, timeout int) error

func (*Client) Close

func (s *Client) Close()

func (*Client) Connect

func (s *Client) Connect(instanceTo string, timeout int) error

type ClientConfig

type ClientConfig struct {
	Instance string
	Prefix   string
	QoS      byte
}

type MqttRpcConn

type MqttRpcConn struct {
	Mqtt       mqtt.Client
	Inbound    chan []byte
	ReadTopic  string
	WriteTopic string
	QoS        byte
	Instance   string
}

func NewMqttRpcConn

func NewMqttRpcConn(mqtt mqtt.Client, read string, write string, qos byte) *MqttRpcConn

func (*MqttRpcConn) Close

func (m *MqttRpcConn) Close() error

func (*MqttRpcConn) Open

func (m *MqttRpcConn) Open() error

func (*MqttRpcConn) Read

func (m *MqttRpcConn) Read(p []byte) (int, error)

func (*MqttRpcConn) Write

func (m *MqttRpcConn) Write(p []byte) (int, error)

type Server

type Server struct {
	Mqtt   mqtt.Client
	Config *ServerConfig
}

func NewServer

func NewServer(client mqtt.Client, config *ServerConfig) *Server

func (*Server) Register

func (r *Server) Register(serverMethods interface{}) error

func (*Server) ServeConnection

func (r *Server) ServeConnection(instanceTo string, errors chan error)

func (*Server) Start

func (r *Server) Start(errors chan error)

type ServerConfig

type ServerConfig struct {
	Prefix   string
	QoS      byte
	Instance string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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