kodirpc

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2016 License: MIT Imports: 8 Imported by: 7

README

Build Status GoDoc License-MIT

kodirpc

-- import "github.com/pdf/kodirpc"

Package kodirpc is a JSON-RPC client for the TCP socket of the Kodi home theatre software.

Usage

const (
	// DefaultReadTimeout is the default time a call will wait for a response.
	DefaultReadTimeout = 5 * time.Second
	// DefaultConnectTimeout is the default time re-/connection will be
	// attempted before failure.
	DefaultConnectTimeout = 5 * time.Minute
	// DefaultReconnect determines whether the client reconnects by default.
	DefaultReconnect = true
	// DefaultConnectBackoffScale is the default back-off scaling factor
	DefaultConnectBackoffScale = 2
)
func SetLogger
func SetLogger(l LevelledLogger)

SetLogger enables logging for the library and wraps the supplied logger with a logPrefixer to denote locally generated logs

type Client
type Client struct {
	sync.RWMutex
}

Client is a TCP JSON-RPC client for Kodi.

func NewClient
func NewClient(address string, config *Config) (c *Client, err error)

NewClient connects to the specified address and returns the resulting Client.

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

Call an RPC method and return the result.

func (*Client) Close
func (c *Client) Close() error

Close the client connection, not further use of the Client is permitted after this method has been called.

func (*Client) Handle
func (c *Client) Handle(method string, handler NotificationHandler)

Handle the notification method, using the specificed handler. The handler will be passed the data parameter from the incoming notification.

func (*Client) Notify
func (c *Client) Notify(method string, params interface{}) error

Notify sends the RPC request and does not wait for a response.

type Config
type Config struct {
	// ReadTimeout is the time a call will wait for a response before failure.
	ReadTimeout time.Duration
	// ConnectTimeout is the time a re-/connection will be attempted before
	// failure. A value of zero attempts indefinitely.
	ConnectTimeout time.Duration
	// Reconnect determines whether the client will attempt to reconnect on
	// connection failure
	Reconnect bool
	// ConnectBackoffScale sets the scaling factor for back-off on failed
	// connection attempts
	ConnectBackoffScale int
}

Config represents the user-configurable parameters for the client

func NewConfig
func NewConfig() (c *Config)

NewConfig returns a config instance with default values.

type Error
type Error struct {
	Code    int         `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data,omitempty"`
}

Error response.

func (*Error) Error
func (e *Error) Error() string

Error satisfies the error interface.

type LevelledLogger
type LevelledLogger interface {
	// Debugf handles debug level messages
	Debugf(format string, args ...interface{})
	// Infof handles info level messages
	Infof(format string, args ...interface{})
	// Warnf handles warn level messages
	Warnf(format string, args ...interface{})
	// Errorf handles error level messages
	Errorf(format string, args ...interface{})
	// Fatalf handles fatal level messages, and must exit the application
	Fatalf(format string, args ...interface{})
	// Panicf handles debug level messages, and must panic the application
	Panicf(format string, args ...interface{})
}

LevelledLogger represents a minimal levelled logger

type NotificationHandler
type NotificationHandler func(method string, data interface{})

NotificationHandler is a callback handler for notifications.

Documentation

Overview

Package kodirpc is a JSON-RPC client for the TCP socket of the Kodi home theatre software.

Index

Examples

Constants

View Source
const (
	// DefaultReadTimeout is the default time a call will wait for a response.
	DefaultReadTimeout = 5 * time.Second
	// DefaultConnectTimeout is the default time re-/connection will be
	// attempted before failure.
	DefaultConnectTimeout = 5 * time.Minute
	// DefaultReconnect determines whether the client reconnects by default.
	DefaultReconnect = true
	// DefaultConnectBackoffScale is the default back-off scaling factor
	DefaultConnectBackoffScale = 2
)

Variables

This section is empty.

Functions

func SetLogger

func SetLogger(l LevelledLogger)

SetLogger enables logging for the library and wraps the supplied logger with a logPrefixer to denote locally generated logs

Types

type Client

type Client struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Client is a TCP JSON-RPC client for Kodi.

Example
package main

import (
	"github.com/pdf/kodirpc"
)

func main() {
	kodirpc.NewClient(`127.0.0.1:9090`, kodirpc.NewConfig())
}
Output:

Example (Config)
package main

import (
	"time"

	"github.com/pdf/kodirpc"
)

func main() {
	config := kodirpc.NewConfig()
	config.ReadTimeout = 2 * time.Second
	kodirpc.NewClient(`127.0.0.1:9090`, config)
}
Output:

Example (Logger)
package main

import (
	"os"

	"github.com/Sirupsen/logrus"
	"github.com/pdf/kodirpc"
)

func main() {
	// logrus is only used as an example, though loggers do need to satisfy the
	// LevelledLogger interface
	logger := &logrus.Logger{
		Out:       os.Stdout,
		Formatter: &logrus.TextFormatter{},
		Hooks:     make(logrus.LevelHooks),
		Level:     logrus.WarnLevel,
	}
	kodirpc.SetLogger(logger)

	kodirpc.NewClient(`127.0.0.1:9090`, kodirpc.NewConfig())
}
Output:

func NewClient

func NewClient(address string, config *Config) (c *Client, err error)

NewClient connects to the specified address and returns the resulting Client.

func (*Client) Call

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

Call an RPC method and return the result.

func (*Client) Close

func (c *Client) Close() error

Close the client connection, not further use of the Client is permitted after this method has been called.

func (*Client) Handle

func (c *Client) Handle(method string, handler NotificationHandler)

Handle the notification method, using the specificed handler. The handler will be passed the data parameter from the incoming notification.

func (*Client) Notify

func (c *Client) Notify(method string, params interface{}) error

Notify sends the RPC request and does not wait for a response.

type Config

type Config struct {
	// ReadTimeout is the time a call will wait for a response before failure.
	ReadTimeout time.Duration
	// ConnectTimeout is the time a re-/connection will be attempted before
	// failure. A value of zero attempts indefinitely.
	ConnectTimeout time.Duration
	// Reconnect determines whether the client will attempt to reconnect on
	// connection failure
	Reconnect bool
	// ConnectBackoffScale sets the scaling factor for back-off on failed
	// connection attempts
	ConnectBackoffScale int
}

Config represents the user-configurable parameters for the client

func NewConfig

func NewConfig() (c *Config)

NewConfig returns a config instance with default values.

type Error

type Error struct {
	Code    int         `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data,omitempty"`
}

Error response.

func (*Error) Error

func (e *Error) Error() string

Error satisfies the error interface.

type LevelledLogger

type LevelledLogger interface {
	// Debugf handles debug level messages
	Debugf(format string, args ...interface{})
	// Infof handles info level messages
	Infof(format string, args ...interface{})
	// Warnf handles warn level messages
	Warnf(format string, args ...interface{})
	// Errorf handles error level messages
	Errorf(format string, args ...interface{})
	// Fatalf handles fatal level messages, and must exit the application
	Fatalf(format string, args ...interface{})
	// Panicf handles debug level messages, and must panic the application
	Panicf(format string, args ...interface{})
}

LevelledLogger represents a minimal levelled logger

type NotificationHandler

type NotificationHandler func(method string, data interface{})

NotificationHandler is a callback handler for notifications.

Jump to

Keyboard shortcuts

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