11package keepalive
22
33import (
4- "fmt"
54 "github.com/cloudstruct/go-ouroboros-network/protocol"
6- "time"
75)
86
97const (
@@ -49,9 +47,8 @@ var StateMap = protocol.StateMap{
4947}
5048
5149type KeepAlive struct {
52- * protocol.Protocol
53- config * Config
54- timer * time.Timer
50+ Client * Client
51+ Server * Server
5552}
5653
5754type Config struct {
@@ -65,88 +62,10 @@ type KeepAliveFunc func(uint16) error
6562type KeepAliveResponseFunc func (uint16 ) error
6663type DoneFunc func () error
6764
68- func New (options protocol.ProtocolOptions , cfg * Config ) * KeepAlive {
65+ func New (protoOptions protocol.ProtocolOptions , cfg * Config ) * KeepAlive {
6966 k := & KeepAlive {
70- config : cfg ,
67+ Client : NewClient (protoOptions , cfg ),
68+ Server : NewServer (protoOptions , cfg ),
7169 }
72- protoConfig := protocol.ProtocolConfig {
73- Name : PROTOCOL_NAME ,
74- ProtocolId : PROTOCOL_ID ,
75- Muxer : options .Muxer ,
76- ErrorChan : options .ErrorChan ,
77- Mode : options .Mode ,
78- Role : options .Role ,
79- MessageHandlerFunc : k .messageHandler ,
80- MessageFromCborFunc : NewMsgFromCbor ,
81- StateMap : StateMap ,
82- InitialState : STATE_CLIENT ,
83- }
84- k .Protocol = protocol .New (protoConfig )
8570 return k
8671}
87-
88- func (k * KeepAlive ) Start () {
89- k .Protocol .Start ()
90- k .startTimer ()
91- }
92-
93- func (k * KeepAlive ) messageHandler (msg protocol.Message , isResponse bool ) error {
94- var err error
95- switch msg .Type () {
96- case MESSAGE_TYPE_KEEP_ALIVE :
97- err = k .handleKeepAlive (msg )
98- case MESSAGE_TYPE_KEEP_ALIVE_RESPONSE :
99- err = k .handleKeepAliveResponse (msg )
100- case MESSAGE_TYPE_DONE :
101- err = k .handleDone ()
102- default :
103- err = fmt .Errorf ("%s: received unexpected message type %d" , PROTOCOL_NAME , msg .Type ())
104- }
105- return err
106- }
107-
108- func (k * KeepAlive ) startTimer () {
109- k .timer = time .AfterFunc (KEEP_ALIVE_PERIOD * time .Second , func () {
110- if err := k .KeepAlive (0 ); err != nil {
111- k .SendError (err )
112- }
113- })
114- }
115-
116- func (k * KeepAlive ) KeepAlive (cookie uint16 ) error {
117- msg := NewMsgKeepAlive (cookie )
118- return k .SendMessage (msg )
119- }
120-
121- func (k * KeepAlive ) handleKeepAlive (msgGeneric protocol.Message ) error {
122- msg := msgGeneric .(* MsgKeepAlive )
123- if k .config != nil && k .config .KeepAliveFunc != nil {
124- // Call the user callback function
125- return k .config .KeepAliveFunc (msg .Cookie )
126- } else {
127- // Send the keep-alive response
128- resp := NewMsgKeepAliveResponse (msg .Cookie )
129- return k .SendMessage (resp )
130- }
131- }
132-
133- func (k * KeepAlive ) handleKeepAliveResponse (msgGeneric protocol.Message ) error {
134- msg := msgGeneric .(* MsgKeepAliveResponse )
135- // Start the timer again if we had one previously
136- if k .timer != nil {
137- defer k .startTimer ()
138- }
139- if k .config != nil && k .config .KeepAliveResponseFunc != nil {
140- // Call the user callback function
141- return k .config .KeepAliveResponseFunc (msg .Cookie )
142- }
143- return nil
144- }
145-
146- func (k * KeepAlive ) handleDone () error {
147- if k .config != nil && k .config .DoneFunc != nil {
148- // Call the user callback function
149- return k .config .DoneFunc ()
150- }
151- return nil
152- }
0 commit comments