1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-21 17:46:58 -05:00

commander interface

This commit is contained in:
Darien Raymond 2018-01-24 15:05:46 +01:00
parent 9cd0e90e99
commit 8923159aee
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
2 changed files with 73 additions and 0 deletions

63
commander.go Normal file
View File

@ -0,0 +1,63 @@
package core
import (
"sync"
"google.golang.org/grpc"
)
// ServiceRegistryCallback is a callback function for registering services.
type ServiceRegistryCallback func(s *grpc.Server)
// Commander is a feature that accepts commands from external source.
type Commander interface {
Feature
// RegisterService registers a service into this Commander.
RegisterService(ServiceRegistryCallback)
}
type syncCommander struct {
sync.RWMutex
Commander
}
func (c *syncCommander) RegisterService(callback ServiceRegistryCallback) {
c.RLock()
defer c.RUnlock()
if c.Commander == nil {
return
}
c.Commander.RegisterService(callback)
}
func (c *syncCommander) Start() error {
c.RLock()
defer c.RUnlock()
if c.Commander == nil {
return nil
}
return c.Commander.Start()
}
func (c *syncCommander) Close() {
c.RLock()
defer c.RUnlock()
if c.Commander == nil {
return
}
c.Commander.Close()
}
func (c *syncCommander) Set(commander Commander) {
c.Lock()
defer c.Unlock()
c.Commander = commander
}

View File

@ -28,6 +28,7 @@ type Instance struct {
ihm syncInboundHandlerManager
ohm syncOutboundHandlerManager
clock syncClock
cmd syncCommander
features []Feature
id uuid.UUID
@ -130,6 +131,10 @@ func (s *Instance) RegisterFeature(feature interface{}, instance Feature) error
s.ihm.Set(instance.(InboundHandlerManager))
case OutboundHandlerManager, *OutboundHandlerManager:
s.ohm.Set(instance.(OutboundHandlerManager))
case Clock, *Clock:
s.clock.Set(instance.(Clock))
case Commander, *Commander:
s.cmd.Set(instance.(Commander))
}
s.features = append(s.features, instance)
return nil
@ -169,3 +174,8 @@ func (s *Instance) OutboundHandlerManager() OutboundHandlerManager {
func (s *Instance) Clock() Clock {
return &(s.clock)
}
// Commander returns the Commander used by this Instance. The returned Commander is always functional.
func (s *Instance) Commander() Commander {
return &(s.cmd)
}