Skip to content

Commit

Permalink
Doc and lint cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
dustin committed Feb 16, 2014
1 parent dfb9749 commit e001126
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 41 deletions.
21 changes: 13 additions & 8 deletions client.go
Expand Up @@ -5,19 +5,24 @@ import (
"time"
)

const RESPONSE_TIMEOUT = time.Second * 2

const RESPONSE_RANDOM_FACTOR = 1.5

const MAX_RETRANSMIT = 4
const (
// ResponseTimeout is the amount of time to wait for a
// response.
ResponseTimeout = time.Second * 2
// ResponseRandomFactor is a multiplier for response backoff.
ResponseRandomFactor = 1.5
// MaxRetransmit is the maximum number of times a message will
// be retransmitted.
MaxRetransmit = 4
)

// A CoAP client connection.
// Conn is a CoAP client connection.
type Conn struct {
conn *net.UDPConn
buf []byte
}

// Get a CoAP client.
// Dial connects a CoAP client.
func Dial(n, addr string) (*Conn, error) {
uaddr, err := net.ResolveUDPAddr(n, addr)
if err != nil {
Expand All @@ -32,7 +37,7 @@ func Dial(n, addr string) (*Conn, error) {
return &Conn{s, make([]byte, maxPktLen)}, nil
}

// Duration a message. Get a response if there is one.
// Send a message. Get a response if there is one.
func (c *Conn) Send(req Message) (*Message, error) {
err := Transmit(c.conn, nil, req)
if err == nil {
Expand Down
58 changes: 31 additions & 27 deletions message.go
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
)

// COAPType represents the message type.
type COAPType uint8

const (
Expand Down Expand Up @@ -37,6 +38,7 @@ func (t COAPType) String() string {
return typeNames[t]
}

// COAPCode is the type used for both request and response codes.
type COAPCode uint8

// Request Codes
Expand Down Expand Up @@ -114,10 +116,11 @@ func (c COAPCode) String() string {
return codeNames[c]
}

var InvalidTokenLen = errors.New("Invalid token length")
var OptionTooLong = errors.New("Option is too long")
var OptionGapTooLarge = errors.New("Option gap too large")
var ErrInvalidTokenLen = errors.New("invalid token length")
var ErrOptionTooLong = errors.New("option is too long")
var ErrOptionGapTooLarge = errors.New("option gap too large")

// OptionID identifies an option in a message.
type OptionID uint8

/*
Expand Down Expand Up @@ -162,6 +165,7 @@ const (
Size1 = OptionID(60)
)

// MediaType specifies the content type of a message.
type MediaType byte

const (
Expand Down Expand Up @@ -224,7 +228,7 @@ func (o option) toBytes() []byte {
case uint32:
v = i
default:
panic(fmt.Errorf("Invalid type for option %x: %T (%v)",
panic(fmt.Errorf("invalid type for option %x: %T (%v)",
o.ID, o.Value, o.Value))
}

Expand Down Expand Up @@ -258,7 +262,7 @@ func (o options) Minus(oid OptionID) options {
return rv
}

// A CoAP message.
// Message is a CoAP message.
type Message struct {
Type COAPType
Code COAPCode
Expand All @@ -269,12 +273,12 @@ type Message struct {
opts options
}

// Return True if this message is confirmable.
// IsConfirmable returns true if this message is confirmable.
func (m Message) IsConfirmable() bool {
return m.Type == Confirmable
}

// Get all the values for the given option.
// Options gets all the values for the given option.
func (m Message) Options(o OptionID) []interface{} {
var rv []interface{}

Expand All @@ -287,7 +291,7 @@ func (m Message) Options(o OptionID) []interface{} {
return rv
}

// Get the first value for the given option ID.
// Option gets the first value for the given option ID.
func (m Message) Option(o OptionID) interface{} {
for _, v := range m.opts {
if o == v.ID {
Expand All @@ -305,46 +309,46 @@ func (m Message) optionStrings(o OptionID) []string {
return rv
}

// Get the Path set on this message if any.
// Path gets the Path set on this message if any.
func (m Message) Path() []string {
return m.optionStrings(URIPath)
}

// Get a path as a / separated string.
// PathString gets a path as a / separated string.
func (m Message) PathString() string {
return strings.Join(m.Path(), "/")
}

// Set a path by a / separated string.
// SetPathString sets a path by a / separated string.
func (m *Message) SetPathString(s string) {
for s[0] == '/' {
s = s[1:]
}
m.SetPath(strings.Split(s, "/"))
}

// Update or add a LocationPath attribute on this message.
// SetPath updates or adds a LocationPath attribute on this message.
func (m *Message) SetPath(s []string) {
m.RemoveOption(URIPath)
for _, p := range s {
m.AddOption(URIPath, p)
}
}

// Remove all references to an option
func (m *Message) RemoveOption(opId OptionID) {
m.opts = m.opts.Minus(opId)
// RemoveOption removes all references to an option
func (m *Message) RemoveOption(opID OptionID) {
m.opts = m.opts.Minus(opID)
}

// Add an option.
func (m *Message) AddOption(opId OptionID, val interface{}) {
m.opts = append(m.opts, option{opId, val})
// AddOption adds an option.
func (m *Message) AddOption(opID OptionID, val interface{}) {
m.opts = append(m.opts, option{opID, val})
}

// Set an option, discarding any previous value
func (m *Message) SetOption(opId OptionID, val interface{}) {
m.RemoveOption(opId)
m.AddOption(opId, val)
// SetOption sets an option, discarding any previous value
func (m *Message) SetOption(opID OptionID, val interface{}) {
m.RemoveOption(opID)
m.AddOption(opID, val)
}

func (m *Message) encode() ([]byte, error) {
Expand Down Expand Up @@ -402,7 +406,7 @@ func (m *Message) encode() ([]byte, error) {
buf.Write([]byte{byte(int(o.ID)-prev)<<4 | byte(len(b))})
}
if int(o.ID)-prev > 15 {
return nil, OptionGapTooLarge
return nil, ErrOptionGapTooLarge
}

buf.Write(b)
Expand All @@ -420,17 +424,17 @@ func (m *Message) encode() ([]byte, error) {

func parseMessage(data []byte) (rv Message, err error) {
if len(data) < 6 {
return rv, errors.New("Short packet")
return rv, errors.New("short packet")
}

if data[0]>>6 != 1 {
return rv, errors.New("Invalid version")
return rv, errors.New("invalid version")
}

rv.Type = COAPType((data[0] >> 4) & 0x3)
tokenLen := int(data[0] & 0xf)
if tokenLen > 8 {
return rv, InvalidTokenLen
return rv, ErrInvalidTokenLen
}

rv.Code = COAPCode(data[1])
Expand All @@ -451,7 +455,7 @@ func parseMessage(data []byte) (rv Message, err error) {
b = b[1:]
}
if len(b) < l {
return rv, errors.New("Truncated")
return rv, errors.New("truncated")
}
var opval interface{} = b[:l]
switch oid {
Expand Down
2 changes: 1 addition & 1 deletion message_test.go
Expand Up @@ -50,7 +50,7 @@ func TestEncodeMessageLargeOptionGap(t *testing.T) {
req.AddOption(ProxyURI, "u")

_, err := req.encode()
if err != OptionGapTooLarge {
if err != ErrOptionGapTooLarge {
t.Fatalf("Expected 'option gap too large', got: %v", err)
}
}
Expand Down
10 changes: 5 additions & 5 deletions server.go
@@ -1,4 +1,4 @@
// CoAP Client and Server in Go
// Package coap provides a CoAP client and server.
package coap

import (
Expand All @@ -9,7 +9,7 @@ import (

const maxPktLen = 1500

// Handle CoAP messages.
// Handler is a type that handles CoAP messages.
type Handler interface {
// Handle the message and optionally return a response message.
ServeCOAP(l *net.UDPConn, a *net.UDPAddr, m *Message) *Message
Expand All @@ -21,7 +21,7 @@ func (f funcHandler) ServeCOAP(l *net.UDPConn, a *net.UDPAddr, m *Message) *Mess
return f(l, a, m)
}

// Build a handler from a function.
// FuncHandler builds a handler from a function.
func FuncHandler(f func(l *net.UDPConn, a *net.UDPAddr, m *Message) *Message) Handler {
return funcHandler(f)
}
Expand Down Expand Up @@ -59,7 +59,7 @@ func Transmit(l *net.UDPConn, a *net.UDPAddr, m Message) error {

// Receive a message.
func Receive(l *net.UDPConn, buf []byte) (Message, error) {
l.SetReadDeadline(time.Now().Add(RESPONSE_TIMEOUT))
l.SetReadDeadline(time.Now().Add(ResponseTimeout))

nr, _, err := l.ReadFromUDP(buf)
if err != nil {
Expand All @@ -68,7 +68,7 @@ func Receive(l *net.UDPConn, buf []byte) (Message, error) {
return parseMessage(buf[:nr])
}

// Bind to the given address and serve requests forever.
// ListenAndServe binds to the given address and serve requests forever.
func ListenAndServe(n, addr string, rh Handler) error {
uaddr, err := net.ResolveUDPAddr(n, addr)
if err != nil {
Expand Down

0 comments on commit e001126

Please sign in to comment.