diff --git a/src/git.xdrm.io/schastsp/client/client.go b/src/git.xdrm.io/schastsp/client/client.go new file mode 100644 index 0000000..7920ea1 --- /dev/null +++ b/src/git.xdrm.io/schastsp/client/client.go @@ -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 Shared context +* @saveDir 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); + } + +} \ No newline at end of file