Commit 26c5a939 authored by Zhu Yong's avatar Zhu Yong
Browse files

remove old implementation

parent 2cdf06b2
Loading
Loading
Loading
Loading

LICENSE/mit.md

deleted100644 → 0
+0 −26
Original line number Diff line number Diff line
The MIT License (MIT)
=====================

Copyright © `2015` `Seagate Technology`

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the “Software”), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

LICENSE/mit.txt

deleted100644 → 0
+0 −21
Original line number Diff line number Diff line
The MIT License (MIT)

Copyright (c) 2015 Seagate Technology

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

README.md

deleted100644 → 0
+0 −3
Original line number Diff line number Diff line
# Kinetic in Go

Soon...
 No newline at end of file

kinetic/client.go

deleted100644 → 0
+0 −242
Original line number Diff line number Diff line
// The MIT License (MIT)
//
// Copyright (c) 2015 Seagate Technology
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// @author = ["Ignacio Corderi"]

package kinetic

import (
	"github.com/golang/protobuf/proto"	
	"crypto/hmac"
	"crypto/sha1"
	"encoding/binary"
	"net"
	"fmt"
	
	"github.com/seagate/kinetic-go/kinetic/network"
	kproto "github.com/seagate/kinetic-go/kinetic/proto"
)

// Default credentials
var (
	USER_ID = int64(1)
    CLIENT_SECRET = []byte("asdfasdf")
)

func calculate_hmac(secret []byte, bytes []byte) []byte {
	mac := hmac.New(sha1.New, secret)
    
	ln := make([]byte, 4)
    binary.BigEndian.PutUint32(ln, uint32(len(bytes)))
	
    mac.Write(ln)
    mac.Write(bytes)

	return mac.Sum(nil)
} 

type RemoteError struct {
	status kproto.Command_Status	
}

func (e RemoteError) Error() string {
	return fmt.Sprintf("%v: %v", e.status.Code, *e.status.StatusMessage)
}

type Client interface {
	Put (key []byte, value []byte) ((<-chan error), error)
	PutFrom (key []byte, length int, queue <-chan []byte) ((<-chan error), error)
	
	Close()
}

type PendingOperation struct {
	sequence int64
	receiver chan error
}

type NetworkClient struct {
	connectionId int64 
	userId       int64
	secret       []byte
	sequence     int64
	conn         net.Conn
	closed       bool
	error        error	
	notifier     chan<- PendingOperation 
}

func Connect(target string) (Client, error) {
	conn, err := net.Dial("tcp", target)
	if err != nil { return nil, err }
	// hanshake
	_, cmd, _, err := network.Receive(conn)
	if err != nil { return nil, err }
	
	ch := make(chan PendingOperation)
	
	c := &NetworkClient { connectionId: *cmd.Header.ConnectionID,
		                 userId: USER_ID,
						 secret: CLIENT_SECRET,
						 sequence: 1,
						 conn: conn, 
						 closed: false,
						 error: nil,
						 notifier: ch }
				
	go c.listen(ch)						 
	return c, nil						 
} 

func (self *NetworkClient) listen(notifications <-chan PendingOperation) {
	pending := make(map[int64]PendingOperation) // pendings
	for {
		_, cmd, _, err := network.Receive(self.conn)
		if err != nil { 
			if !self.closed { 
				self.error = err
				// TODO: try closing socket
			}
			break
		}
		
		var response error
		if *cmd.Status.Code != kproto.Command_Status_SUCCESS {
			response = RemoteError { status: *cmd.Status }
		}
		
		// Notify caller
		// Seems more complicated than it should, but we are optimizing 
		// for when we receive in order
		for {
			op := <-notifications
			// Chances are, it's in order
			if op.sequence == *cmd.Header.AckSequence {
				op.receiver <- response // TODO: send back the actual response
				break
			} else {				
				// Either we missed it or it hasnt arrived yet.
				pending[op.sequence] = op				 
				op, ok := pending[*cmd.Header.AckSequence]
				if ok { // this is the case where we missed it
					op.receiver <- response
					delete(pending, op.sequence)
					break
				}
			}			
		}		
	}
	
	// Notify all pendings that we are closed for business
}

// Client implementation

func(self *NetworkClient) Put(key []byte, value []byte) ((<-chan error), error) {	
	cmd := &kproto.Command {
			Header: &kproto.Command_Header {
				ConnectionID: proto.Int64(self.connectionId),
				Sequence: proto.Int64(self.sequence),
				MessageType: kproto.Command_PUT.Enum(),
			},
			Body: &kproto.Command_Body {
				KeyValue: &kproto.Command_KeyValue {
					Key: key,
					Algorithm: kproto.Command_SHA1.Enum(),
					Tag: make([]byte, 0),
					Synchronization: kproto.Command_WRITEBACK.Enum(),
				},
			},
		}
		
	cmd_bytes, err := proto.Marshal(cmd)		
	if err != nil { return nil, err }
	
	msg := &kproto.Message {
			AuthType: kproto.Message_HMACAUTH.Enum(),
			HmacAuth: &kproto.Message_HMACauth {
				Identity: proto.Int64(self.userId),
				Hmac: calculate_hmac(self.secret, cmd_bytes),
			},
			CommandBytes: cmd_bytes,
		}
	
	err = network.Send(self.conn, msg, value)	
	if err != nil { return nil, err }
			
	rx := make(chan error, 1)		
	pending := PendingOperation { sequence: self.sequence, receiver: rx }		
			
	self.notifier <- pending			
			
	self.sequence += 1
	
	return rx, nil
}

// TOOD: needs refactor
func(self *NetworkClient) PutFrom(key []byte, length int, queue <-chan []byte) ((<-chan error), error) {	
	cmd := &kproto.Command {
			Header: &kproto.Command_Header {
				ConnectionID: proto.Int64(self.connectionId),
				Sequence: proto.Int64(self.sequence),
				MessageType: kproto.Command_PUT.Enum(),
			},
			Body: &kproto.Command_Body {
				KeyValue: &kproto.Command_KeyValue {
					Key: key,
					Algorithm: kproto.Command_SHA1.Enum(),
					Tag: make([]byte, 0),
					Synchronization: kproto.Command_WRITEBACK.Enum(),
				},
			},
		}
		
	cmd_bytes, err := proto.Marshal(cmd)		
	if err != nil { return nil, err }
	
	msg := &kproto.Message {
			AuthType: kproto.Message_HMACAUTH.Enum(),
			HmacAuth: &kproto.Message_HMACauth {
				Identity: proto.Int64(self.userId),
				Hmac: calculate_hmac(self.secret, cmd_bytes),
			},
			CommandBytes: cmd_bytes,
		}
	
	err = network.SendFrom(self.conn, msg, length, queue)	
	if err != nil { return nil, err }
			
	rx := make(chan error, 1)		
	pending := PendingOperation { sequence: self.sequence, receiver: rx }		
			
	self.notifier <- pending			
			
	self.sequence += 1
	
	return rx, nil
}

func(self *NetworkClient) Close() {
	self.closed = true
	self.conn.Close()
}
 No newline at end of file

kinetic/network/net.go

deleted100644 → 0
+0 −123
Original line number Diff line number Diff line
// The MIT License (MIT)
//
// Copyright (c) 2015 Seagate Technology
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// @author = ["Ignacio Corderi"]

package network

import (
	"github.com/golang/protobuf/proto"	
	"encoding/binary"
	"net"
	"io"
	"errors"
	
	kproto "github.com/seagate/kinetic-go/kinetic/proto"
)

// Refactor
func SendFrom(conn net.Conn, msg *kproto.Message, length int, queue <-chan []byte) error {
	msg_bytes, err := proto.Marshal(msg)
	if err != nil { return err }
	
	header := make([]byte, 9)
	header[0] = 70 // magic
	binary.BigEndian.PutUint32(header[1:5], uint32(len(msg_bytes)))
	binary.BigEndian.PutUint32(header[5:9], uint32(length))
	
	conn.Write(header)
	conn.Write(msg_bytes)
	sent := 0
	for {
		chunk := <-queue
		ln := len(chunk)
		if ln + sent > length {
			// TODO: should shut down socket to cancel operation.
			return errors.New("Tried to send more bytes than promised.")
		}
		if chunk == nil { break }
		conn.Write(chunk)
		sent += ln
	}
	
	if sent < length {
		// TODO: should shut down socket to cancel operation.
		return errors.New("Received less bytes than promised.")
	}
	
	return nil
}

func Send(conn net.Conn, msg *kproto.Message, value []byte) error {
	msg_bytes, err := proto.Marshal(msg)
	if err != nil { return err }
	
	header := make([]byte, 9)
	header[0] = 70 // magic
	binary.BigEndian.PutUint32(header[1:5], uint32(len(msg_bytes)))
	binary.BigEndian.PutUint32(header[5:9], uint32(len(value)))
	
	conn.Write(header)
	conn.Write(msg_bytes)
	conn.Write(value)
	
	return nil
}

func Receive(conn net.Conn) (*kproto.Message, *kproto.Command, []byte, error) {
	header := make([]byte, 9)
	
	_, err := io.ReadFull(conn, header[0:])
	if err != nil { return nil, nil, nil, err }
	
	magic := header[0]
	if magic != 70 { panic("Invalid magic number!") }
	
    proto_ln := int32(binary.BigEndian.Uint32(header[1:5]))
    value_ln := int32(binary.BigEndian.Uint32(header[5:9]))
	
	proto_bytes := make([]byte, proto_ln)
	
	_, err = io.ReadFull(conn, proto_bytes)
	if err != nil { return nil, nil, nil, err }
	
	msg := &kproto.Message{}
	err = proto.Unmarshal(proto_bytes, msg)
	if err != nil { return nil, nil, nil, err }
	
	// TODO: check hmac
	
	cmd := &kproto.Command{}
	err = proto.Unmarshal(msg.CommandBytes, cmd)
	if err != nil { return nil, nil, nil, err }
	
	if value_ln > 0 {
    	value := make([]byte, value_ln)
		
		_, err = io.ReadFull(conn, value)
		if err != nil { return nil, nil, nil, err }
		
		return msg, cmd, value, nil
	} else {
		return msg, cmd, nil, nil
	}
}
 No newline at end of file
Loading