This commit is contained in:
Jakob Borg 2014-02-24 13:29:30 +01:00
parent e7bf3ac108
commit 434a0ccf2a
9 changed files with 35 additions and 37 deletions

View File

@ -34,7 +34,7 @@ type Discoverer struct {
}
var (
ErrIncorrectMagic = errors.New("Incorrect magic number")
ErrIncorrectMagic = errors.New("incorrect magic number")
)
// We tolerate a certain amount of errors because we might be running on

View File

@ -24,7 +24,7 @@ import (
)
var cfg Configuration
var Version string = "unknown-dev"
var Version = "unknown-dev"
var (
myID string
@ -83,7 +83,7 @@ func main() {
fatalErr(err)
}
myID = string(certId(cert.Certificate[0]))
myID = string(certID(cert.Certificate[0]))
log.SetPrefix("[" + myID[0:5] + "] ")
logger.SetPrefix("[" + myID[0:5] + "] ")
@ -388,7 +388,7 @@ listen:
continue
}
remoteID := certId(tc.ConnectionState().PeerCertificates[0].Raw)
remoteID := certID(tc.ConnectionState().PeerCertificates[0].Raw)
if remoteID == myID {
warnf("Connect from myself (%s) - should not happen", remoteID)
@ -469,7 +469,7 @@ func connect(myID string, disc *discover.Discoverer, m *Model, tlsCfg *tls.Confi
continue
}
remoteID := certId(conn.ConnectionState().PeerCertificates[0].Raw)
remoteID := certID(conn.ConnectionState().PeerCertificates[0].Raw)
if remoteID != nodeCfg.NodeID {
warnln("Unexpected nodeID", remoteID, "!=", nodeCfg.NodeID)
conn.Close()

View File

@ -46,7 +46,6 @@ func (e *ErrPipe) Write(data []byte) (int, error) {
e.PipeWriter.CloseWithError(e.err)
e.closed = true
return n, e.err
} else {
return e.PipeWriter.Write(data)
}
return e.PipeWriter.Write(data)
}

View File

@ -31,7 +31,8 @@ const (
)
var (
ErrClusterHash = fmt.Errorf("Configuration error: mismatched cluster hash")
ErrClusterHash = fmt.Errorf("configuration error: mismatched cluster hash")
ErrClosed = errors.New("connection closed")
)
type Model interface {
@ -56,7 +57,7 @@ type Connection struct {
xw *xdr.Writer
closed bool
awaiting map[int]chan asyncResult
nextId int
nextID int
indexSent map[string]map[string][2]int64
peerOptions map[string]string
myOptions map[string]string
@ -68,8 +69,6 @@ type Connection struct {
statisticsLock sync.Mutex
}
var ErrClosed = errors.New("Connection closed")
type asyncResult struct {
val []byte
err error
@ -105,7 +104,7 @@ func NewConnection(nodeID string, reader io.Reader, writer io.Writer, receiver M
c.myOptions = options
go func() {
c.Lock()
header{0, c.nextId, messageTypeOptions}.encodeXDR(c.xw)
header{0, c.nextID, messageTypeOptions}.encodeXDR(c.xw)
var om OptionsMessage
for k, v := range options {
om.Options = append(om.Options, Option{k, v})
@ -118,7 +117,7 @@ func NewConnection(nodeID string, reader io.Reader, writer io.Writer, receiver M
if err != nil {
log.Println("Warning: Write error during initial handshake:", err)
}
c.nextId++
c.nextID++
c.Unlock()
}()
}
@ -155,12 +154,12 @@ func (c *Connection) Index(repo string, idx []FileInfo) {
idx = diff
}
header{0, c.nextId, msgType}.encodeXDR(c.xw)
header{0, c.nextID, msgType}.encodeXDR(c.xw)
_, err := IndexMessage{repo, idx}.encodeXDR(c.xw)
if err == nil {
err = c.flush()
}
c.nextId = (c.nextId + 1) & 0xfff
c.nextID = (c.nextID + 1) & 0xfff
c.hasSentIndex = true
c.Unlock()
@ -178,8 +177,8 @@ func (c *Connection) Request(repo string, name string, offset int64, size int) (
return nil, ErrClosed
}
rc := make(chan asyncResult)
c.awaiting[c.nextId] = rc
header{0, c.nextId, messageTypeRequest}.encodeXDR(c.xw)
c.awaiting[c.nextID] = rc
header{0, c.nextID, messageTypeRequest}.encodeXDR(c.xw)
_, err := RequestMessage{repo, name, uint64(offset), uint32(size)}.encodeXDR(c.xw)
if err == nil {
err = c.flush()
@ -189,7 +188,7 @@ func (c *Connection) Request(repo string, name string, offset int64, size int) (
c.close(err)
return nil, err
}
c.nextId = (c.nextId + 1) & 0xfff
c.nextID = (c.nextID + 1) & 0xfff
c.Unlock()
res, ok := <-rc
@ -206,8 +205,8 @@ func (c *Connection) ping() bool {
return false
}
rc := make(chan asyncResult, 1)
c.awaiting[c.nextId] = rc
header{0, c.nextId, messageTypePing}.encodeXDR(c.xw)
c.awaiting[c.nextID] = rc
header{0, c.nextID, messageTypePing}.encodeXDR(c.xw)
err := c.flush()
if err != nil {
c.Unlock()
@ -218,7 +217,7 @@ func (c *Connection) ping() bool {
c.close(c.xw.Error())
return false
}
c.nextId = (c.nextId + 1) & 0xfff
c.nextID = (c.nextID + 1) & 0xfff
c.Unlock()
res, ok := <-rc
@ -268,7 +267,7 @@ loop:
break loop
}
if hdr.version != 0 {
c.close(fmt.Errorf("Protocol error: %s: unknown message version %#x", c.id, hdr.version))
c.close(fmt.Errorf("protocol error: %s: unknown message version %#x", c.id, hdr.version))
break loop
}
@ -371,7 +370,7 @@ loop:
}
default:
c.close(fmt.Errorf("Protocol error: %s: unknown message type %#x", c.id, hdr.msgType))
c.close(fmt.Errorf("protocol error: %s: unknown message type %#x", c.id, hdr.msgType))
break loop
}
}
@ -410,10 +409,10 @@ func (c *Connection) pingerLoop() {
select {
case ok := <-rc:
if !ok {
c.close(fmt.Errorf("Ping failure"))
c.close(fmt.Errorf("ping failure"))
}
case <-time.After(pingTimeout):
c.close(fmt.Errorf("Ping timeout"))
c.close(fmt.Errorf("ping timeout"))
}
}
}

View File

@ -38,7 +38,7 @@ func TestPing(t *testing.T) {
}
func TestPingErr(t *testing.T) {
e := errors.New("Something broke")
e := errors.New("something broke")
for i := 0; i < 12; i++ {
for j := 0; j < 12; j++ {
@ -64,7 +64,7 @@ func TestPingErr(t *testing.T) {
}
func TestRequestResponseErr(t *testing.T) {
e := errors.New("Something broke")
e := errors.New("something broke")
var pass bool
for i := 0; i < 48; i++ {

View File

@ -6,7 +6,7 @@ import (
)
const (
MAX_CHANGE_HISTORY = 4
MaxChangeHistory = 4
)
type change struct {
@ -45,8 +45,8 @@ func (h changeHistory) bandwidth(t time.Time) int64 {
func (h *changeHistory) append(size int64, t time.Time) {
c := change{size, t}
if len(h.changes) == MAX_CHANGE_HISTORY {
h.changes = h.changes[1:MAX_CHANGE_HISTORY]
if len(h.changes) == MaxChangeHistory {
h.changes = h.changes[1:MaxChangeHistory]
}
h.changes = append(h.changes, c)
}

View File

@ -84,29 +84,29 @@ func TestHistory(t *testing.T) {
t.Errorf("Incorrect first record size %d", s)
}
for i := 1; i < MAX_CHANGE_HISTORY; i++ {
for i := 1; i < MaxChangeHistory; i++ {
h.append(int64(40+i), t0.Add(time.Duration(i)*time.Second))
}
if l := len(h.changes); l != MAX_CHANGE_HISTORY {
if l := len(h.changes); l != MaxChangeHistory {
t.Errorf("Incorrect history length %d", l)
}
if s := h.changes[0].size; s != 40 {
t.Errorf("Incorrect first record size %d", s)
}
if s := h.changes[MAX_CHANGE_HISTORY-1].size; s != 40+MAX_CHANGE_HISTORY-1 {
if s := h.changes[MaxChangeHistory-1].size; s != 40+MaxChangeHistory-1 {
t.Errorf("Incorrect last record size %d", s)
}
h.append(999, t0.Add(time.Duration(999)*time.Second))
if l := len(h.changes); l != MAX_CHANGE_HISTORY {
if l := len(h.changes); l != MaxChangeHistory {
t.Errorf("Incorrect history length %d", l)
}
if s := h.changes[0].size; s != 41 {
t.Errorf("Incorrect first record size %d", s)
}
if s := h.changes[MAX_CHANGE_HISTORY-1].size; s != 999 {
if s := h.changes[MaxChangeHistory-1].size; s != 999 {
t.Errorf("Incorrect last record size %d", s)
}

2
tls.go
View File

@ -25,7 +25,7 @@ func loadCert(dir string) (tls.Certificate, error) {
return tls.LoadX509KeyPair(path.Join(dir, "cert.pem"), path.Join(dir, "key.pem"))
}
func certId(bs []byte) string {
func certID(bs []byte) string {
hf := sha256.New()
hf.Write(bs)
id := hf.Sum(nil)

View File

@ -5,7 +5,7 @@ import (
"io"
)
var ErrElementSizeExceeded = errors.New("Element size exceeded")
var ErrElementSizeExceeded = errors.New("element size exceeded")
type Reader struct {
r io.Reader