[client:client] Created client initialisation with Update() that stores/fetches data using {client:config} system

This commit is contained in:
xdrm-brackets 2018-04-21 19:48:18 +02:00
parent 775eb33435
commit 9785e8e573
1 changed files with 146 additions and 0 deletions

View File

@ -0,0 +1,146 @@
package client;
import (
"git.xdrm.io/schastsp/context"
"git.xdrm.io/schastsp/client/keyset"
)
/* (1) Structure
---------------------------------------------------------*/
type T struct {
ctx *context.T // shared context
key *keyset.T; // current key
sync *keyset.T; // next bufferised key
consumed bool; // true if 'key' must be renewed (with 'sync')
fkey *config // key file management
fsync *config // sync file management
}
/* (2) Constructor
*
* @ctx<context.T> Shared context
* @saveDir<string> Configuration path
*
---------------------------------------------------------*/
func New(ctx *context.T, saveDir string) (*T, error) {
var err error;
inst := new(T);
/* (1) Store context */
inst.ctx = ctx;
/* (2) Get file management for KEY */
inst.fkey, err = Config(saveDir, "key");
if err != nil { return nil, err }
/* (3) Get file management for SYNC */
inst.fsync, err = Config(saveDir, "sync");
if err != nil { return nil, err }
/* (4) Init KEY */
inst.key = nil
/* (5) Init SYNC */
inst.sync = nil
/* (6) Restore from config */
inst.Update();
return inst, nil;
}
/* (1) Updates 'key' and 'sync' with files (reads or write)
*
---------------------------------------------------------*/
func (c T) Update(){
/* (1) Restore (both are NIL)
---------------------------------------------------------*/
if c.key == nil && c.sync == nil {
/* (1) Fetch key */
keyReader := c.fkey.Reader();
if keyReader != nil {
defer keyReader.Close();
c.key, _ = keyset.Create(c.ctx);
c.key.Fetch(keyReader);
}
/* (2) Fetch sync */
syncReader := c.fsync.Reader();
if syncReader != nil {
defer syncReader.Close();
c.sync, _ = keyset.Create(c.ctx);
c.sync.Fetch(syncReader);
}
/* (3) Exit if no error */
if c.key != nil && c.sync != nil {
return;
}
}
/* (2) Manage if no config
---------------------------------------------------------*/
if c.key == nil{
c.key, _ = keyset.Create(c.ctx);
}
if c.sync == nil{
c.sync, _ = keyset.Create(c.ctx);
}
/* (3) Store current value
---------------------------------------------------------*/
/* (1) Store key */
c.key, _ = keyset.Create(c.ctx);
keyWriter := c.fkey.Writer();
if keyWriter != nil {
defer keyWriter.Close();
c.key, _ = keyset.Create(c.ctx);
c.key.Store(keyWriter);
}
/* (2) Store sync */
c.sync, _ = keyset.Create(c.ctx);
syncWriter := c.fsync.Writer();
if syncWriter != nil {
defer syncWriter.Close();
c.sync, _ = keyset.Create(c.ctx);
c.sync.Store(syncWriter);
}
}