tracker/pkg/event/event.go

71 lines
1.7 KiB
Go
Raw Normal View History

2016-01-25 06:41:39 +01:00
// Copyright 2016 The Chihaya Authors. All rights reserved.
// Use of this source code is governed by the BSD 2-Clause license,
// which can be found in the LICENSE file.
// Package event implements type-level constraints for dealing with the events
// communicated via BitTorrent announce.
package event
import (
"errors"
"strings"
)
// ErrUnknownEvent is returned when New fails to return an event.
var ErrUnknownEvent = errors.New("unknown event")
// Event represents an event done by a BitTorrent client.
type Event uint8
2016-01-25 06:41:39 +01:00
const (
// None is the event when a BitTorrent client announces due to time lapsed
// since the previous announce.
None Event = iota
2016-01-25 06:41:39 +01:00
// Started is the event sent by a BitTorrent client when it joins a swarm.
Started
// Stopped is the event sent by a BitTorrent client when it leaves a swarm.
Stopped
// Completed is the event sent by a BitTorrent client when it finishes
// downloading all of the required chunks.
Completed
)
var (
eventToString = make(map[Event]string)
stringToEvent = make(map[string]Event)
2016-01-25 06:41:39 +01:00
)
func init() {
eventToString[None] = "none"
eventToString[Started] = "started"
eventToString[Stopped] = "stopped"
eventToString[Completed] = "completed"
2016-02-17 23:39:21 +01:00
stringToEvent[""] = None
2016-02-15 20:25:08 +01:00
for k, v := range eventToString {
stringToEvent[v] = k
}
2016-01-25 06:41:39 +01:00
}
// New returns the proper Event given a string.
func New(eventStr string) (Event, error) {
2016-01-25 06:41:39 +01:00
if e, ok := stringToEvent[strings.ToLower(eventStr)]; ok {
return e, nil
}
return None, ErrUnknownEvent
}
// String implements Stringer for an event.
func (e Event) String() string {
2016-01-25 06:41:39 +01:00
if name, ok := eventToString[e]; ok {
return name
}
panic("event: event has no associated name")
}