[server:server] created struct 'T' + constructor 'New' + signature for 'HandleRequest(req, res)' [server:server.internal] created 'fetch()' and 'store()'

This commit is contained in:
xdrm-brackets 2018-04-22 21:46:31 +02:00
parent 3d7a8ae034
commit 3c9964b0cb
2 changed files with 142 additions and 0 deletions

View File

@ -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<context.T> Shared context
* @savePath<string> 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<io.Reader> Request reader
* @res<io.Writer> Response Writer
*
* @return err<error> Error or NIL if not
*
---------------------------------------------------------*/
func (s *T) HandleRequest(req io.Reader, res io.Writer) error { return nil }

View File

@ -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> 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> 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
}