mirror of
https://github.com/docker/docs.git
synced 2026-11-05 00:14:17 +03:00
This is a minimal effort to get things running. Only the `beam` package has changed - backend code is untouched. - The `beam` package is still around, but functions as a libswarm-specific wrapper around libchan messages. It'll either be renamed or moved to the top-level namespace. - We have our own Message type which serializes/deserializes to libchan messages, and our own versions of Sender and Receiver which speak libswarm. WrapSender and WrapReceiver promote libchan objects to their libswarm equivalents. - We also have our own wrappers around libchan utilities: RetPipe, Handler, Pipe, Copy. Signed-off-by: Aanand Prasad <aanand.prasad@gmail.com>
79 lines
1.3 KiB
Go
79 lines
1.3 KiB
Go
package beam
|
|
|
|
import (
|
|
"github.com/docker/libchan"
|
|
|
|
"fmt"
|
|
)
|
|
|
|
type Server struct {
|
|
handlers map[Verb]Sender
|
|
catchall Sender
|
|
}
|
|
|
|
func NewServer() *Server {
|
|
return &Server{
|
|
handlers: make(map[Verb]Sender),
|
|
}
|
|
}
|
|
|
|
func (s *Server) Catchall(h Sender) *Server {
|
|
s.catchall = h
|
|
return s
|
|
}
|
|
|
|
func (s *Server) OnVerb(v Verb, h Sender) *Server {
|
|
s.handlers[v] = h
|
|
return s
|
|
}
|
|
|
|
func (s *Server) OnSpawn(h Sender) *Server {
|
|
return s.OnVerb(Spawn, h)
|
|
}
|
|
|
|
func (s *Server) OnStart(h Sender) *Server {
|
|
return s.OnVerb(Start, h)
|
|
}
|
|
|
|
func (s *Server) OnStop(h Sender) *Server {
|
|
return s.OnVerb(Stop, h)
|
|
}
|
|
|
|
func (s *Server) OnAttach(h Sender) *Server {
|
|
return s.OnVerb(Attach, h)
|
|
}
|
|
|
|
func (s *Server) OnLog(h Sender) *Server {
|
|
return s.OnVerb(Log, h)
|
|
}
|
|
|
|
func (s *Server) OnError(h Sender) *Server {
|
|
return s.OnVerb(Error, h)
|
|
}
|
|
|
|
func (s *Server) OnLs(h Sender) *Server {
|
|
return s.OnVerb(Ls, h)
|
|
}
|
|
|
|
func (s *Server) OnGet(h Sender) *Server {
|
|
return s.OnVerb(Get, h)
|
|
}
|
|
|
|
func (s *Server) Send(msg *Message) (Receiver, error) {
|
|
if h, exists := s.handlers[msg.Verb]; exists {
|
|
return h.Send(msg)
|
|
}
|
|
if s.catchall != nil {
|
|
return s.catchall.Send(msg)
|
|
}
|
|
return NotImplemented.Send(msg)
|
|
}
|
|
|
|
func (s *Server) Close() error {
|
|
return fmt.Errorf("can't close")
|
|
}
|
|
|
|
func (s *Server) Unwrap() libchan.Sender {
|
|
return &senderUnwrapper{s}
|
|
}
|