Skip to content

Commit 7a99505

Browse files
committed
Refactor websocket client and server struct names
Signed-off-by: Lorenzo <[email protected]>
1 parent c87b412 commit 7a99505

14 files changed

+302
-544
lines changed

ocpp1.6/v16.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ type ChargePoint interface {
158158
// })
159159
//
160160
// For more advanced options, or if a customer networking/occpj layer is required,
161-
// please refer to ocppj.Client and ws.WsClient.
162-
func NewChargePoint(id string, endpoint *ocppj.Client, client ws.WsClient) ChargePoint {
161+
// please refer to ocppj.Client and ws.Client.
162+
func NewChargePoint(id string, endpoint *ocppj.Client, client ws.Client) ChargePoint {
163163
if client == nil {
164164
client = ws.NewClient()
165165
}
@@ -339,7 +339,7 @@ type CentralSystem interface {
339339
// If you need a TLS server, you may use the following:
340340
//
341341
// cs := NewServer(nil, ws.NewTLSServer("certificatePath", "privateKeyPath"))
342-
func NewCentralSystem(endpoint *ocppj.Server, server ws.WsServer) CentralSystem {
342+
func NewCentralSystem(endpoint *ocppj.Server, server ws.Server) CentralSystem {
343343
if server == nil {
344344
server = ws.NewServer()
345345
}

ocpp1.6_test/ocpp16_test.go

+7-39
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"crypto/tls"
55
"fmt"
66
"net"
7-
"net/http"
87
"reflect"
98
"testing"
109
"time"
@@ -47,14 +46,18 @@ func (websocket MockWebSocket) TLSConnectionState() *tls.ConnectionState {
4746
return nil
4847
}
4948

49+
func (websocket MockWebSocket) IsConnected() bool {
50+
return true
51+
}
52+
5053
func NewMockWebSocket(id string) MockWebSocket {
5154
return MockWebSocket{id: id}
5255
}
5356

5457
// ---------------------- MOCK WEBSOCKET SERVER ----------------------
5558
type MockWebsocketServer struct {
5659
mock.Mock
57-
ws.WsServer
60+
ws.Server
5861
MessageHandler func(ws ws.Channel, data []byte) error
5962
NewClientHandler func(ws ws.Channel)
6063
CheckClientHandler ws.CheckClientHandler
@@ -93,14 +96,14 @@ func (websocketServer *MockWebsocketServer) NewClient(websocketId string, client
9396
websocketServer.MethodCalled("NewClient", websocketId, client)
9497
}
9598

96-
func (websocketServer *MockWebsocketServer) SetCheckClientHandler(handler func(id string, r *http.Request) bool) {
99+
func (websocketServer *MockWebsocketServer) SetCheckClientHandler(handler ws.CheckClientHandler) {
97100
websocketServer.CheckClientHandler = handler
98101
}
99102

100103
// ---------------------- MOCK WEBSOCKET CLIENT ----------------------
101104
type MockWebsocketClient struct {
102105
mock.Mock
103-
ws.WsClient
106+
ws.Client
104107
MessageHandler func(data []byte) error
105108
ReconnectedHandler func()
106109
DisconnectedHandler func(err error)
@@ -442,41 +445,6 @@ func (smartChargingListener *MockChargePointSmartChargingListener) OnGetComposit
442445
}
443446

444447
// ---------------------- COMMON UTILITY METHODS ----------------------
445-
func NewWebsocketServer(t *testing.T, onMessage func(data []byte) ([]byte, error)) *ws.Server {
446-
wsServer := ws.Server{}
447-
wsServer.SetMessageHandler(func(ws ws.Channel, data []byte) error {
448-
assert.NotNil(t, ws)
449-
assert.NotNil(t, data)
450-
if onMessage != nil {
451-
response, err := onMessage(data)
452-
assert.Nil(t, err)
453-
if response != nil {
454-
err = wsServer.Write(ws.ID(), data)
455-
assert.Nil(t, err)
456-
}
457-
}
458-
return nil
459-
})
460-
return &wsServer
461-
}
462-
463-
func NewWebsocketClient(t *testing.T, onMessage func(data []byte) ([]byte, error)) *ws.Client {
464-
wsClient := ws.Client{}
465-
wsClient.SetMessageHandler(func(data []byte) error {
466-
assert.NotNil(t, data)
467-
if onMessage != nil {
468-
response, err := onMessage(data)
469-
assert.Nil(t, err)
470-
if response != nil {
471-
err = wsClient.Write(data)
472-
assert.Nil(t, err)
473-
}
474-
}
475-
return nil
476-
})
477-
return &wsClient
478-
}
479-
480448
type expectedCentralSystemOptions struct {
481449
clientId string
482450
rawWrittenMessage []byte

ocpp2.0.1/v2.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ type ChargingStation interface {
207207
// })
208208
//
209209
// For more advanced options, or if a custom networking/occpj layer is required,
210-
// please refer to ocppj.Client and ws.WsClient.
211-
func NewChargingStation(id string, endpoint *ocppj.Client, client ws.WsClient) ChargingStation {
210+
// please refer to ocppj.Client and ws.Client.
211+
func NewChargingStation(id string, endpoint *ocppj.Client, client ws.Client) ChargingStation {
212212
if client == nil {
213213
client = ws.NewClient()
214214
}
@@ -415,7 +415,7 @@ type CSMS interface {
415415
// If you need a TLS server, you may use the following:
416416
//
417417
// csms := NewCSMS(nil, ws.NewTLSServer("certificatePath", "privateKeyPath"))
418-
func NewCSMS(endpoint *ocppj.Server, server ws.WsServer) CSMS {
418+
func NewCSMS(endpoint *ocppj.Server, server ws.Server) CSMS {
419419
if server == nil {
420420
server = ws.NewServer()
421421
}

ocpp2.0.1_test/ocpp2_test.go

+7-40
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"crypto/tls"
55
"fmt"
66
"net"
7-
"net/http"
87
"reflect"
98
"testing"
109
"time"
@@ -59,6 +58,10 @@ func (websocket MockWebSocket) TLSConnectionState() *tls.ConnectionState {
5958
return nil
6059
}
6160

61+
func (websocket MockWebSocket) IsConnected() bool {
62+
return true
63+
}
64+
6265
func NewMockWebSocket(id string) MockWebSocket {
6366
return MockWebSocket{id: id}
6467
}
@@ -67,7 +70,7 @@ func NewMockWebSocket(id string) MockWebSocket {
6770

6871
type MockWebsocketServer struct {
6972
mock.Mock
70-
ws.WsServer
73+
ws.Server
7174
MessageHandler func(ws ws.Channel, data []byte) error
7275
NewClientHandler func(ws ws.Channel)
7376
CheckClientHandler ws.CheckClientHandler
@@ -106,15 +109,15 @@ func (websocketServer *MockWebsocketServer) NewClient(websocketId string, client
106109
websocketServer.MethodCalled("NewClient", websocketId, client)
107110
}
108111

109-
func (websocketServer *MockWebsocketServer) SetCheckClientHandler(handler func(id string, r *http.Request) bool) {
112+
func (websocketServer *MockWebsocketServer) SetCheckClientHandler(handler ws.CheckClientHandler) {
110113
websocketServer.CheckClientHandler = handler
111114
}
112115

113116
// ---------------------- MOCK WEBSOCKET CLIENT ----------------------
114117

115118
type MockWebsocketClient struct {
116119
mock.Mock
117-
ws.WsClient
120+
ws.Client
118121
MessageHandler func(data []byte) error
119122
ReconnectedHandler func()
120123
DisconnectedHandler func(err error)
@@ -812,42 +815,6 @@ func (handler *MockCSMSTransactionsHandler) OnTransactionEvent(chargingStationID
812815
}
813816

814817
// ---------------------- COMMON UTILITY METHODS ----------------------
815-
816-
func NewWebsocketServer(t *testing.T, onMessage func(data []byte) ([]byte, error)) *ws.Server {
817-
wsServer := ws.Server{}
818-
wsServer.SetMessageHandler(func(ws ws.Channel, data []byte) error {
819-
assert.NotNil(t, ws)
820-
assert.NotNil(t, data)
821-
if onMessage != nil {
822-
response, err := onMessage(data)
823-
assert.Nil(t, err)
824-
if response != nil {
825-
err = wsServer.Write(ws.ID(), data)
826-
assert.Nil(t, err)
827-
}
828-
}
829-
return nil
830-
})
831-
return &wsServer
832-
}
833-
834-
func NewWebsocketClient(t *testing.T, onMessage func(data []byte) ([]byte, error)) *ws.Client {
835-
wsClient := ws.Client{}
836-
wsClient.SetMessageHandler(func(data []byte) error {
837-
assert.NotNil(t, data)
838-
if onMessage != nil {
839-
response, err := onMessage(data)
840-
assert.Nil(t, err)
841-
if response != nil {
842-
err = wsClient.Write(data)
843-
assert.Nil(t, err)
844-
}
845-
}
846-
return nil
847-
})
848-
return &wsClient
849-
}
850-
851818
type expectedCSMSOptions struct {
852819
clientId string
853820
rawWrittenMessage []byte

ocppj/central_system_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ func (suite *OcppJTestSuite) TestCentralSystemNewClientHandler() {
400400
suite.mockServer.NewClientHandler(channel)
401401
ok := <-connectedC
402402
assert.True(t, ok)
403-
// Client state was created
403+
// client state was created
404404
_, ok = suite.serverRequestMap.Get(mockClientID)
405405
assert.True(t, ok)
406406
}

ocppj/client.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
// During message exchange, the two roles may be reversed (depending on the message direction), but a client struct remains associated to a charge point/charging station.
1414
type Client struct {
1515
Endpoint
16-
client ws.WsClient
16+
client ws.Client
1717
Id string
1818
requestHandler func(request ocpp.Request, requestId string, action string)
1919
responseHandler func(response ocpp.Response, requestId string)
@@ -35,7 +35,7 @@ type Client struct {
3535
//
3636
// The wsClient parameter cannot be nil. Refer to the ws package for information on how to create and
3737
// customize a websocket client.
38-
func NewClient(id string, wsClient ws.WsClient, dispatcher ClientDispatcher, stateHandler ClientState, profiles ...*ocpp.Profile) *Client {
38+
func NewClient(id string, wsClient ws.Client, dispatcher ClientDispatcher, stateHandler ClientState, profiles ...*ocpp.Profile) *Client {
3939
endpoint := Endpoint{}
4040
if wsClient == nil {
4141
panic("wsClient parameter cannot be nil")

ocppj/dispatcher.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ type ClientDispatcher interface {
5454
// Sets the network client, so the dispatcher may send requests using the networking layer directly.
5555
//
5656
// This needs to be set before calling the Start method. If not, sending requests will fail.
57-
SetNetworkClient(client ws.WsClient)
57+
SetNetworkClient(client ws.Client)
5858
// Sets the state manager for pending requests in the dispatcher.
5959
//
6060
// The state should only be accessed by the dispatcher while running.
@@ -90,7 +90,7 @@ type DefaultClientDispatcher struct {
9090
requestChannel chan bool
9191
readyForDispatch chan bool
9292
pendingRequestState ClientState
93-
network ws.WsClient
93+
network ws.Client
9494
mutex sync.RWMutex
9595
onRequestCancel func(requestID string, request ocpp.Request, err *ocpp.Error)
9696
timer *time.Timer
@@ -149,7 +149,7 @@ func (d *DefaultClientDispatcher) Stop() {
149149
// TODO: clear pending requests?
150150
}
151151

152-
func (d *DefaultClientDispatcher) SetNetworkClient(client ws.WsClient) {
152+
func (d *DefaultClientDispatcher) SetNetworkClient(client ws.Client) {
153153
d.network = client
154154
}
155155

@@ -335,7 +335,7 @@ type ServerDispatcher interface {
335335
// Sets the network server, so the dispatcher may send requests using the networking layer directly.
336336
//
337337
// This needs to be set before calling the Start method. If not, sending requests will fail.
338-
SetNetworkServer(server ws.WsServer)
338+
SetNetworkServer(server ws.Server)
339339
// Sets the state manager for pending requests in the dispatcher.
340340
//
341341
// The state should only be accessed by the dispatcher while running.
@@ -371,7 +371,7 @@ type DefaultServerDispatcher struct {
371371
running bool
372372
stoppedC chan struct{}
373373
onRequestCancel CanceledRequestHandler
374-
network ws.WsServer
374+
network ws.Server
375375
mutex sync.RWMutex
376376
}
377377

@@ -442,7 +442,7 @@ func (d *DefaultServerDispatcher) DeleteClient(clientID string) {
442442
}
443443
}
444444

445-
func (d *DefaultServerDispatcher) SetNetworkServer(server ws.WsServer) {
445+
func (d *DefaultServerDispatcher) SetNetworkServer(server ws.Server) {
446446
d.network = server
447447
}
448448

@@ -491,7 +491,7 @@ func (d *DefaultServerDispatcher) messagePump() {
491491
for {
492492
select {
493493
case <-d.stoppedC:
494-
// Server was stopped
494+
// server was stopped
495495
d.queueMap.Init()
496496
log.Info("stopped processing requests")
497497
return
@@ -547,7 +547,7 @@ func (d *DefaultServerDispatcher) messagePump() {
547547
clientCtx.cancel()
548548
clientContextMap[clientID] = clientTimeoutContext{}
549549
}
550-
// Client can now transmit again
550+
// client can now transmit again
551551
clientQueue, ok = d.queueMap.Get(clientID)
552552
if ok {
553553
// Ready to transmit
@@ -620,7 +620,7 @@ func (d *DefaultServerDispatcher) waitForTimeout(clientID string, clientCtx clie
620620
log.Debugf("timeout canceled for %s", clientID)
621621
}
622622
case <-d.stoppedC:
623-
// Server was stopped, every pending timeout gets canceled
623+
// server was stopped, every pending timeout gets canceled
624624
}
625625
}
626626

0 commit comments

Comments
 (0)