• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang cells.Event类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/tideland/golib/cells.Event的典型用法代码示例。如果您正苦于以下问题:Golang Event类的具体用法?Golang Event怎么用?Golang Event使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了Event类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: payloadCents

// 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)
}
开发者ID:kung-foo,项目名称:cellchat,代码行数:8,代码来源:fsm_test.go


示例2: Locked

// Locked represents the locked state receiving coins.
func (m *lockMachine) Locked(ctx cells.Context, 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().EmitNew("restorer", "drop!", m.cents, event.Scene())
			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())
}
开发者ID:kung-foo,项目名称:cellchat,代码行数:31,代码来源:fsm_test.go


示例3: ProcessEvent

// 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.ctx, event)
		if err != nil {
			b.done = true
			b.err = err
		} else if state == nil {
			b.done = true
		}
		b.state = state
	}
	return nil
}
开发者ID:kung-foo,项目名称:cellchat,代码行数:27,代码来源:fsm.go


示例4: newEventData

// 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
}
开发者ID:kung-foo,项目名称:cellchat,代码行数:8,代码来源:collector.go


示例5: ProcessEvent

// 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
}
开发者ID:kung-foo,项目名称:golib,代码行数:9,代码来源:callback.go


示例6: ProcessEvent

// 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.ctx.ID(),
			TickerTimePayload: time.Now(),
		}
		b.ctx.EmitNew(TickerTopic, pvs, nil)
	}
	return nil
}
开发者ID:kung-foo,项目名称:golib,代码行数:12,代码来源:ticker.go


示例7: ProcessEvent

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
}
开发者ID:kung-foo,项目名称:cellchat,代码行数:11,代码来源:fsm_test.go


示例8: filter

func (c *censor) filter(event cells.Event) bool {
	message, _ := event.Payload().Get("message")
	user, _ := event.Payload().Get("from")
	for _, w := range strings.Split(message.(string), " ") {
		if _, ok := c.words[w]; ok {
			log.Infof("%s said a bad word: %s", user, w)
			c.warnings[user.(string)]++
			return false
		}
	}
	return true
}
开发者ID:kung-foo,项目名称:cellchat,代码行数:12,代码来源:services.go


示例9: Configuration

// Configuration returns the configuration payload
// of the passed event or an empty configuration.
func Configuration(event cells.Event) configuration.Configuration {
	payload, ok := event.Payload().Get(ConfigurationPayload)
	if !ok {
		logger.Warningf("event does not contain configuration payload")
		config, _ := configuration.ReadString("{config}")
		return config
	}
	config, ok := payload.(configuration.Configuration)
	if !ok {
		logger.Warningf("configuration payload has illegal type")
		config, _ := configuration.ReadString("{config}")
		return config
	}
	return config
}
开发者ID:kung-foo,项目名称:golib,代码行数:17,代码来源:configurator.go


示例10: ProcessEvent

// 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.ctx.Emit(event)
	}
	return nil
}
开发者ID:kung-foo,项目名称:cellchat,代码行数:20,代码来源:collector.go


示例11: ProcessEvent

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.ctx.SubscribersDo(func(s cells.Subscriber) error {
			return s.ProcessNewEvent("love", b.ctx.ID()+" loves "+s.ID(), event.Scene())
		})
		if err != nil {
			return err
		}
	case panicTopic:
		panic("Ouch!")
	case subscribersTopic:
		var ids []string
		b.ctx.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.ctx.Emit(event)
	}
	return nil
}
开发者ID:kung-foo,项目名称:golib,代码行数:41,代码来源:behaviors_test.go


示例12: ProcessEvent

// 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)
		config, err := configuration.ReadFile(filename)
		if err != nil {
			return errors.Annotate(err, ErrCannotReadConfiguration, errorMessages)
		}
		// If wanted then validate it.
		if b.validate != nil {
			err = b.validate(config)
			if err != nil {
				return errors.Annotate(err, ErrCannotValidateConfiguration, errorMessages)
			}
		}
		// All done, emit it.
		pvs := cells.PayloadValues{
			ConfigurationPayload: config,
		}
		b.ctx.EmitNew(ConfigurationTopic, pvs, event.Scene())
	}
	return nil
}
开发者ID:kung-foo,项目名称:golib,代码行数:30,代码来源:configurator.go


示例13: ProcessEvent

// 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.ctx.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.ctx.EmitNew(topic, b.counters[cid], event.Scene())
			}
		}
	}
	return nil
}
开发者ID:kung-foo,项目名称:cellchat,代码行数:28,代码来源:counter.go


示例14: ProcessEvent

func (u *user) ProcessEvent(event cells.Event) error {
	switch event.Topic() {
	case SAYS_TO:
		to, ok := event.Payload().Get("to")
		if !ok || to.(string) != makeUserID(u.name) {
			return nil
		}
		log.Info(event.Payload())
	case SAY:
		env := u.ctx.Environment()
		roomID, _ := event.Payload().Get("room")

		if !env.HasCell(roomID.(string)) {
			log.Panicf("%s not found", roomID)
			return fmt.Errorf("room %s not found", roomID)
		}

		ok, err := env.Request(roomID.(string), IN_ROOM, cells.PayloadValues{"user": makeUserID(u.name)}, nil, time.Second)

		if err != nil {
			log.Panic(err)
			return err
		}

		if !ok.(bool) {
			return fmt.Errorf("%s is not in %s", u.name, roomID)
		}

		payload := event.Payload()
		payload = payload.Apply(cells.PayloadValues{"from": makeUserID(u.name)})

		env.EmitNew(roomID.(string), SAYS_ALL, payload, nil)
	case USER_DISCONNECTED:
		log.Info(USER_DISCONNECTED)
	}
	return nil
}
开发者ID:kung-foo,项目名称:cellchat,代码行数:37,代码来源:user.go


示例15: ProcessEvent

// ProcessEvent stores and flags the event in the event scene.
// So other code parts using the same scene can wait for the signal.
func (b *sceneBehavior) ProcessEvent(event cells.Event) error {
	scn := event.Scene()
	if scn != nil {
		err := scn.StoreAndFlag(event.Topic(), event.Payload())
		if err != nil {
			return err
		}
	}
	return nil
}
开发者ID:kung-foo,项目名称:golib,代码行数:12,代码来源:scene.go


示例16: ProcessEvent

func (b *building) ProcessEvent(event cells.Event) (err error) {
	switch event.Topic() {
	case ROOM_ADDED:
		room, _ := event.Payload().Get("room")
		b.rooms[room.(string)] = true
		log.Infof("added %v", room)
	case LIST_ROOMS:
		rooms := []string{}
		for room := range b.rooms {
			rooms = append(rooms, room)
		}
		event.Respond(rooms)
	case SAYS_ALL:
		user, ok := event.Payload().Get("from")
		if !ok || user.(string) != PublicAddressUserID {
			return fmt.Errorf("invalid public address request")
		}
		b.ctx.Emit(event)
	}
	return
}
开发者ID:kung-foo,项目名称:cellchat,代码行数:21,代码来源:building.go


示例17: ProcessEvent

func (wsb *webSocketBehavior) ProcessEvent(event cells.Event) (err error) {
	switch event.Topic() {
	case SAYS_TO:
		to, ok := event.Payload().Get("to")
		if !ok || to.(string) != makeUserID(wsb.user) {
			return
		}
		msg, err := json.Marshal(payloadToValues(event.Payload()))
		if err != nil {
			log.Fatal(err)
		}
		wsb.output <- msg
	case SAYS_ALL:
		msg, err := json.Marshal(payloadToValues(event.Payload()))
		if err != nil {
			log.Fatal(err)
		}
		wsb.output <- msg
	}
	return
}
开发者ID:kung-foo,项目名称:cellchat,代码行数:21,代码来源:server.go


示例18: ProcessEvent

func (c *censor) ProcessEvent(event cells.Event) (err error) {
	switch event.Topic() {
	case CENSOR:
		ok := c.filter(event)
		userID, _ := event.Payload().Get("from")
		event.Respond(ok)

		if _, ok := c.warnings[userID.(string)]; !ok {
			c.warnings[userID.(string)] = 0
		}

		if !ok {
			return c.ctx.EmitNew(SAYS_TO, cells.PayloadValues{
				"message": fmt.Sprintf("You can't say that! You've been warned %d times.", c.warnings[userID.(string)]),
				"from":    c.id,
				"to":      userID.(string),
			}, nil)
		}
	}
	return
}
开发者ID:kung-foo,项目名称:cellchat,代码行数:21,代码来源:services.go


示例19: ProcessEvent

func (r *room) ProcessEvent(event cells.Event) (err error) {
	switch event.Topic() {
	case SAYS_ALL, SAYS_TO:
		user, _ := event.Payload().Get("from")
		switch user.(string) {
		case PublicAddressUserID, r.censorID:
			r.ctx.Emit(event)
		default:
			env := r.ctx.Environment()
			ok, err := env.Request(r.censorID, CENSOR, event.Payload(), nil, time.Second)

			if err != nil {
				log.Error(err)
				return err
			}

			if ok.(bool) {
				r.ctx.Emit(event)
			}
		}
	case IN_ROOM:
		userID, _ := event.Payload().Get("user")
		ok, _ := r.users[userID.(string)]
		event.Respond(ok)
	case USER_ADDED:
		userID, _ := event.Payload().Get("user")
		if ok, _ := r.users[userID.(string)]; !ok {
			r.users[userID.(string)] = true
			r.ctx.Environment().EmitNew(r.ctx.ID(), SAYS_ALL, cells.PayloadValues{
				"message": fmt.Sprintf("%s has entered the room", userID),
				"from":    PublicAddressUserID,
			}, nil)
		}
	case USER_COUNT:
		event.Respond(len(r.users))
	}
	return
}
开发者ID:kung-foo,项目名称:cellchat,代码行数:38,代码来源:room.go


示例20: Unlocked

// Unlocked represents the unlocked state receiving coins.
func (m *lockMachine) Unlocked(ctx cells.Context, 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.EmitNew("return", cents, event.Scene())
		return m.Unlocked, nil
	case "button-press!":
		ctx.Environment().EmitNew("restorer", "drop!", m.cents, event.Scene())
		m.cents = 0
		return m.Locked, nil
	}
	return m.Unlocked, fmt.Errorf("illegal topic in state 'unlocked': %s", event.Topic())
}
开发者ID:kung-foo,项目名称:cellchat,代码行数:19,代码来源:fsm_test.go



注:本文中的github.com/tideland/golib/cells.Event类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang errors.Annotate函数代码示例发布时间:2022-05-28
下一篇:
Golang cells.NewEnvironment函数代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap