Esempio n. 1
0
// subscribe will add the given protocol client to the channel to subscribe to
func (b *PubSubBackend) subscribe(data interface{}, client server.ProtocolClient) error {
	d, _ := data.([][]byte)
	if len(d) < 1 {
		return nil
	} else {
		for _, k := range d {
			key := string(k)
			if topic, ok := b.topics[key]; ok {
				topic.Lock()
				id := client.Address()
				if _, ok = topic.clients[id]; !ok {
					topic.clients[id] = empty
					topic.size++
				}
				topic.Unlock()
			} else {
				topic := new(TopicChannel)
				topic.clients = make(map[string]struct{})
				topic.size = 1
				topic.clients[client.Address()] = empty
				b.topics[key] = topic
			}
		}

		return nil
	}
}
Esempio n. 2
0
func (stats *StatsBackend) Count(data interface{}, client server.ProtocolClient) error {
	d, _ := data.([][]byte)
	if len(d) == 0 {
		client.WriteError(errors.New("COUNTER takes at least 1 parameter (i.e. key to increment)"))
		client.Flush()
		return nil
	} else {
		key, err := stats.readString(d[0])
		if err != nil {
			return err
		}
		values := d[1:]
		if len(values) > 0 {
			value, err := stats.readInt64(values[0])
			if err != nil {
				return err
			}
			_, err = stats.mem.CounterBy(key, value)
			return err
		} else {
			_, err := stats.mem.Counter(key)
			return err
		}
	}
}
Esempio n. 3
0
func (stats *StatsBackend) SRem(data interface{}, client server.ProtocolClient) error {
	d, _ := data.([][]byte)
	key := ""
	if len(d) < 2 {
		client.WriteError(errors.New("SREM takes at least 2 parameters (SREM key member)"))
		client.Flush()
		return nil
	} else {
		key = string(d[0])
		members := d[1:]
		result := int64(0)
		for _, v := range members {
			r, err := stats.mem.SRem(key, string(v))
			if err != nil {
				return err
			} else if r == -1 {
				return stats.FlushInt(r, nil, client)
			} else {
				result += r
			}
		}

		return stats.FlushInt(result, nil, client)
	}
}
Esempio n. 4
0
func (stats *StatsBackend) FlushInt(i int64, err error, client server.ProtocolClient) error {
	if err != nil {
		return err
	}
	client.WriteInt64(i)
	client.Flush()
	return nil
}
Esempio n. 5
0
func (stats *StatsBackend) Exists(data interface{}, client server.ProtocolClient) error {
	d, _ := data.([][]byte)
	if len(d) == 0 {
		client.WriteError(errors.New("EXISTS takes at least 1 parameter (i.e. key to find)"))
		client.Flush()
		return nil
	} else {
		key, err := stats.readString(d[0])
		if err != nil {
			return err
		}
		i, err := stats.mem.Exists(key)
		return stats.FlushInt(i, err, client)
	}
}
Esempio n. 6
0
func (stats *StatsBackend) Set(data interface{}, client server.ProtocolClient) error {
	d, _ := data.([][]byte)
	if len(d) < 2 {
		client.WriteError(errors.New("SET takes at least 2 parameters (i.e. key to set and value to set to)"))
		client.Flush()
		return nil
	} else {
		key, value, err := stats.readStringInt64(d)
		if err != nil {
			return err
		}
		i, err := stats.mem.Set(key, value)
		return stats.FlushInt(i, err, client)
	}
}
Esempio n. 7
0
func (stats *StatsBackend) Get(data interface{}, client server.ProtocolClient) error {
	d, _ := data.([][]byte)
	if len(d) == 0 {
		client.WriteError(errors.New("GET takes at least 1 parameter (i.e. key to get)"))
		client.Flush()
		return nil
	} else {
		key, err := stats.readString(d[0])
		if err != nil {
			return err
		}
		i, err := stats.mem.Get(key)
		if err == ErrNotFound {
			return stats.FlushNil(client)
		}
		return stats.FlushInt(i, err, client)
	}
}
Esempio n. 8
0
func (t *TermBackend) CatFile(data interface{}, client server.ProtocolClient) error {
	d, _ := data.([][]byte)
	if len(d) > 0 {
		fileName := string(d[0])
		content, err := ioutil.ReadFile(path.Join(t.homeDir, fileName))
		if err != nil {
			client.WriteError(err)
		} else {
			client.WriteBytes(content)
		}
		client.Flush()
	} else {
		client.WriteError(errors.New("cat takes at least 1 parameter (cat filename)"))
		client.Flush()
	}

	return nil
}
Esempio n. 9
0
func (stats *StatsBackend) Counters(data interface{}, client server.ProtocolClient) error {
	results, err := stats.mem.Counters()
	if err != nil {
		client.WriteError(err)
		client.Flush()
		return nil
	}

	client.WriteJson(results)
	client.Flush()
	return nil
}
Esempio n. 10
0
func (b *PubSubBackend) unsubscribe(data interface{}, client server.ProtocolClient) error {
	d, _ := data.([][]byte)
	if len(d) < 1 {
		return nil
	} else {
		for _, k := range d {
			key := string(k)
			if topic, ok := b.topics[key]; ok {
				topic.Lock()
				id := client.Address()
				if _, ok = topic.clients[id]; ok {
					delete(topic.clients, id)
					topic.size--
				}
				topic.Unlock()
			}
		}

		return nil
	}
}
Esempio n. 11
0
func (stats *StatsBackend) Del(data interface{}, client server.ProtocolClient) error {
	d, _ := data.([][]byte)
	if len(d) == 0 {
		client.WriteError(errors.New("DEL takes at least 1 parameter (i.e. key to delete)"))
		client.Flush()
		return nil
	} else {
		i := int64(0)
		for _, k := range d {
			key, err := stats.readString(k)
			if err != nil {
				return err
			}
			i2, err := stats.mem.Del(key)
			if err != nil {
				return err
			} else {
				i += i2
			}
		}
		return stats.FlushInt(i, nil, client)
	}
}
Esempio n. 12
0
func (stats *StatsBackend) Keys(data interface{}, client server.ProtocolClient) error {
	d, _ := data.([][]byte)
	key := ""
	if len(d) > 0 {
		pattern, err := stats.readString(d[0])
		if err != nil {
			return err
		}
		key = pattern
	}

	results, err := stats.mem.Keys(key)
	if err != nil {
		client.WriteError(err)
		client.Flush()
		return nil
	}
	client.WriteLen('*', len(results))
	for _, k := range results {
		client.WriteString(k)
	}
	client.Flush()
	return nil
}
Esempio n. 13
0
func (stats *StatsBackend) SUnion(data interface{}, client server.ProtocolClient) error {
	d, _ := data.([][]byte)
	if len(d) < 2 {
		client.WriteError(errors.New("SUNION takes at least 2 parameters (SINTER key [key ...])"))
		client.Flush()
		return nil
	} else {
		keys := make([]string, len(d))
		for i, v := range d {
			keys[i] = string(v)
		}
		results, err := stats.mem.SUnion(keys)
		if err != nil {
			return err
		}

		client.WriteLen('*', len(results))
		for k, _ := range results {
			client.WriteString(k)
		}
		client.Flush()
		return nil
	}
}
Esempio n. 14
0
func (t *TermBackend) ListFiles(data interface{}, client server.ProtocolClient) error {
	files, err := ioutil.ReadDir(t.homeDir)

	filenames := []interface{}{}
	if err != nil {
		client.WriteError(err)
		client.Flush()
	} else {
		for _, f := range files {
			if !f.IsDir() {
				filenames = append(filenames, f.Name())
			}
		}
		client.WriteArray(filenames)
		client.Flush()
	}
	return nil
}
Esempio n. 15
0
func (t *TermBackend) EditFile(data interface{}, client server.ProtocolClient) error {
	d, _ := data.([][]byte)
	if len(d) > 0 {
		fileName := string(d[0])
		content, err := ioutil.ReadFile(path.Join(t.homeDir, fileName))
		if err == nil {
			fileMap := make(map[string]string)
			fileMap["filename"] = fileName
			fileMap["contents"] = string(content)
			client.WriteJson(fileMap)
			client.Flush()
		} else {
			fileMap := make(map[string]string)
			fileMap["filename"] = fileName
			fileMap["contents"] = ""
			client.WriteJson(fileMap)
			client.Flush()
		}
	}

	return nil
}
Esempio n. 16
0
func (p *LineProtocol) RunClient(client server.ProtocolClient) {
	c, ok := client.(*LineProtocolClient)
	if !ok {
		client.WriteError(errInvalidProtocol)
		client.Close()
		return
	}

	defer func() {
		if e := recover(); e != nil {
			buf := make([]byte, 4096)
			n := runtime.Stack(buf, false)
			buf = buf[0:n]
			p.ctx.Events <- server.BroadcastEvent{"fatal", "client run panic", errors.New(fmt.Sprintf("%v", e)), buf}
		}

		c.Close()
		return
	}()

	reqErr := client.RequestErrorChan()
	for {
		data, err := c.readBulk()

		if err != nil {
			if err != io.EOF {
				p.ctx.Events <- server.BroadcastEvent{"error", "read error", err, nil}
			}
			return
		}

		err = p.handleData(data, c, reqErr)
		if err != nil {
			if err == errQuit {
				return
			} else {
				p.ctx.Events <- server.BroadcastEvent{"error", "accept error", err, nil}
				c.WriteError(err)
				c.Flush()
			}
		}
	}
}
Esempio n. 17
0
func (b *DefaultBackend) info(data interface{}, client server.ProtocolClient) error {
	status, _ := b.app.Status()
	client.WriteJson(status)
	client.Flush()
	return nil
}
Esempio n. 18
0
func (t *TermBackend) ShowResume(data interface{}, client server.ProtocolClient) error {
	client.WriteBytes(t.resumeText)
	client.Flush()
	return nil
}
Esempio n. 19
0
func (b *DefaultBackend) help(data interface{}, client server.ProtocolClient) error {
	help, _ := b.app.Help()
	client.WriteJson(help)
	client.Flush()
	return nil
}
Esempio n. 20
0
func (stats *StatsBackend) FlushNil(client server.ProtocolClient) error {
	client.WriteNull()
	client.Flush()
	return nil
}
Esempio n. 21
0
func (b *DefaultBackend) echo(data interface{}, client server.ProtocolClient) error {
	d, okInterface := data.([]interface{})
	if okInterface {
		if len(d) == 0 {
			client.WriteString("")
			client.Flush()
			return nil
		} else {
			if len(d) == 1 {
				fmt.Printf("%v", d[0])
				client.WriteInterface(d[0])
			} else {
				client.WriteArray(d)
			}
			client.Flush()
			return nil
		}
	} else {
		b, _ := data.([][]byte)
		if len(b) == 0 {
			client.WriteString("")
			client.Flush()
			return nil
		} else {
			if len(b) == 1 {
				client.WriteString(string(b[0]))
			} else {
				s := make([]string, len(b))
				for i, k := range b {
					s[i] = string(k)
				}
				client.WriteString(strings.Join(s, " "))
			}
			client.Flush()
			return nil
		}
	}
}
Esempio n. 22
0
func (t *TermBackend) SaveFile(data interface{}, client server.ProtocolClient) error {
	d, _ := data.([][]byte)
	if len(d) >= 2 {
		fileName := string(d[0])
		content := d[1]
		err := ioutil.WriteFile(fileName, content, 0644)
		if err != nil {
			client.WriteError(err)
			client.Flush()
		} else {
			client.WriteString("saved " + fileName + " successfully")
			client.Flush()
		}
	} else {
		client.WriteError(errors.New("save takes at least 2 parameters (save filename filecontents...)"))
		client.Flush()
	}

	return nil
}
Esempio n. 23
0
func (b *BGraphBackend) IntersectEdges(data interface{}, client server.ProtocolClient) error {
	d, _ := data.([][]byte)
	if len(d) < 2 {
		client.WriteError(errors.New("&e takes at least 2 parameters (&e vertex [vertex ...])"))
		client.Flush()
		return nil
	}

	keys := make([]string, len(d))
	for i, k := range d {
		keys[i] = string(k)
	}

	results := b.db.sumIntersectEdges(keys)
	if results != nil && len(results) > 0 {
		client.WriteJson(results)
		client.Flush()
	} else {
		client.WriteNull()
		client.Flush()
	}

	return nil
}
Esempio n. 24
0
func (stats *StatsBackend) SIsMember(data interface{}, client server.ProtocolClient) error {
	d, _ := data.([][]byte)
	if len(d) < 2 {
		client.WriteError(errors.New("SISMEMBER takes 2 parameters (SISMEMBER key member)"))
		client.Flush()
		return nil
	} else {
		key := string(d[0])
		results := make([]int64, len(d)-1)
		for i, v := range d[1:] {
			result, err := stats.mem.SIsMember(key, string(v))
			if err != nil {
				return err
			}
			results[i] = result
		}

		if len(results) > 1 {
			client.WriteLen('*', len(results))
			for _, v := range results {
				client.WriteInt64(v)
			}
		} else {
			client.WriteInt64(results[0])
		}
		client.Flush()
		return nil
	}
}
Esempio n. 25
0
func (stats *StatsBackend) SMembers(data interface{}, client server.ProtocolClient) error {
	d, _ := data.([][]byte)
	if len(d) == 0 {
		client.WriteError(errors.New("SMEMBERS takes 1 parameter (SMEMBERS key)"))
		client.Flush()
		return nil
	} else {
		values, err := stats.mem.SMembers(string(d[0]))
		if err != nil {
			return err
		}

		if values == nil {
			client.WriteNull()
			client.Flush()
			return nil
		}

		client.WriteLen('*', len(values))
		for k, _ := range values {
			client.WriteString(k)
		}
		client.Flush()
		return nil
	}
}
Esempio n. 26
0
func (b *DefaultBackend) ping(data interface{}, client server.ProtocolClient) error {
	client.WriteString(PONG)
	client.Flush()
	return nil
}
Esempio n. 27
0
func (b *BGraphBackend) FindEdges(data interface{}, client server.ProtocolClient) error {
	d, _ := data.([][]byte)
	if len(d) < 1 {
		client.WriteError(errors.New("*e takes at least 1 parameter (*e vertex [vertex ...])"))
		client.Flush()
		return nil
	}

	vertexEdges := make(map[string]map[string]float64)
	for _, k := range d {
		key := string(k)
		edges := b.db.findEdges(key)
		if edges != nil {
			vertexEdges[key] = edges
		}
	}

	if len(vertexEdges) > 0 {
		client.WriteJson(vertexEdges)
		client.Flush()
	} else {
		client.WriteNull()
		client.Flush()
	}
	return nil
}
Esempio n. 28
0
func (stats *StatsBackend) SCard(data interface{}, client server.ProtocolClient) error {
	d, _ := data.([][]byte)
	if len(d) < 1 {
		client.WriteError(errors.New("SCARD takes at least 1 parameter (SCARD key)"))
		client.Flush()
		return nil
	} else {
		results := make([]int64, len(d))
		for i, v := range d {
			r, err := stats.mem.SCard(string(v))
			if err != nil {
				return err
			} else {
				results[i] = r
			}
		}

		if len(d) > 1 {
			client.WriteLen('*', len(d))
			for _, v := range results {
				client.WriteInt64(v)
			}
		} else {
			client.WriteInt64(results[0])
		}
		client.Flush()
		return nil
	}
}
Esempio n. 29
0
func (p *RedisProtocol) RunClient(client server.ProtocolClient) {
	// defer panics to the loggable event routine
	defer func() {
		if e := recover(); e != nil {
			buf := make([]byte, 4096)
			n := runtime.Stack(buf, false)
			buf = buf[0:n]
			p.ctx.Events <- server.BroadcastEvent{"fatal", "client run panic", errors.New(fmt.Sprintf("%v", e)), buf}
		}

		client.Close()
		return
	}()

	reqErr := client.RequestErrorChan()
	for {
		data, err := client.ReadBulkPayload()
		if err != nil {
			if err != io.EOF {
				p.ctx.Events <- server.BroadcastEvent{"error", "read error", err, nil}
			}
			return
		}

		err = p.handleData(data, client, reqErr)
		if err != nil {
			if err == errQuit {
				client.WriteString("OK")
				client.Flush()
				return
			} else {
				p.ctx.Events <- server.BroadcastEvent{"error", "accept error", err, nil}
				client.WriteError(err)
				client.Flush()
			}
		}
	}
}
Esempio n. 30
0
func (stats *StatsBackend) SDiff(data interface{}, client server.ProtocolClient) error {
	d, _ := data.([][]byte)
	if len(d) < 2 {
		client.WriteError(errors.New("SDIFF requires at least 2 parameters (SDIFF key [key ...])"))
		client.Flush()
		return nil
	} else {
		results, err := stats.mem.SMembers(string(d[0]))
		if err != nil {
			return err
		} else if results == nil {
			client.WriteNull()
			client.Flush()
			return nil
		} else {
			for _, v := range d[1:] {
				results, err = stats.mem.SDiff(results, string(v))
				if err != nil {
					return err
				} else if len(results) == 0 {
					break
				}
			}

			client.WriteLen('*', len(results))
			for k, _ := range results {
				client.WriteString(k)
			}
			client.Flush()
			return nil
		}
	}
}