github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/core/fsm.go (about) 1 /* 2 Copyright IBM Corp. 2016 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package core 18 19 import "github.com/looplab/fsm" 20 21 // PeerConnectionFSM example FSM for demonstration purposes. 22 type PeerConnectionFSM struct { 23 To string 24 FSM *fsm.FSM 25 } 26 27 // NewPeerConnectionFSM creates and returns a PeerConnectionFSM 28 func NewPeerConnectionFSM(to string) *PeerConnectionFSM { 29 d := &PeerConnectionFSM{ 30 To: to, 31 } 32 33 d.FSM = fsm.NewFSM( 34 "created", 35 fsm.Events{ 36 {Name: "HELLO", Src: []string{"created"}, Dst: "established"}, 37 {Name: "GET_PEERS", Src: []string{"established"}, Dst: "established"}, 38 {Name: "PEERS", Src: []string{"established"}, Dst: "established"}, 39 {Name: "PING", Src: []string{"established"}, Dst: "established"}, 40 {Name: "DISCONNECT", Src: []string{"created", "established"}, Dst: "closed"}, 41 }, 42 fsm.Callbacks{ 43 "enter_state": func(e *fsm.Event) { d.enterState(e) }, 44 "before_HELLO": func(e *fsm.Event) { d.beforeHello(e) }, 45 "after_HELLO": func(e *fsm.Event) { d.afterHello(e) }, 46 "before_PING": func(e *fsm.Event) { d.beforePing(e) }, 47 "after_PING": func(e *fsm.Event) { d.afterPing(e) }, 48 }, 49 ) 50 51 return d 52 } 53 54 func (d *PeerConnectionFSM) enterState(e *fsm.Event) { 55 log.Debugf("The bi-directional stream to %s is %s, from event %s\n", d.To, e.Dst, e.Event) 56 } 57 58 func (d *PeerConnectionFSM) beforeHello(e *fsm.Event) { 59 log.Debugf("Before reception of %s, dest is %s, current is %s", e.Event, e.Dst, d.FSM.Current()) 60 } 61 62 func (d *PeerConnectionFSM) afterHello(e *fsm.Event) { 63 log.Debugf("After reception of %s, dest is %s, current is %s", e.Event, e.Dst, d.FSM.Current()) 64 } 65 66 func (d *PeerConnectionFSM) afterPing(e *fsm.Event) { 67 log.Debugf("After reception of %s, dest is %s, current is %s", e.Event, e.Dst, d.FSM.Current()) 68 } 69 70 func (d *PeerConnectionFSM) beforePing(e *fsm.Event) { 71 log.Debugf("Before %s, dest is %s, current is %s", e.Event, e.Dst, d.FSM.Current()) 72 }