diff --git a/src/git.xdrm.io/schastsp/lib/CyclicHash/CyclicHash.go b/src/git.xdrm.io/schastsp/lib/scha/scha.go similarity index 97% rename from src/git.xdrm.io/schastsp/lib/CyclicHash/CyclicHash.go rename to src/git.xdrm.io/schastsp/lib/scha/scha.go index ccf82f8..fbfc99b 100644 --- a/src/git.xdrm.io/schastsp/lib/CyclicHash/CyclicHash.go +++ b/src/git.xdrm.io/schastsp/lib/scha/scha.go @@ -1,4 +1,4 @@ -package CyclicHash +package scha import ( "errors" @@ -6,9 +6,9 @@ import ( "git.xdrm.io/schastsp/lib/xor" ) -/* (0) Constants +/* (0) Static ---------------------------------------------------------*/ -/* (1) Hash size */ +/* (1) Constants */ const HBITSIZE uint = 512; const HSIZE uint = HBITSIZE / 8; diff --git a/src/git.xdrm.io/schastsp/lib/CyclicHash/CyclicHash_test.go b/src/git.xdrm.io/schastsp/lib/scha/scha_test.go similarity index 99% rename from src/git.xdrm.io/schastsp/lib/CyclicHash/CyclicHash_test.go rename to src/git.xdrm.io/schastsp/lib/scha/scha_test.go index 2d81be8..7809b48 100644 --- a/src/git.xdrm.io/schastsp/lib/CyclicHash/CyclicHash_test.go +++ b/src/git.xdrm.io/schastsp/lib/scha/scha_test.go @@ -1,4 +1,4 @@ -package CyclicHash +package scha import "testing" diff --git a/src/git.xdrm.io/schastsp/lib/wtime/wtime.go b/src/git.xdrm.io/schastsp/lib/timeid/timeid.go similarity index 50% rename from src/git.xdrm.io/schastsp/lib/wtime/wtime.go rename to src/git.xdrm.io/schastsp/lib/timeid/timeid.go index cbbf53e..b3f0521 100644 --- a/src/git.xdrm.io/schastsp/lib/wtime/wtime.go +++ b/src/git.xdrm.io/schastsp/lib/timeid/timeid.go @@ -1,4 +1,4 @@ -package wtime +package timeid import ( "math" @@ -6,40 +6,33 @@ import ( ) -/* (0) Definitions ----------------------------------------------------------*/ -/* (1) Time ID struct */ -type TimeID struct { ID uint; Parity uint } - - - /* (1) Generates the current time id * * @wsize Window Size in seconds * +* @return id Current time id +* @return parity Current time parity +* ---------------------------------------------------------*/ -func GenerateID(wsize float64) *TimeID{ - - /* (0) Initialise instance */ - instance := new(TimeID); +func Generate(wsize float64) (uint, uint){ /* (1) If wsize is 0 (div by zero possible error) */ - if wsize == 0 { return instance } + if wsize == 0 { return 0, 0 } /* (2) Get current timestamp */ timestamp := float64( time.Now().Unix() ); /* (3) Calculate the time id */ - instance.ID = uint( timestamp / wsize ); + var id = uint( timestamp / wsize ); /* (4) Calculate parity */ - instance.Parity = instance.ID % 2; + var parity = id % 2; - return instance; + return id, parity; } -/* (2) Try to guess the received ID from current time and received parity +/* (2) Try to guess a previous time id from its parity * * @wsize Window Size in seconds * @parity Received parity @@ -47,12 +40,12 @@ func GenerateID(wsize float64) *TimeID{ * @return id The guessed time id * ---------------------------------------------------------*/ -func GuessID(wsize float64, parity uint) uint{ +func Guess(wsize float64, parity uint) uint{ /* (1) Get current time id */ - var tidNow *TimeID = GenerateID(wsize); + var idNow, parityNow = Generate(wsize); /* (2) Update ID with tidNow parity difference */ - return tidNow.ID - uint(math.Abs( float64(tidNow.Parity) - float64(parity) )); + return idNow - uint(math.Abs( float64(parityNow) - float64(parity) )); } \ No newline at end of file diff --git a/src/git.xdrm.io/schastsp/lib/timeid/timeid_test.go b/src/git.xdrm.io/schastsp/lib/timeid/timeid_test.go new file mode 100644 index 0000000..705a82e --- /dev/null +++ b/src/git.xdrm.io/schastsp/lib/timeid/timeid_test.go @@ -0,0 +1,23 @@ +package timeid + +import ( + "time" + "testing" +) + + +func TestGuessing(t *testing.T){ + + var windowSize float64 = .5; + + id, parity := Generate(windowSize); + + time.Sleep( time.Duration(windowSize) * time.Second); + + var guessedId uint = Guess(windowSize, parity); + + if id != guessedId { + t.Errorf("Wrong guessed id, expected '%d' ; got '%d'", id, guessedId); + } + +} \ No newline at end of file diff --git a/src/git.xdrm.io/schastsp/lib/wtime/wtime_test.go b/src/git.xdrm.io/schastsp/lib/wtime/wtime_test.go deleted file mode 100644 index d7f9365..0000000 --- a/src/git.xdrm.io/schastsp/lib/wtime/wtime_test.go +++ /dev/null @@ -1,23 +0,0 @@ -package wtime - -import ( - "time" - "testing" -) - - -func TestGuessing(t *testing.T){ - - var windowSize float64 = .5; - - var tid *TimeID = GenerateID(windowSize); - - time.Sleep( time.Duration(windowSize) * time.Second); - - var guessedId uint = GuessID(windowSize, tid.Parity); - - if tid.ID != guessedId { - t.Errorf("Wrong guessed id, expected '%d' ; got '%d'", tid.ID, guessedId); - } - -} \ No newline at end of file