Example #1
0
func (this *Connection) Call(cmd *Command) (interface{}, error) {
	if cmd.Key == "" {
		cmd.Key = betterguid.Ascending()
	}
	match, ok := this.stats.Get(cmd.Action)
	if !ok {
		match = new(Stats)
		this.stats.Set(cmd.Action, match)
	}
	stats := match.(*Stats)
	for {
		block := this.Enqueue(cmd.Key)
		err := this.Fire(cmd)
		if err != nil {
			return nil, err
		}
		result := <-block
		switch result.Action {
		case EXCEPTION:
			message := dynamic.String(result.Map(), "message")
			return nil, Exception(message)
		case ERROR:
			message := dynamic.String(result.Map(), "message")
			return nil, Error(message)
		case RESPONSE:
			atomic.AddInt64(&stats.Success, 1)
			return result.Body, nil
		}
	}
}
Example #2
0
func (this *Server) Listen(host string) error {
	return this.Transport.Listen(host, func(raw io.ReadWriteCloser) {
		conn := Accept(this.Protocol, raw)
		conn.Processor = this.Processor
		for _, cb := range this.connect {
			err := cb(conn)
			if err != nil {
				log.Println(err)
				conn.Close()
				return
			}
		}
		key := betterguid.Ascending()
		defer func() {
			this.inbound.Remove(key)
			for _, cb := range this.disconnect {
				cb(conn)
			}
			conn.Close()
		}()
		this.inbound.Set(key, conn)
		conn.Read()
	})
}