Exemple #1
0
func (p *LookupProtocolV1) UnRegister(client *ClientV1, reader *bufio.Reader, params []string) ([]byte, error) {
	var buf []byte
	var bodyLen int32
	var err error

	err = binary.Read(reader, binary.BigEndian, &bodyLen)
	if err != nil {
		return nil, proto.NewFatalClientErr(err, "E_INVALID_BODY", fmt.Sprintf("UNREGISTER failed"))
	}

	buf = make([]byte, bodyLen)
	_, err = io.ReadFull(reader, buf)
	if err != nil {
		return nil, proto.NewFatalClientErr(err, "E_INVALID_BODY", fmt.Sprintf("UNREGISTER failed"))
	}

	files := strings.Split(string(buf), "+")

	for _, file := range files {
		p.ctx.s.Hold.RemoveFileProducer(file, client.peerInfo)
	}

	p.ctx.s.logf("client %s UNREGISTER file: %s", client, buf)
	return []byte("OK"), nil
}
Exemple #2
0
func (p *LookupProtocolV1) Identify(client *ClientV1, reader *bufio.Reader, params []string) ([]byte, error) {
	var buf []byte
	var err error
	//buf := make([]buf, 4)

	if client.peerInfo != nil {
		return nil, proto.NewFatalClientErr(nil, "E_INVALID", fmt.Sprintf("client cannot identify again"))
	}

	var bodyLen int32
	err = binary.Read(reader, binary.BigEndian, &bodyLen)
	if err != nil {
		return nil, proto.NewFatalClientErr(err, "E_BAD_BODY", fmt.Sprintf("IDENTIFY failed"))
	}

	buf = make([]byte, bodyLen)
	_, err = io.ReadFull(reader, buf)
	if err != nil {
		return nil, proto.NewFatalClientErr(err, "E_BAD_BODY", fmt.Sprintf("IDENTIFY failed"))
	}

	peerInfo := PeerInfo{id: client.RemoteAddr().String()}
	err = json.Unmarshal(buf, &peerInfo)
	if err != nil {
		return nil, proto.NewFatalClientErr(err, "E_BAD_BODY", fmt.Sprintf("IDENTIFY failed"))
	}
	p.ctx.s.logf("register: %+v", peerInfo)
	//peerInfo.RemoteAddr = client.RemoteAddr().String()

	tnow := time.Now().Unix()
	atomic.StoreInt64(&peerInfo.lastActive, tnow)

	client.peerInfo = &peerInfo

	data := make(map[string]interface{})
	hostname, err := os.Hostname()
	if err != nil {
		p.ctx.s.logf("FATAL, quit now")
		os.Exit(1)
	}
	data["hostname"] = hostname
	data["tcp_address"] = p.ctx.s.Opts.TcpAddress
	buf, err = json.Marshal(data)
	if err != nil {
		return []byte("OK"), nil
	}
	p.ctx.s.logf("client: %s identify success", client)
	return buf, nil
}
Exemple #3
0
func (p *LookupProtocolV1) Load(client *ClientV1, reader *bufio.Reader, params []string) ([]byte, error) {
	var buf []byte
	var bodyLen int32
	var err error

	err = binary.Read(reader, binary.BigEndian, &bodyLen)
	if err != nil {
		return nil, proto.NewFatalClientErr(err, "E_INVALID_BODY", fmt.Sprintf("LOAD failed"))
	}

	buf = make([]byte, bodyLen)
	_, err = io.ReadFull(reader, buf)
	if err != nil {
		return nil, proto.NewFatalClientErr(err, "E_INVALID_BODY", fmt.Sprintf("LOAD failed"))
	}

	p.ctx.s.logf("client: %s, load: %s", client, buf)
	//client.peerInfo.UpdateLoad()
	return []byte("OK"), nil
}
Exemple #4
0
func (p *LookupProtocolV1) Lookup(client *ClientV1, reader *bufio.Reader, params []string) ([]byte, error) {
	file := params[0]

	producer := p.ctx.s.Hold.FindProperProducer(file)
	if producer == nil {
		return []byte("E_NOT_FOUND"), nil
	}

	pbuf, err := json.Marshal(producer.peerInfo)
	if err != nil {
		return nil, proto.NewFatalClientErr(err, "E_INTERNAL_ERROR", fmt.Sprintf("LOOKUP failed"))
	}
	p.ctx.s.logf("send lookup data: %s", pbuf)
	return pbuf, nil
}
Exemple #5
0
func (p *LookupProtocolV1) Exec(client *ClientV1, reader *bufio.Reader, params []string) ([]byte, error) {
	cmd := params[0]
	switch cmd {
	case "IDENTIFY":
		return p.Identify(client, reader, params[1:])
	case "REGISTER":
		return p.Register(client, reader, params[1:])
	case "UNREGISTER":
		return p.UnRegister(client, reader, params[1:])
	case "LOOKUP":
		return p.Lookup(client, reader, params[1:])
	case "LOOKUPALL":
		return p.LookupAll(client, reader, params[1:])
	case "LOAD":
		return p.Load(client, reader, params[1:])
	case "PING":
		return p.Ping(client)
	}
	return nil, proto.NewFatalClientErr(nil, "E_INVALID", fmt.Sprintf("invalid command"))
}
Exemple #6
0
func (p *protocolV1) Exec(client *Client, params [][]byte) error {
	if "GET" != string(params[0]) {
		return proto.NewFatalClientErr(nil, "E_BAD_PROTOCOL", "bad protocol: "+string(params[0]))
	}
	file := string(params[1])
	//typ := string(params[2])
	switch {
	case bytes.Equal(params[2], []byte("META")):
		return p.SendMetaInfo(client, file)
	case bytes.Equal(params[2], []byte("DATA")):
		start, _ := strconv.Atoi(string(params[3]))
		end, _ := strconv.Atoi(string(params[4]))
		startPos, endPos := int64(start), int64(end)
		p.ctx.s.wg.Wrap(func() {
			p.processBlockRequest(client, file, startPos, endPos)
		})
		return nil
	}
	return nil
}