tracker/server/server.go

52 lines
1.5 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 server implements an abstraction over servers meant to be run .
// alongside a tracker.
//
// Servers may be implementations of different transport protocols or have their
// own custom behavior.
package server
import (
"fmt"
2016-03-03 02:18:55 +01:00
"github.com/chihaya/chihaya"
2016-01-25 06:41:39 +01:00
"github.com/chihaya/chihaya/tracker"
)
var constructors = make(map[string]Constructor)
// Constructor is a function that creates a new Server.
2016-03-03 02:18:55 +01:00
type Constructor func(*chihaya.ServerConfig, *tracker.Tracker) (Server, error)
2016-01-25 06:41:39 +01:00
// Register makes a Constructor available by the provided name.
//
// If this function is called twice with the same name or if the Constructor is
// nil, it panics.
func Register(name string, con Constructor) {
if con == nil {
panic("server: could not register nil Constructor")
}
if _, dup := constructors[name]; dup {
panic("server: could not register duplicate Constructor: " + name)
}
constructors[name] = con
}
// New creates a Server specified by a configuration.
2016-03-03 02:18:55 +01:00
func New(cfg *chihaya.ServerConfig, tkr *tracker.Tracker) (Server, error) {
2016-01-25 06:41:39 +01:00
con, ok := constructors[cfg.Name]
if !ok {
return nil, fmt.Errorf("server: unknown Constructor %q (forgotten import?)", cfg.Name)
2016-01-25 06:41:39 +01:00
}
return con(cfg, tkr)
}
// Server represents one instance of a server accessing the tracker.
type Server interface {
Start()
Stop()
}