schastsp/cmd/simple/simple.go

154 lines
3.5 KiB
Go
Raw Permalink Normal View History

2018-04-24 19:26:13 +00:00
package main
import (
"bytes"
"flag"
"fmt"
"git.xdrm.io/logauth/schastsp/client"
2018-09-06 14:41:03 +00:00
"git.xdrm.io/logauth/schastsp/context"
"git.xdrm.io/logauth/schastsp/server"
2018-09-06 14:41:03 +00:00
"os"
"time"
2018-04-24 19:26:13 +00:00
)
/* Store target config paths */
2018-09-06 14:41:03 +00:00
var clientConfigPath = "/tmp/cyclichash/client/"
var serverConfigPath = "/tmp/cyclichash/server/lastkey"
2018-04-24 19:26:13 +00:00
2018-09-06 14:41:03 +00:00
func main() {
2018-04-24 19:26:13 +00:00
executionStart := time.Now().UnixNano()
/* (1) Create context
---------------------------------------------------------*/
/* (1) Create it */
2018-09-06 14:41:03 +00:00
ctx, err := context.Create(2, 0x0f0, 0x0a, 0xfff)
if err != nil {
fmt.Printf("[ERROR:context] %s\n", err)
return
}
2018-04-24 19:26:13 +00:00
/* (2) Manage flags (cli arguments)
---------------------------------------------------------*/
/* (1) Define 'sync' mode */
2018-09-06 14:41:03 +00:00
var sync bool
2018-04-24 19:26:13 +00:00
flag.BoolVar(&sync, "sync", false, "If set, a new synchronisation key will be outputed\nNote: it will break existing systems")
/* (2) Define connection 'latency' */
2018-09-06 14:41:03 +00:00
var latency int64
2018-04-24 19:26:13 +00:00
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()
2018-09-06 14:41:03 +00:00
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)
}
2018-04-24 19:26:13 +00:00
return
}
2018-09-06 14:41:03 +00:00
func synchronise(ctx *context.T) {
2018-04-24 19:26:13 +00:00
/* (1) Create client */
2018-09-06 14:41:03 +00:00
client, err := client.New(ctx, clientConfigPath)
if err != nil {
fmt.Printf("[ERROR:client] %s\n", err)
return
}
2018-04-24 19:26:13 +00:00
/* (2) Get synchronisation key */
syncKey, err := client.SynchronisationKey()
2018-09-06 14:41:03 +00:00
if err != nil {
fmt.Printf("[ERROR:syncKey] %s\n", err)
return
}
2018-04-24 19:26:13 +00:00
/* (3) Store synchronisation key */
conf, err := os.OpenFile(serverConfigPath, os.O_RDWR|os.O_CREATE, 0775)
2018-09-06 14:41:03 +00:00
if err != nil {
fmt.Printf("[ERROR:syncKey:storage] %s\n", err)
return
}
2018-04-24 19:26:13 +00:00
defer conf.Close()
conf.Write(syncKey)
2018-09-06 14:41:03 +00:00
fmt.Printf("Synchronisation Key stored\n")
2018-04-24 19:26:13 +00:00
2018-09-06 14:41:03 +00:00
return
2018-04-24 19:26:13 +00:00
}
2018-09-06 14:41:03 +00:00
func testCommunication(ctx *context.T, latency int64) {
2018-04-24 19:26:13 +00:00
/* (1) Setup
---------------------------------------------------------*/
/* (1) Create client */
2018-09-06 14:41:03 +00:00
client, err := client.New(ctx, clientConfigPath)
if err != nil {
fmt.Printf("[ERROR:client] %s\n", err)
return
}
2018-04-24 19:26:13 +00:00
/* (2) Create server */
server, err := server.New(ctx, serverConfigPath)
2018-09-06 14:41:03 +00:00
if err != nil {
fmt.Printf("[ERROR:server] %s\n", err)
return
}
2018-04-24 19:26:13 +00:00
/* (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)
2018-09-06 14:41:03 +00:00
if err != nil {
fmt.Printf("[ERROR:request:send] %s\n", err)
return
}
2018-04-24 19:26:13 +00:00
/* (2) Emulate latency */
2018-09-06 14:41:03 +00:00
time.Sleep(time.Duration(latency) * time.Second / 1e3)
2018-04-24 19:26:13 +00:00
/* (3) Request | server receive */
/* Response | server send */
err = server.HandleRequest(requestSocket, responseSocket)
2018-09-06 14:41:03 +00:00
if err != nil {
fmt.Printf("[ERROR:request:receive] %s\n", err)
return
}
2018-04-24 19:26:13 +00:00
/* (4) Response | client receive */
err = client.Receive(responseSocket)
2018-09-06 14:41:03 +00:00
if err != nil {
fmt.Printf("[ERROR:response:receive] %s\n", err)
return
}
2018-04-24 19:26:13 +00:00
2018-09-06 14:41:03 +00:00
return
2018-04-24 19:26:13 +00:00
2018-09-06 14:41:03 +00:00
}