|
| 1 | +package keepalive |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/cloudstruct/go-ouroboros-network/muxer" |
| 6 | + "github.com/cloudstruct/go-ouroboros-network/protocol" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +const ( |
| 11 | + PROTOCOL_NAME = "keep-alive" |
| 12 | + PROTOCOL_ID uint16 = 8 |
| 13 | + |
| 14 | + // Time between keep-alive probes, in seconds |
| 15 | + KEEP_ALIVE_PERIOD = 60 |
| 16 | +) |
| 17 | + |
| 18 | +var ( |
| 19 | + STATE_CLIENT = protocol.NewState(1, "Client") |
| 20 | + STATE_SERVER = protocol.NewState(2, "Server") |
| 21 | + STATE_DONE = protocol.NewState(3, "Done") |
| 22 | +) |
| 23 | + |
| 24 | +type KeepAlive struct { |
| 25 | + proto *protocol.Protocol |
| 26 | + callbackConfig *KeepAliveCallbackConfig |
| 27 | + timer *time.Timer |
| 28 | +} |
| 29 | + |
| 30 | +type KeepAliveCallbackConfig struct { |
| 31 | + KeepAliveFunc KeepAliveFunc |
| 32 | + KeepAliveResponseFunc KeepAliveResponseFunc |
| 33 | + DoneFunc DoneFunc |
| 34 | +} |
| 35 | + |
| 36 | +// Callback function types |
| 37 | +type KeepAliveFunc func(uint16) error |
| 38 | +type KeepAliveResponseFunc func(uint16) error |
| 39 | +type DoneFunc func() error |
| 40 | + |
| 41 | +func New(m *muxer.Muxer, errorChan chan error, callbackConfig *KeepAliveCallbackConfig) *KeepAlive { |
| 42 | + k := &KeepAlive{ |
| 43 | + callbackConfig: callbackConfig, |
| 44 | + } |
| 45 | + k.proto = protocol.New(PROTOCOL_NAME, PROTOCOL_ID, m, errorChan, k.messageHandler, NewMsgFromCbor) |
| 46 | + // Set initial state |
| 47 | + k.proto.SetState(STATE_CLIENT) |
| 48 | + return k |
| 49 | +} |
| 50 | + |
| 51 | +func (k *KeepAlive) messageHandler(msg protocol.Message) error { |
| 52 | + var err error |
| 53 | + switch msg.Type() { |
| 54 | + case MESSAGE_TYPE_KEEP_ALIVE: |
| 55 | + err = k.handleKeepAlive(msg) |
| 56 | + case MESSAGE_TYPE_KEEP_ALIVE_RESPONSE: |
| 57 | + err = k.handleKeepAliveResponse(msg) |
| 58 | + case MESSAGE_TYPE_DONE: |
| 59 | + err = k.handleDone() |
| 60 | + default: |
| 61 | + err = fmt.Errorf("%s: received unexpected message type %d", PROTOCOL_NAME, msg.Type()) |
| 62 | + } |
| 63 | + return err |
| 64 | +} |
| 65 | + |
| 66 | +func (k *KeepAlive) Start() { |
| 67 | + k.timer = time.AfterFunc(KEEP_ALIVE_PERIOD*time.Second, func() { |
| 68 | + if err := k.KeepAlive(0); err != nil { |
| 69 | + k.proto.SendError(err) |
| 70 | + } |
| 71 | + }) |
| 72 | +} |
| 73 | + |
| 74 | +func (k *KeepAlive) Stop() { |
| 75 | + if k.timer != nil { |
| 76 | + k.timer.Stop() |
| 77 | + } |
| 78 | + // Remove timer, since we check for its presence elsewhere |
| 79 | + k.timer = nil |
| 80 | +} |
| 81 | + |
| 82 | +func (k *KeepAlive) KeepAlive(cookie uint16) error { |
| 83 | + if err := k.proto.LockState([]protocol.State{STATE_CLIENT}); err != nil { |
| 84 | + return fmt.Errorf("%s: KeepAlive: protocol not in expected state", PROTOCOL_NAME) |
| 85 | + } |
| 86 | + msg := newMsgKeepAlive(cookie) |
| 87 | + // Unlock and change state when we're done |
| 88 | + defer k.proto.UnlockState(STATE_SERVER) |
| 89 | + // Send request |
| 90 | + return k.proto.SendMessage(msg, false) |
| 91 | +} |
| 92 | + |
| 93 | +func (k *KeepAlive) handleKeepAlive(msgGeneric protocol.Message) error { |
| 94 | + if err := k.proto.LockState([]protocol.State{STATE_CLIENT}); err != nil { |
| 95 | + return fmt.Errorf("received keep-alive KeepAlive message when protocol not in expected state") |
| 96 | + } |
| 97 | + msg := msgGeneric.(*msgKeepAlive) |
| 98 | + // Unlock and change state when we're done |
| 99 | + defer k.proto.UnlockState(STATE_CLIENT) |
| 100 | + if k.callbackConfig != nil && k.callbackConfig.KeepAliveFunc != nil { |
| 101 | + // Call the user callback function |
| 102 | + return k.callbackConfig.KeepAliveFunc(msg.Cookie) |
| 103 | + } else { |
| 104 | + // Send the keep-alive response |
| 105 | + resp := newMsgKeepAliveResponse(msg.Cookie) |
| 106 | + return k.proto.SendMessage(resp, true) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func (k *KeepAlive) handleKeepAliveResponse(msgGeneric protocol.Message) error { |
| 111 | + if err := k.proto.LockState([]protocol.State{STATE_SERVER}); err != nil { |
| 112 | + return fmt.Errorf("received keep-alive KeepAliveResponse message when protocol not in expected state") |
| 113 | + } |
| 114 | + msg := msgGeneric.(*msgKeepAliveResponse) |
| 115 | + // Unlock and change state when we're done |
| 116 | + defer k.proto.UnlockState(STATE_CLIENT) |
| 117 | + // Start the timer again if we had one previously |
| 118 | + if k.timer != nil { |
| 119 | + defer k.Start() |
| 120 | + } |
| 121 | + if k.callbackConfig != nil && k.callbackConfig.KeepAliveResponseFunc != nil { |
| 122 | + // Call the user callback function |
| 123 | + return k.callbackConfig.KeepAliveResponseFunc(msg.Cookie) |
| 124 | + } |
| 125 | + return nil |
| 126 | +} |
| 127 | + |
| 128 | +func (k *KeepAlive) handleDone() error { |
| 129 | + if err := k.proto.LockState([]protocol.State{STATE_CLIENT}); err != nil { |
| 130 | + return fmt.Errorf("received keep-alive Done message when protocol not in expected state") |
| 131 | + } |
| 132 | + // Unlock and change state when we're done |
| 133 | + defer k.proto.UnlockState(STATE_DONE) |
| 134 | + if k.callbackConfig != nil && k.callbackConfig.DoneFunc != nil { |
| 135 | + // Call the user callback function |
| 136 | + return k.callbackConfig.DoneFunc() |
| 137 | + } |
| 138 | + return nil |
| 139 | +} |
0 commit comments