2018-04-24 19:26:13 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
"bytes"
|
|
|
|
"os"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"git.xdrm.io/schastsp/context"
|
2018-04-25 06:26:54 +00:00
|
|
|
"git.xdrm.io/schastsp/client"
|
|
|
|
"git.xdrm.io/schastsp/server"
|
2018-04-24 19:26:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
/* Store target config paths */
|
|
|
|
var clientConfigPath = "/tmp/cyclichash/client/";
|
|
|
|
var serverConfigPath = "/tmp/cyclichash/server/lastkey";
|
|
|
|
|
|
|
|
|
|
|
|
func main(){
|
|
|
|
|
|
|
|
executionStart := time.Now().UnixNano()
|
|
|
|
|
|
|
|
|
|
|
|
/* (1) Create context
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Create it */
|
|
|
|
ctx, err := context.Create(2, 0x0f0, 0x0a, 0xfff);
|
|
|
|
if err != nil { fmt.Printf("[ERROR:context] %s\n", err); return }
|
|
|
|
|
|
|
|
|
|
|
|
/* (2) Manage flags (cli arguments)
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Define 'sync' mode */
|
|
|
|
var sync bool;
|
|
|
|
flag.BoolVar(&sync, "sync", false, "If set, a new synchronisation key will be outputed\nNote: it will break existing systems")
|
|
|
|
|
|
|
|
/* (2) Define connection 'latency' */
|
|
|
|
var latency int64;
|
|
|
|
flag.Int64Var(&latency, "t", 0, "Connection latency in ms")
|
|
|
|
|
|
|
|
/* (3) Parse cli arguments */
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
/* (4) Dispatch to functions */
|
|
|
|
if sync {
|
|
|
|
synchronise(ctx)
|
|
|
|
} else {
|
|
|
|
testCommunication(ctx, latency)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
executionStop := time.Now().UnixNano()
|
|
|
|
nsElapsed := float64(executionStop - executionStart)
|
|
|
|
usElapsed := float64(nsElapsed / 1e3)
|
|
|
|
msElapsed := float64(usElapsed / 1e3)
|
|
|
|
sElapsed := float64(msElapsed / 1e3)
|
|
|
|
|
|
|
|
if sElapsed >= 1 { fmt.Printf("executed in %.3f s\n", sElapsed)
|
|
|
|
} else if msElapsed >= 1 { fmt.Printf("executed in %.3f ms\n", msElapsed)
|
|
|
|
} else if usElapsed >= 1 { fmt.Printf("executed in %.3f us\n", usElapsed)
|
|
|
|
} else if nsElapsed >= 1 { fmt.Printf("executed in %.3f ns\n", nsElapsed) }
|
|
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func synchronise(ctx *context.T){
|
|
|
|
|
|
|
|
/* (1) Create client */
|
|
|
|
client, err := client.New(ctx, clientConfigPath);
|
|
|
|
if err != nil { fmt.Printf("[ERROR:client] %s\n", err); return }
|
|
|
|
|
|
|
|
/* (2) Get synchronisation key */
|
|
|
|
syncKey, err := client.SynchronisationKey()
|
|
|
|
if err != nil { fmt.Printf("[ERROR:syncKey] %s\n", err); return }
|
|
|
|
|
|
|
|
/* (3) Store synchronisation key */
|
|
|
|
conf, err := os.OpenFile(serverConfigPath, os.O_RDWR|os.O_CREATE, 0775)
|
|
|
|
if err != nil { fmt.Printf("[ERROR:syncKey:storage] %s\n", err); return }
|
|
|
|
|
|
|
|
defer conf.Close()
|
|
|
|
|
|
|
|
conf.Write(syncKey)
|
|
|
|
|
|
|
|
fmt.Printf("Synchronisation Key stored\n");
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func testCommunication(ctx *context.T, latency int64){
|
|
|
|
|
|
|
|
/* (1) Setup
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Create client */
|
|
|
|
client, err := client.New(ctx, clientConfigPath);
|
|
|
|
if err != nil { fmt.Printf("[ERROR:client] %s\n", err); return }
|
|
|
|
|
|
|
|
/* (2) Create server */
|
|
|
|
server, err := server.New(ctx, serverConfigPath)
|
|
|
|
if err != nil { fmt.Printf("[ERROR:server] %s\n", err); return }
|
|
|
|
|
|
|
|
/* (3) Create emulation buffers */
|
|
|
|
requestSocket, responseSocket := new(bytes.Buffer), new(bytes.Buffer)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (2) REAL STUFF HAPPENS HERE
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Request | client send */
|
|
|
|
err = client.Send(requestSocket)
|
|
|
|
if err != nil { fmt.Printf("[ERROR:request:send] %s\n", err); return }
|
|
|
|
|
|
|
|
/* (2) Emulate latency */
|
|
|
|
time.Sleep( time.Duration(latency) * time.Second / 1e3)
|
|
|
|
|
|
|
|
/* (3) Request | server receive */
|
|
|
|
/* Response | server send */
|
|
|
|
err = server.HandleRequest(requestSocket, responseSocket)
|
|
|
|
if err != nil { fmt.Printf("[ERROR:request:receive] %s\n", err); return }
|
|
|
|
|
|
|
|
/* (4) Response | client receive */
|
|
|
|
err = client.Receive(responseSocket)
|
|
|
|
if err != nil { fmt.Printf("[ERROR:response:receive] %s\n", err); return }
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|