Esempio n. 1
0
func (c Client) handshake() error {
	decodedCmd, err := msg.DecodeCommand(c.conn)
	if err != nil {
		if err == io.EOF {
			log.Println("Connection closed before handshake could be completed")
			c.conn.Close()
		}

		return err
	}

	switch decodedCmd.SubCommand.(type) {
	case msg.HandshakeCommand:
		res := msg.NewResult(decodedCmd.Id, msg.StatusSuccess, msg.NewHandshakeResult(c.id))

		if err := res.Encode(c.conn); err != nil {
			return errors.New("Failed to send handshake response to client")
		}

		log.Printf("Handshake successful clientId=%s", c.id)

		return nil
	default:
		return errors.New("First message from client must be handshake")
	}
}
Esempio n. 2
0
func (s *Store) handleSetCommand(cmd *msg.Command, sb msg.SetCommand, w io.Writer) {
	s.objects[sb.Key] = object{sb.Key, sb.Value, sb.Expiry}

	res := msg.NewResult(
		cmd.Id,
		msg.StatusSuccess,
		msg.NewSetResult(),
	)

	res.Encode(w)
}
Esempio n. 3
0
func (s *Store) handleGetCommand(cmd *msg.Command, sb msg.GetCommand, w io.Writer) {
	obj := s.objects[sb.Key]

	res := msg.NewResult(
		cmd.Id,
		msg.StatusSuccess,
		msg.NewGetResult(obj.key, obj.value, obj.expiry),
	)

	res.Encode(w)
}