2017-02-26 23:27:38 +00:00
|
|
|
package genericlistener
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"net"
|
|
|
|
)
|
|
|
|
|
|
|
|
//ListenerRegistrationStatus - post registration status
|
|
|
|
type ListenerRegistrationStatus int
|
|
|
|
|
|
|
|
const (
|
|
|
|
listenerAdded ListenerRegistrationStatus = iota
|
|
|
|
listenerExists
|
|
|
|
listenerFault
|
|
|
|
)
|
|
|
|
|
|
|
|
//ListenerRegistration -- A connection registration structure used to bring up a connection
|
|
|
|
//connection table will then handle additing and sdtarting up the various readers
|
|
|
|
//else error.
|
|
|
|
type ListenerRegistration struct {
|
|
|
|
// The websocket connection.
|
|
|
|
listener *net.Listener
|
|
|
|
|
|
|
|
// The listener port
|
|
|
|
port int
|
|
|
|
|
|
|
|
// The status
|
|
|
|
status ListenerRegistrationStatus
|
|
|
|
|
|
|
|
// The error
|
|
|
|
err error
|
|
|
|
|
|
|
|
// communications channel between go routines
|
|
|
|
commCh chan *ListenerRegistration
|
|
|
|
}
|
|
|
|
|
|
|
|
//NewListenerRegistration -- Constructor
|
|
|
|
func NewListenerRegistration(port int) (p *ListenerRegistration) {
|
|
|
|
p = new(ListenerRegistration)
|
|
|
|
p.port = port
|
|
|
|
p.commCh = make(chan *ListenerRegistration)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//GenericListeners -
|
|
|
|
type GenericListeners struct {
|
2017-03-03 00:47:59 +00:00
|
|
|
listeners map[*net.Listener]int
|
|
|
|
ctx context.Context
|
|
|
|
connnectionTable *Table
|
|
|
|
connectionTracking *Tracking
|
|
|
|
secretKey string
|
|
|
|
certbundle tls.Certificate
|
|
|
|
register chan *ListenerRegistration
|
|
|
|
genericListeners *GenericListeners
|
2017-03-11 05:36:42 +00:00
|
|
|
wssHostName string
|
2017-03-11 20:28:49 +00:00
|
|
|
adminHostName string
|
|
|
|
cancelCheck int
|
2017-03-19 14:56:33 +00:00
|
|
|
lbDefaultMethod string
|
2017-03-20 00:04:47 +00:00
|
|
|
serverStatus *Status
|
2017-02-26 23:27:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//NewGenerListeners --
|
2017-03-20 00:04:47 +00:00
|
|
|
func NewGenerListeners(ctx context.Context, secretKey string, certbundle tls.Certificate, serverStatus *Status) (p *GenericListeners) {
|
2017-02-26 23:27:38 +00:00
|
|
|
p = new(GenericListeners)
|
|
|
|
p.listeners = make(map[*net.Listener]int)
|
|
|
|
p.ctx = ctx
|
2017-03-20 00:04:47 +00:00
|
|
|
p.connnectionTable = serverStatus.ConnectionTable
|
|
|
|
p.connectionTracking = serverStatus.ConnectionTracking
|
2017-02-26 23:27:38 +00:00
|
|
|
p.secretKey = secretKey
|
|
|
|
p.certbundle = certbundle
|
|
|
|
p.register = make(chan *ListenerRegistration)
|
2017-03-20 00:04:47 +00:00
|
|
|
p.wssHostName = serverStatus.WssDomain
|
|
|
|
p.adminHostName = serverStatus.AdminDomain
|
|
|
|
p.cancelCheck = serverStatus.DeadTime.cancelcheck
|
|
|
|
p.lbDefaultMethod = serverStatus.LoadbalanceDefaultMethod
|
|
|
|
p.serverStatus = serverStatus
|
2017-02-26 23:27:38 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//Run -- Execute
|
|
|
|
// - execute the GenericLister
|
|
|
|
// - pass initial port, we'll announce that
|
|
|
|
func (gl *GenericListeners) Run(ctx context.Context, initialPort int) {
|
|
|
|
loginfo.Println("ConnectionTable starting")
|
|
|
|
|
|
|
|
config := &tls.Config{Certificates: []tls.Certificate{gl.certbundle}}
|
|
|
|
|
|
|
|
ctx = context.WithValue(ctx, ctxSecretKey, gl.secretKey)
|
2017-03-03 00:47:59 +00:00
|
|
|
|
|
|
|
loginfo.Println(gl.connectionTracking)
|
|
|
|
|
|
|
|
ctx = context.WithValue(ctx, ctxConnectionTrack, gl.connectionTracking)
|
2017-02-26 23:27:38 +00:00
|
|
|
ctx = context.WithValue(ctx, ctxConfig, config)
|
|
|
|
ctx = context.WithValue(ctx, ctxListenerRegistration, gl.register)
|
2017-03-11 05:36:42 +00:00
|
|
|
ctx = context.WithValue(ctx, ctxWssHostName, gl.wssHostName)
|
2017-03-11 20:28:49 +00:00
|
|
|
ctx = context.WithValue(ctx, ctxAdminHostName, gl.adminHostName)
|
|
|
|
ctx = context.WithValue(ctx, ctxCancelCheck, gl.cancelCheck)
|
2017-03-19 14:56:33 +00:00
|
|
|
ctx = context.WithValue(ctx, ctxLoadbalanceDefaultMethod, gl.lbDefaultMethod)
|
2017-03-20 00:04:47 +00:00
|
|
|
ctx = context.WithValue(ctx, ctxServerStatus, gl.serverStatus)
|
2017-02-26 23:27:38 +00:00
|
|
|
|
|
|
|
go func(ctx context.Context) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
loginfo.Println("Cancel signal hit")
|
|
|
|
return
|
|
|
|
|
|
|
|
case registration := <-gl.register:
|
|
|
|
loginfo.Println("register fired", registration.port)
|
|
|
|
|
|
|
|
// check to see if port is already running
|
|
|
|
for listener := range gl.listeners {
|
|
|
|
if gl.listeners[listener] == registration.port {
|
2017-03-02 03:02:20 +00:00
|
|
|
loginfo.Println("listener already running", registration.port)
|
2017-02-26 23:27:38 +00:00
|
|
|
registration.status = listenerExists
|
|
|
|
registration.commCh <- registration
|
|
|
|
}
|
|
|
|
}
|
|
|
|
loginfo.Println("listener starting up ", registration.port)
|
2017-03-03 00:47:59 +00:00
|
|
|
loginfo.Println(ctx.Value(ctxConnectionTrack).(*Tracking))
|
2017-02-26 23:27:38 +00:00
|
|
|
go GenericListenAndServe(ctx, registration)
|
|
|
|
|
|
|
|
status := <-registration.commCh
|
|
|
|
if status.status == listenerAdded {
|
|
|
|
gl.listeners[status.listener] = status.port
|
|
|
|
} else if status.status == listenerFault {
|
|
|
|
loginfo.Println("Unable to create a new listerer", registration.port)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}(ctx)
|
|
|
|
|
|
|
|
newListener := NewListenerRegistration(initialPort)
|
|
|
|
gl.register <- newListener
|
|
|
|
}
|