|
1 | 1 | // Package multiplex provides protocol multiplexing support for OCPP servers. |
2 | | -// It allows a single WebSocket server to handle both OCPP 1.6 and OCPP 2.0.1 clients |
| 2 | +// It allows a single WebSocket server to handle multiple OCPP protocol versions |
3 | 3 | // on the same port, using WebSocket subprotocol negotiation to route connections |
4 | 4 | // to the appropriate handler. |
5 | 5 | package multiplex |
6 | 6 |
|
7 | | -import ( |
8 | | - ocpp16 "github.com/lorenzodonini/ocpp-go/ocpp1.6" |
9 | | - ocpp2 "github.com/lorenzodonini/ocpp-go/ocpp2.0.1" |
10 | | - "github.com/lorenzodonini/ocpp-go/ws" |
11 | | -) |
12 | | - |
13 | | -// ProtocolVersion represents an OCPP protocol version. |
14 | | -type ProtocolVersion string |
15 | | - |
16 | | -const ( |
17 | | - // V16 represents OCPP 1.6 |
18 | | - V16 ProtocolVersion = "ocpp1.6" |
19 | | - // V201 represents OCPP 2.0.1 |
20 | | - V201 ProtocolVersion = "ocpp2.0.1" |
21 | | -) |
| 7 | +import "github.com/lorenzodonini/ocpp-go/ws" |
22 | 8 |
|
23 | 9 | // Subprotocol constants for WebSocket negotiation. |
24 | 10 | const ( |
@@ -55,47 +41,39 @@ const ( |
55 | 41 | // }) |
56 | 42 | type SubprotocolSelector func(clientID string, requestedSubprotocols []string) string |
57 | 43 |
|
58 | | -// MultiProtocolServer is a server that can handle both OCPP 1.6 and OCPP 2.0.1 clients |
59 | | -// on a single port. It uses WebSocket subprotocol negotiation to determine which |
60 | | -// protocol version each client uses. |
61 | | -type MultiProtocolServer interface { |
62 | | - // Start begins listening for connections on the specified port and path. |
63 | | - // The function blocks until Stop is called. |
64 | | - // |
65 | | - // Example: |
66 | | - // go server.Start(8080, "/ocpp/{id}") |
67 | | - Start(port int, listenPath string) |
68 | | - |
69 | | - // Stop gracefully shuts down the server. |
70 | | - Stop() |
71 | | - |
72 | | - // OCPP16Server returns the underlying OCPP 1.6 Central System. |
73 | | - // Use this to register handlers for OCPP 1.6 messages. |
74 | | - OCPP16Server() ocpp16.CentralSystem |
75 | | - |
76 | | - // OCPP201Server returns the underlying OCPP 2.0.1 CSMS. |
77 | | - // Use this to register handlers for OCPP 2.0.1 messages. |
78 | | - OCPP201Server() ocpp2.CSMS |
| 44 | +// Server provides a shared WebSocket server for multiple OCPP protocol versions. |
| 45 | +// Create OCPP servers (CentralSystem, CSMS) using the shared WebSocketServer(), |
| 46 | +// then call Start() on each one. The ws.Server.Start() method is idempotent, |
| 47 | +// so only the first call actually starts the server. |
| 48 | +// |
| 49 | +// Example usage: |
| 50 | +// |
| 51 | +// // Create multiplex server |
| 52 | +// mux := multiplex.NewServer() |
| 53 | +// |
| 54 | +// // Create only the OCPP servers you need |
| 55 | +// cs := ocpp16.NewCentralSystem(nil, mux.WebSocketServer()) |
| 56 | +// csms := ocpp2.NewCSMS(nil, mux.WebSocketServer()) |
| 57 | +// |
| 58 | +// // Register handlers |
| 59 | +// cs.SetCoreHandler(&myOCPP16Handler{}) |
| 60 | +// csms.SetProvisioningHandler(&myOCPP201Handler{}) |
| 61 | +// |
| 62 | +// // Start servers (ws.Server.Start is idempotent) |
| 63 | +// go cs.Start(8080, "/ocpp/{id}") |
| 64 | +// csms.Start(8080, "/ocpp/{id}") |
| 65 | +type Server interface { |
| 66 | + // WebSocketServer returns the underlying ws.Server. |
| 67 | + // Use this when creating OCPP servers to share the same WebSocket connection. |
| 68 | + WebSocketServer() ws.Server |
79 | 69 |
|
80 | 70 | // SetSubprotocolSelector sets a custom callback for choosing which subprotocol |
81 | | - // to use when a client requests multiple subprotocols. This allows the application |
82 | | - // to implement custom protocol selection logic (e.g., prefer OCPP 2.0.1 over 1.6). |
83 | | - // If not set, the default behavior is used (first mutually-supported protocol). |
| 71 | + // to use when a client requests multiple subprotocols. |
84 | 72 | SetSubprotocolSelector(selector SubprotocolSelector) |
85 | 73 |
|
86 | | - // SetNewClientHandler sets a callback for all new client connections, |
87 | | - // regardless of protocol version. The callback receives the WebSocket channel |
88 | | - // which can be used to determine the protocol via channel.Subprotocol(). |
89 | | - SetNewClientHandler(handler func(channel ws.Channel)) |
90 | | - |
91 | | - // SetDisconnectedClientHandler sets a callback for all client disconnections, |
92 | | - // regardless of protocol version. |
93 | | - SetDisconnectedClientHandler(handler func(channel ws.Channel)) |
94 | | - |
95 | 74 | // SetBasicAuthHandler enables HTTP Basic Authentication for all connections. |
96 | 75 | SetBasicAuthHandler(handler func(username, password string) bool) |
97 | 76 |
|
98 | 77 | // SetCheckClientHandler sets a validation handler for incoming connections. |
99 | | - // Return false to reject the connection. |
100 | 78 | SetCheckClientHandler(handler ws.CheckClientHandler) |
101 | 79 | } |
0 commit comments