// ProcessEvent executes the state function and stores // the returned new state. func (b *fsmBehavior) ProcessEvent(event cells.Event) error { switch event.Topic() { case cells.StatusTopic: status := FSMStatus{ Done: b.done, Error: b.err, } if err := event.Respond(status); err != nil { return err } default: if b.done { return nil } state, err := b.state(b.cell, event) if err != nil { b.done = true b.err = err } else if state == nil { b.done = true } b.state = state } return nil }
// newEventData returns the passed event as event data to collect. func newEventData(event cells.Event) EventData { data := EventData{ Topic: event.Topic(), Payload: event.Payload(), } return data }
// Locked represents the locked state receiving coins. func (m *lockMachine) Locked(ctx cells.Cell, event cells.Event) (behaviors.FSMState, error) { switch event.Topic() { case "cents?": return m.Locked, event.Respond(m.cents) case "info?": info := fmt.Sprintf("state 'locked' with %d cents", m.cents) return m.Locked, event.Respond(info) case "coin!": cents := payloadCents(event) if cents < 1 { return nil, fmt.Errorf("do not insert buttons") } m.cents += cents if m.cents > 100 { m.cents -= 100 return m.Unlocked, nil } return m.Locked, nil case "button-press!": if m.cents > 0 { ctx.Environment().EmitNewContext("restorer", "drop!", m.cents, event.Context()) m.cents = 0 } return m.Locked, nil case "screwdriver!": // Allow a screwdriver to bring the lock into an undefined state. return nil, nil } return m.Locked, fmt.Errorf("illegal topic in state 'locked': %s", event.Topic()) }
// cents retrieves the cents out of the payload of an event. func payloadCents(event cells.Event) int { cents, ok := event.Payload().Get(cells.DefaultPayload) if !ok { return -1 } return cents.(int) }
// ProcessEvent calls a callback functions with the event data. func (b *callbackBehavior) ProcessEvent(event cells.Event) error { for _, callbackFunc := range b.callbackFuncs { if err := callbackFunc(event.Topic(), event.Payload()); err != nil { return err } } return nil }
// PrecessEvent emits a ticker event each time the // defined duration elapsed. func (b *tickerBehavior) ProcessEvent(event cells.Event) error { if event.Topic() == TickerTopic { pvs := cells.PayloadValues{ TickerIDPayload: b.cell.ID(), TickerTimePayload: time.Now(), } b.cell.EmitNew(TickerTopic, pvs) } return nil }
func (b *restorerBehavior) ProcessEvent(event cells.Event) error { switch event.Topic() { case "grab!": cents := b.cents b.cents = 0 return event.Respond(cents) case "drop!": b.cents += payloadCents(event) } return nil }
// Configuration returns the configuration payload // of the passed event or an empty configuration. func Configuration(event cells.Event) etc.Etc { payload, ok := event.Payload().Get(ConfigurationPayload) if !ok { logger.Warningf("event does not contain configuration payload") cfg, _ := etc.ReadString("{etc}") return cfg } cfg, ok := payload.(etc.Etc) if !ok { logger.Warningf("configuration payload has illegal type") cfg, _ := etc.ReadString("{etc}") return cfg } return cfg }
// ProcessEvent collects and re-emits events. func (b *collectorBehavior) ProcessEvent(event cells.Event) error { switch event.Topic() { case cells.CollectedTopic: response := make([]EventData, len(b.collected)) copy(response, b.collected) if err := event.Respond(response); err != nil { return err } case cells.ResetTopic: b.collected = []EventData{} default: b.collected = append(b.collected, newEventData(event)) if len(b.collected) > b.max { b.collected = b.collected[1:] } b.cell.Emit(event) } return nil }
func (b *collectBehavior) ProcessEvent(event cells.Event) error { switch event.Topic() { case cells.ProcessedTopic: processed := make([]string, len(b.processed)) copy(processed, b.processed) err := event.Respond(processed) if err != nil { return err } case cells.ResetTopic: b.processed = []string{} case cells.PingTopic: err := event.Respond(cells.PongResponse) if err != nil { return err } case iterateTopic: err := b.c.SubscribersDo(func(s cells.Subscriber) error { return s.ProcessNewEvent("love", b.c.ID()+" loves "+s.ID(), event.Context()) }) if err != nil { return err } case panicTopic: panic("Ouch!") case subscribersTopic: var ids []string b.c.SubscribersDo(func(s cells.Subscriber) error { ids = append(ids, s.ID()) return nil }) err := event.Respond(ids) if err != nil { return err } default: b.processed = append(b.processed, fmt.Sprintf("%v", event)) return b.c.Emit(event) } return nil }
// ProcessEvent reads, validates and emits a configuration. func (b *configuratorBehavior) ProcessEvent(event cells.Event) error { switch event.Topic() { case ReadConfigurationTopic: // Read configuration filename, ok := event.Payload().GetString(ConfigurationFilenamePayload) if !ok { logger.Errorf("cannot read configuration without filename payload") return nil } logger.Infof("reading configuration from %q", filename) cfg, err := etc.ReadFile(filename) if err != nil { return errors.Annotate(err, ErrCannotReadConfiguration, errorMessages) } // If wanted then validate it. if b.validate != nil { err = b.validate(cfg) if err != nil { return errors.Annotate(err, ErrCannotValidateConfiguration, errorMessages) } } // All done, emit it. pvs := cells.PayloadValues{ ConfigurationPayload: cfg, } b.cell.EmitNewContext(ConfigurationTopic, pvs, event.Context()) } return nil }
// ProcessEvent counts the event for the return value of the counter func // and emits this value. func (b *counterBehavior) ProcessEvent(event cells.Event) error { switch event.Topic() { case cells.CountersTopic: response := b.copyCounters() if err := event.Respond(response); err != nil { return err } case cells.ResetTopic: b.counters = make(map[string]int64) default: cids := b.counterFunc(b.cell.ID(), event) if cids != nil { for _, cid := range cids { v, ok := b.counters[cid] if ok { b.counters[cid] = v + 1 } else { b.counters[cid] = 1 } topic := "counter:" + cid b.cell.EmitNewContext(topic, b.counters[cid], event.Context()) } } } return nil }
func (b *emitBehavior) ProcessEvent(event cells.Event) error { return b.c.EmitNew(sleepTopic, event.Payload()) }
// Unlocked represents the unlocked state receiving coins. func (m *lockMachine) Unlocked(ctx cells.Cell, event cells.Event) (behaviors.FSMState, error) { switch event.Topic() { case "cents?": return m.Unlocked, event.Respond(m.cents) case "info?": info := fmt.Sprintf("state 'unlocked' with %d cents", m.cents) return m.Unlocked, event.Respond(info) case "coin!": cents := payloadCents(event) ctx.EmitNewContext("return", cents, event.Context()) return m.Unlocked, nil case "button-press!": ctx.Environment().EmitNewContext("restorer", "drop!", m.cents, event.Context()) m.cents = 0 return m.Locked, nil } return m.Unlocked, fmt.Errorf("illegal topic in state 'unlocked': %s", event.Topic()) }