client

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

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

Go to latest
Published: Jun 12, 2022 License: Apache-2.0 Imports: 15 Imported by: 0

README

go-span-client

This repository contains a simple client library for communicating with Span as an IoT device. If you want to access the Span API for managing Span resources, the library you want is the Go Span API Client.

This library uses the Pion DTLS library.

Godoc

Minimal example

package main

import (
    "log"
    "time"
    client "github.com/borud/go-span-client"
)

func main() {
    client, err := client.Connect(client.NewDefaultConfig())
    if err != nil {
        log.Fatal(err)
    }
    defer client.Close()
   
    n, err := client.Write([]byte("this is a test"), time.Second)
    if err != nil {
        log.Fatal(err)
    }
    log.Printf("wrote %d bytes", n)
   
    buffer := make([]byte, 1024)
   
    n, err = client.Read(buffer, time.Second*5)
    if err != nil {
        log.Fatal(err)
    }
    log.Printf("read %d bytes: [%s]", n, string(buffer))
}

Documentation

Overview

Package client implements a DTLS based client for Span

Sample usage:

package main

import (
    "log"
    "time"
    client "github.com/borud/go-span-client"
)

func main() {
    client, err := client.Connect(client.NewDefaultConfig())
    if err != nil {
        log.Fatal(err)
    }
    defer client.Close()

    n, err := client.Write([]byte("this is a test"), time.Second)
    if err != nil {
        log.Fatal(err)
    }
    log.Printf("wrote %d bytes", n)

    buffer := make([]byte, 1024)

    n, err = client.Read(buffer, time.Second*5)
    if err != nil {
        log.Fatal(err)
    }
    log.Printf("read %d bytes: [%s]", n, string(buffer))
}

/

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCannotResolveSpanAddress = errors.New("cannot resolve Span address")
	ErrCannotConnect            = errors.New("cannot connect to Span")
	ErrCannotReadKeyPair        = errors.New("cannot read keypair")
	ErrCannotLoadCertPool       = errors.New("cannot load certpool")
)

Errors for Client

Functions

func COAPConnect

func COAPConnect(config Config) (*client.ClientConn, error)

COAPConnect creates a CoAP connection.

Types

type Client

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

Client is a Span client that connects as a device.

func Connect

func Connect(config Config) (*Client, error)

Connect client to Span using DTLS

func (*Client) Close

func (c *Client) Close() error

Close the client connection.

func (*Client) Read

func (c *Client) Read(buffer []byte, deadline ...time.Duration) (int, error)

Read data with optional deadline.

func (*Client) Write

func (c *Client) Write(data []byte, deadline ...time.Duration) (int, error)

Write data with optional deadline.

type Config

type Config struct {
	DTLSConfig   *dtls.Config
	SpanUDPAddr  string
	SpanCOAPAddr string
}

Config for Client.

func NewConfig

func NewConfig(certBytes []byte, keyBytes []byte) (Config, error)

NewConfig creates a new configuration from certBytes and keyBytes. Note that both certBytes and keyBytes are expected to be PEM-encoded.

func NewDefaultConfig

func NewDefaultConfig() Config

NewDefaultConfig creates a new default configuration. This requires you to have a .devcli/certs directory under your home directory where the cert.crt and key.pem are stored. This function is a bit ugly since it terminates if anything goes wrong, so it is only useful in clients where this behavior is acceptable. If you want to be able to do proper error handling please see the NewConfig function.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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