Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split WebSocket and HTTP server - Breaking Change #230

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ go 1.16
require (
github.com/Shopify/toxiproxy v2.1.4+incompatible
github.com/go-playground/locales v0.12.1 // indirect
github.com/go-playground/universal-translator v0.16.0 // indirect
github.com/gorilla/mux v1.7.3
github.com/go-playground/universal-translator v0.16.0
github.com/gorilla/websocket v1.4.1
github.com/kr/pretty v0.1.0 // indirect
github.com/leodido/go-urn v1.1.0 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ github.com/go-playground/locales v0.12.1 h1:2FITxuFt/xuCNP1Acdhv62OzaCiviiE4kotf
github.com/go-playground/locales v0.12.1/go.mod h1:IUMDtCfWo/w/mtMfIE/IG2K+Ey3ygWanZIBtBW0W2TM=
github.com/go-playground/universal-translator v0.16.0 h1:X++omBR/4cE2MNg91AoC3rmGrCjJ8eAeUP/K/EKx4DM=
github.com/go-playground/universal-translator v0.16.0/go.mod h1:1AnU7NaIRDWWzGEKwgtJRd2xk99HeFyHw3yid4rvQIY=
github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw=
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
Expand Down
5 changes: 3 additions & 2 deletions ocpp1.6/central_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ocpp16

import (
"fmt"
"net/http"
"reflect"

"github.com/lorenzodonini/ocpp-go/internal/callbackqueue"
Expand Down Expand Up @@ -403,11 +404,11 @@ func (cs *centralSystem) SendRequestAsync(clientId string, request ocpp.Request,
return cs.callbackQueue.TryQueue(clientId, send, callback)
}

func (cs *centralSystem) Start(listenPort int, listenPath string) {
func (cs *centralSystem) Start() http.HandlerFunc {
// Overriding some protocol-specific values in the lower layers globally
ocppj.FormationViolation = ocppj.FormatViolationV16
// Start server
cs.server.Start(listenPort, listenPath)
return cs.server.Start()
}

func (cs *centralSystem) sendResponse(chargePointId string, confirmation ocpp.Response, err error, requestId string) {
Expand Down
3 changes: 2 additions & 1 deletion ocpp1.6/v16.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package ocpp16
import (
"crypto/tls"
"net"
"net/http"

"github.com/lorenzodonini/ocpp-go/internal/callbackqueue"
"github.com/lorenzodonini/ocpp-go/ocpp"
Expand Down Expand Up @@ -251,7 +252,7 @@ type CentralSystem interface {
// The central system runs as a daemon and handles incoming charge point connections and messages.
//
// The function blocks forever, so it is suggested to wrap it in a goroutine, in case other functionality needs to be executed on the main program thread.
Start(listenPort int, listenPath string)
Start() http.HandlerFunc
// Errors returns a channel for error messages. If it doesn't exist it es created.
Errors() <-chan error
}
Expand Down
5 changes: 3 additions & 2 deletions ocpp2.0.1/csms.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ocpp2

import (
"fmt"
"net/http"
"reflect"

"github.com/lorenzodonini/ocpp-go/internal/callbackqueue"
Expand Down Expand Up @@ -809,11 +810,11 @@ func (cs *csms) SendRequestAsync(clientId string, request ocpp.Request, callback
return cs.callbackQueue.TryQueue(clientId, send, callback)
}

func (cs *csms) Start(listenPort int, listenPath string) {
func (cs *csms) Start() http.HandlerFunc {
// Overriding some protocol-specific values in the lower layers globally
ocppj.FormationViolation = ocppj.FormatViolationV2
// Start server
cs.server.Start(listenPort, listenPath)
return cs.server.Start()
}

func (cs *csms) sendResponse(chargingStationID string, response ocpp.Response, err error, requestId string) {
Expand Down
9 changes: 6 additions & 3 deletions ocpp2.0.1/v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package ocpp2
import (
"crypto/tls"
"net"
"net/http"

"github.com/lorenzodonini/ocpp-go/internal/callbackqueue"
"github.com/lorenzodonini/ocpp-go/ocpp"
Expand Down Expand Up @@ -34,8 +35,10 @@ type ChargingStationConnection interface {
TLSConnectionState() *tls.ConnectionState
}

type ChargingStationValidationHandler ws.CheckClientHandler
type ChargingStationConnectionHandler func(chargePoint ChargingStationConnection)
type (
ChargingStationValidationHandler ws.CheckClientHandler
ChargingStationConnectionHandler func(chargePoint ChargingStationConnection)
)

// -------------------- v2.0 Charging Station --------------------

Expand Down Expand Up @@ -381,7 +384,7 @@ type CSMS interface {
// The central system runs as a daemon and handles incoming charge point connections and messages.

// The function blocks forever, so it is suggested to wrap it in a goroutine, in case other functionality needs to be executed on the main program thread.
Start(listenPort int, listenPath string)
Start() http.HandlerFunc
// Errors returns a channel for error messages. If it doesn't exist it es created.
Errors() <-chan error
}
Expand Down
18 changes: 10 additions & 8 deletions ocppj/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ocppj

import (
"fmt"
"net/http"

"gopkg.in/go-playground/validator.v9"

Expand All @@ -25,11 +26,13 @@ type Server struct {
RequestState ServerState
}

type ClientHandler func(client ws.Channel)
type RequestHandler func(client ws.Channel, request ocpp.Request, requestId string, action string)
type ResponseHandler func(client ws.Channel, response ocpp.Response, requestId string)
type ErrorHandler func(client ws.Channel, err *ocpp.Error, details interface{})
type InvalidMessageHook func(client ws.Channel, err *ocpp.Error, rawJson string, parsedFields []interface{}) *ocpp.Error
type (
ClientHandler func(client ws.Channel)
RequestHandler func(client ws.Channel, request ocpp.Request, requestId string, action string)
ResponseHandler func(client ws.Channel, response ocpp.Response, requestId string)
ErrorHandler func(client ws.Channel, err *ocpp.Error, details interface{})
InvalidMessageHook func(client ws.Channel, err *ocpp.Error, rawJson string, parsedFields []interface{}) *ocpp.Error
)

// Creates a new Server endpoint.
// Requires a a websocket server. Optionally a structure for queueing/dispatching requests,
Expand Down Expand Up @@ -125,16 +128,15 @@ func (s *Server) SetDisconnectedClientHandler(handler ClientHandler) {
// Invoke this function in a separate goroutine, to perform other operations on the main thread.
//
// An error may be returned, if the websocket server couldn't be started.
func (s *Server) Start(listenPort int, listenPath string) {
func (s *Server) Start() http.HandlerFunc {
// Set internal message handler
s.server.SetCheckClientHandler(s.checkClientHandler)
s.server.SetNewClientHandler(s.onClientConnected)
s.server.SetDisconnectedClientHandler(s.onClientDisconnected)
s.server.SetMessageHandler(s.ocppMessageHandler)
s.dispatcher.Start()
// Serve & run
s.server.Start(listenPort, listenPath)
// TODO: return error?
return s.server.Start()
}

// Stops the server.
Expand Down
Loading
Loading