From 4c387d45a969e50ab4402a84b65c6fdd7e3a93c2 Mon Sep 17 00:00:00 2001 From: Bernhard Tittelbach Date: Sat, 5 Oct 2013 00:35:23 +0000 Subject: [PATCH] Brains: tasty but not needed right now, maybe useful to somebody else --- go/brain/brain.go | 118 ++++++++++++++++++++++++++++++++++++++++ go/r3-netstatus/brain/brain.go | 118 ---------------------------------------- 2 files changed, 118 insertions(+), 118 deletions(-) create mode 100644 go/brain/brain.go delete mode 100644 go/r3-netstatus/brain/brain.go diff --git a/go/brain/brain.go b/go/brain/brain.go new file mode 100644 index 0000000..3bd8ce4 --- /dev/null +++ b/go/brain/brain.go @@ -0,0 +1,118 @@ +// (c) Bernhard Tittelbach, 2013 + +package brain + +import "errors" + +type informationtuple struct { + name string + value interface{} +} + +type informationretrievalpath struct { + name string + returnpath chan interface{} +} + +type hippocampus map[string]interface{} + +type Brain struct { + storeTuple chan informationtuple + retrieveValue chan informationretrievalpath + shutdown chan bool +} + +func New() *Brain { + b := new(Brain) + b.storeTuple = make(chan informationtuple) + b.retrieveValue = make(chan informationretrievalpath) + go b.runBrain() + return b +} + +func (b *Brain) runBrain() { + var h hippocampus = make(hippocampus) + for { + select { + case newtuple := <- b.storeTuple: + h[newtuple.name] = newtuple.value + + case retrievvalue := <- b.retrieveValue: + v, e := h[retrievvalue.name] + if e { + retrievvalue.returnpath <- v + } else { + retrievvalue.returnpath <- nil + } + + case <- b.shutdown: + break + } + } +} + +func (b *Brain) Shutdown() { + b.shutdown <- true +} + +func (b *Brain) Oboite(name string, value interface{}) { + b.storeTuple <- informationtuple{name, value} +} + +func (b *Brain) OmoiDashite(name string) (interface{}, error) { + rc := make(chan interface{}) + b.retrieveValue <- informationretrievalpath{name, rc} + v := <- rc + if v == nil { + return v, errors.New("name not in brain") + } + return v, nil +} + +func (b *Brain) OmoiDashiteBool(name string) (bool, error) { + v, e := b.OmoiDashite(name) + if e != nil { + return false, e + } + vc, ok := v.(bool) + if !ok { + return false, errors.New(name + " does not have type bool") + } + return vc, nil +} + +func (b *Brain) OmoiDashiteInt(name string) (int, error) { + v, e := b.OmoiDashite(name) + if e != nil { + return 0, e + } + vc, ok := v.(int) + if !ok { + return 0, errors.New(name + " does not have type int") + } + return vc, nil +} + +func (b *Brain) OmoiDashiteFloat(name string) (float64, error) { + v, e := b.OmoiDashite(name) + if e != nil { + return 0, e + } + vc, ok := v.(float64) + if !ok { + return 0, errors.New(name + " does not have type float64") + } + return vc, nil +} + +func (b *Brain) OmoiDashiteString(name string) (string, error) { + v, e := b.OmoiDashite(name) + if e != nil { + return "", e + } + vc, ok := v.(string) + if !ok { + return "", errors.New(name + " does not have type string") + } + return vc, nil +} \ No newline at end of file diff --git a/go/r3-netstatus/brain/brain.go b/go/r3-netstatus/brain/brain.go deleted file mode 100644 index 3bd8ce4..0000000 --- a/go/r3-netstatus/brain/brain.go +++ /dev/null @@ -1,118 +0,0 @@ -// (c) Bernhard Tittelbach, 2013 - -package brain - -import "errors" - -type informationtuple struct { - name string - value interface{} -} - -type informationretrievalpath struct { - name string - returnpath chan interface{} -} - -type hippocampus map[string]interface{} - -type Brain struct { - storeTuple chan informationtuple - retrieveValue chan informationretrievalpath - shutdown chan bool -} - -func New() *Brain { - b := new(Brain) - b.storeTuple = make(chan informationtuple) - b.retrieveValue = make(chan informationretrievalpath) - go b.runBrain() - return b -} - -func (b *Brain) runBrain() { - var h hippocampus = make(hippocampus) - for { - select { - case newtuple := <- b.storeTuple: - h[newtuple.name] = newtuple.value - - case retrievvalue := <- b.retrieveValue: - v, e := h[retrievvalue.name] - if e { - retrievvalue.returnpath <- v - } else { - retrievvalue.returnpath <- nil - } - - case <- b.shutdown: - break - } - } -} - -func (b *Brain) Shutdown() { - b.shutdown <- true -} - -func (b *Brain) Oboite(name string, value interface{}) { - b.storeTuple <- informationtuple{name, value} -} - -func (b *Brain) OmoiDashite(name string) (interface{}, error) { - rc := make(chan interface{}) - b.retrieveValue <- informationretrievalpath{name, rc} - v := <- rc - if v == nil { - return v, errors.New("name not in brain") - } - return v, nil -} - -func (b *Brain) OmoiDashiteBool(name string) (bool, error) { - v, e := b.OmoiDashite(name) - if e != nil { - return false, e - } - vc, ok := v.(bool) - if !ok { - return false, errors.New(name + " does not have type bool") - } - return vc, nil -} - -func (b *Brain) OmoiDashiteInt(name string) (int, error) { - v, e := b.OmoiDashite(name) - if e != nil { - return 0, e - } - vc, ok := v.(int) - if !ok { - return 0, errors.New(name + " does not have type int") - } - return vc, nil -} - -func (b *Brain) OmoiDashiteFloat(name string) (float64, error) { - v, e := b.OmoiDashite(name) - if e != nil { - return 0, e - } - vc, ok := v.(float64) - if !ok { - return 0, errors.New(name + " does not have type float64") - } - return vc, nil -} - -func (b *Brain) OmoiDashiteString(name string) (string, error) { - v, e := b.OmoiDashite(name) - if e != nil { - return "", e - } - vc, ok := v.(string) - if !ok { - return "", errors.New(name + " does not have type string") - } - return vc, nil -} \ No newline at end of file -- 1.7.10.4