From 3c9964b0cb42da9a1b3646853d2be3d8912e40f1 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sun, 22 Apr 2018 21:46:31 +0200 Subject: [PATCH] [server:server] created struct 'T' + constructor 'New' + signature for 'HandleRequest(req, res)' [server:server.internal] created 'fetch()' and 'store()' --- src/git.xdrm.io/schastsp/server/server.go | 77 +++++++++++++++++++ .../schastsp/server/server.internal.go | 65 ++++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 src/git.xdrm.io/schastsp/server/server.go create mode 100644 src/git.xdrm.io/schastsp/server/server.internal.go diff --git a/src/git.xdrm.io/schastsp/server/server.go b/src/git.xdrm.io/schastsp/server/server.go new file mode 100644 index 0000000..6d8f16c --- /dev/null +++ b/src/git.xdrm.io/schastsp/server/server.go @@ -0,0 +1,77 @@ +package server + +import ( + "io" + "os" + "path/filepath" + "errors" + "git.xdrm.io/schastsp/context" +) + +const DEBUG = true + +/* (1) Structure +---------------------------------------------------------*/ +type T struct { + ctx *context.T // shared context + hash []byte // current key + conf string // configuration file path +} + + +/* (2) Constructor +* +* @ctx Shared context +* @savePath Configuration file path +* +---------------------------------------------------------*/ +func New(ctx *context.T, savePath string) (*T, error) { + + inst := new(T) + + /* (1) Store context */ + if ctx == nil { return nil, errors.New("Context must not be nil"); } + inst.ctx = ctx; + + /* (2) Get absolute file path */ + absolutePath, err := filepath.Abs(savePath); + if err != nil { return nil, err; } + + /* (3) Check file */ + info, err := os.Stat(absolutePath); + if err != nil { + + // Unknown error + if !os.IsNotExist(err) { return nil, err } + + // File does not exist -> try to create file + _, err2 := os.Create(absolutePath); + if err2 != nil { return nil, err2 } + + // fail if exists but not regular file + } else if !info.Mode().IsRegular() { + return nil, errors.New("Configuration file is not a regular file"); + } + + /* (4) Store file path */ + inst.conf = absolutePath; + + /* (5) Try to fetch hash from conf */ + err = inst.fetch(); + if err != nil { return nil, err } + + return inst, nil; + +} + + + +/* (3) Handle request and send response +* +* @req Request reader +* @res Response Writer +* +* @return err Error or NIL if not +* +---------------------------------------------------------*/ +func (s *T) HandleRequest(req io.Reader, res io.Writer) error { return nil } \ No newline at end of file diff --git a/src/git.xdrm.io/schastsp/server/server.internal.go b/src/git.xdrm.io/schastsp/server/server.internal.go new file mode 100644 index 0000000..634779a --- /dev/null +++ b/src/git.xdrm.io/schastsp/server/server.internal.go @@ -0,0 +1,65 @@ +package server + +import ( + "git.xdrm.io/schastsp/lib/scha" + "encoding/binary" + "os" + "errors" +) + +/* (1) Store hash into file +* +* @return err Error or NIL if not +* +---------------------------------------------------------*/ +func (s T) store() error { + + /* (1) Try to open file for writing */ + file, err := os.OpenFile(s.conf, os.O_RDWR|os.O_CREATE, 0755) + if err != nil { return err } + + /* (2) Defer close */ + defer file.Close(); + + /* (3) Write hash into file */ + err = binary.Write(file, binary.BigEndian, s.hash) + if err != nil { return err } + + return nil + +} + + + + + +/* (2) Fetch hash from file +* +* @return err Error or NIL if not +* +---------------------------------------------------------*/ +func (s *T) fetch() error { + + /* (1) Try to open file for reading */ + file, err := os.Open(s.conf) + if err != nil { return err } + + /* (2) Defer close */ + defer file.Close(); + + /* (3) Try to fetch hash from file */ + fetchedHash := make([]byte, scha.HSIZE) + err = binary.Read(file, binary.BigEndian, s.hash) + if err != nil { return err } + + /* (4) Fail if wrong size */ + if uint16(len(fetchedHash)) != scha.HSIZE { + return errors.New("Invalid hash size in file") + } + + /* (5) Copy fetched hash into real one */ + s.hash = fetchedHash + + return nil + +} \ No newline at end of file