Esempio n. 1
0
func (svr *TLSServer) SetKey(key string, value []byte, flags int16, expiry *time.Time) {
	keymd5 := ch.NewMD5Key(key)
	nodes := svr.ServerNode.GetNodesFor(keymd5, 3)
	svr.Logger.Debug("Server", "SetKey: %d peers for key %02X", len(nodes), keymd5)
	for _, node := range nodes {
		if node.ID == svr.ServerNode.ID {
			svr.Logger.Debug("Server", "SetKey: Peer for key %02X -> %02X (Local)", keymd5, node.ID)
			// Local set.
			svr.KVStore.Set(key, value, flags, expiry)
		} else {
			svr.Logger.Debug("Server", "SetKey: Peer for key %02X -> %02X (Remote)", keymd5, node.ID)

			peer := svr.Connections[node.ID]
			if peer.State != PeerStateConnected {
				svr.Logger.Warn("Server", "SetKey: Peer for key %02X -> %02X (Remote) Unavailable", keymd5, node.ID)
				continue
			}
			// Remote Set.
			payload := packets.KVStorePacket{
				Command:   packets.CMD_KVSTORE_SET,
				Key:       key,
				KeyHash:   keymd5,
				Data:      value,
				ExpiresAt: expiry,
				Flags:     flags,
				TargetID:  node.ID,
			}
			packet := packets.NewPacket(packets.CMD_KVSTORE, payload)
			peer.SendPacketWaitReply(packet, 5*time.Second)
		}
	}
}
Esempio n. 2
0
func (kvs *KVStore) Get(key string) ([]byte, int16, bool) {
	keymd5 := ch.NewMD5Key(key)
	ok, valueInt := kvs.store.Get(keymd5)
	if ok {
		value := valueInt.(*Item)
		kvs.Logger.Debug("KVStore", "GET [%s] %s", value.Key, value.Data)
		return value.Data, value.Flags, true
	} else {
		kvs.Logger.Debug("KVStore", "GET [%s] NOT FOUND", key)
		return nil, 0, false
	}
}
Esempio n. 3
0
func (kvs *KVStore) Set(key string, value []byte, flags int16, expiry *time.Time) {
	keymd5 := ch.NewMD5Key(key)
	item := &Item{Key: key, Data: value, Flags: flags}
	kvs.store.Set(keymd5, item)

	if expiry != nil {
		exptime := expiry.UTC().Unix()
		kvs.Logger.Debug("KVStore", "SET [%s] %s - Expiry %d", key, value, exptime)
		kvs.expiry[exptime] = append(kvs.expiry[exptime], keymd5)
	} else {
		kvs.Logger.Debug("KVStore", "SET [%s] %s", key, value)
	}
}
Esempio n. 4
0
func (svr *TLSServer) GetKey(key string) ([]byte, int16, bool) {
	keymd5 := ch.NewMD5Key(key)
	nodes := svr.ServerNode.GetNodesFor(keymd5, 3)
	for _, node := range nodes {
		if node.ID == svr.ServerNode.ID {
			svr.Logger.Debug("Server", "GetKey: Peer for key %02X -> %02X (Local)", keymd5, node.ID)
			// Local set.
			return svr.KVStore.Get(key)
		} else {
			svr.Logger.Debug("Server", "GetKey: Peer for key %02X -> %02X (Remote)", keymd5, node.ID)

			peer := svr.Connections[node.ID]
			if peer.State != PeerStateConnected {
				svr.Logger.Warn("Server", "GetKey: Peer for key %02X -> %02X (Remote) Unavailable", keymd5, node.ID)
				continue
			}

			// Remote Set.
			payload := packets.KVStorePacket{
				Command:  packets.CMD_KVSTORE_GET,
				Key:      key,
				KeyHash:  keymd5,
				TargetID: node.ID,
			}
			packet := packets.NewPacket(packets.CMD_KVSTORE, payload)
			reply, err := peer.SendPacketWaitReply(packet, 5*time.Second)

			// Process reply or timeout
			if err == nil {
				switch reply.Command {
				case packets.CMD_KVSTORE_ACK:
					kvpacket := reply.Payload.(packets.KVStorePacket)
					svr.Logger.Debug("Server", "GetKey: Reply from Remote %s = %s", key, kvpacket.Data)
					return kvpacket.Data, kvpacket.Flags, true
				case packets.CMD_KVSTORE_NOT_FOUND:
					svr.Logger.Debug("Server", "GetKey: Reply from Remote %s Not Found", key)
					return []byte{}, 0, false
				default:
					svr.Logger.Warn("Server", "GetKey: Unknown Reply Command %d", reply.Command)
				}
			} else {
				svr.Logger.Warn("Server", "GetKey: Reply Timeout")
			}
		}
	}
	return []byte{}, 0, false
}
Esempio n. 5
0
func (svr *TLSServer) DeleteKey(key string) bool {
	keymd5 := ch.NewMD5Key(key)
	node := svr.ServerNode.GetNodeFor(keymd5)
	if node.ID == svr.ServerNode.ID {
		svr.Logger.Debug("Server", "DeleteKey: Peer for key %02X -> %02X (Local)", keymd5, node.ID)
		// Local set.
		return svr.KVStore.Delete(key)
	} else {
		svr.Logger.Debug("Server", "DeleteKey: Peer for key %02X -> %02X (Remote)", keymd5, node.ID)
		// Remote Set.
		payload := packets.KVStorePacket{
			Command:  packets.CMD_KVSTORE_DELETE,
			Key:      key,
			KeyHash:  keymd5,
			TargetID: node.ID,
		}
		packet := packets.NewPacket(packets.CMD_KVSTORE, payload)
		reply, err := svr.Connections[node.ID].SendPacketWaitReply(packet, 5*time.Second)

		// Process reply or timeout
		if err == nil {
			switch reply.Command {
			case packets.CMD_KVSTORE_ACK:
				svr.Logger.Debug("Server", "DeleteKey: Reply from Remote %s Deleted", key)
				return true
			case packets.CMD_KVSTORE_NOT_FOUND:
				svr.Logger.Debug("Server", "DeleteKey: Reply from Remote %s Not Found", key)
				return false
			default:
				svr.Logger.Warn("Server", "DeleteKey: Unknown Reply Command %d", reply.Command)
			}
		} else {
			svr.Logger.Warn("Server", "DeleteKey: Reply Timeout")
		}
	}
	return false
}
Esempio n. 6
0
func (kvs *KVStore) Delete(key string) bool {
	kvs.Logger.Debug("KVStore", "DELETE [%s]", key)
	return kvs.deleteKey(ch.NewMD5Key(key))
}