improve ajar msg
[svn42.git] / go / r3-eventbroker_zmq / smallbrain.go
1 // (c) Bernhard Tittelbach, 2013
2
3 package main
4
5 import (
6     zmq "github.com/vaughan0/go-zmq"
7     r3events "svn.spreadspace.org/realraum/go.svn/r3events"
8 )
9
10 type hippocampus map[string]interface{}
11
12 func BrainCenter( zmq_ctx *zmq.Context, listen_addr string, event_chan <- chan interface{} ) {
13     zbrain_chans, err := ZmqsBindNewReplySocket(zmq_ctx, listen_addr)
14     if err != nil { panic(err) }
15     defer zbrain_chans.Close()
16     h := make(hippocampus,5)
17
18     for { select {
19         case event, ec_still_open := <- event_chan:
20             if ! ec_still_open { return }
21             h[r3events.NameOfStruct(event)] = event
22             Debug_.Printf("Brain: stored %s, %s", r3events.NameOfStruct(event), event)
23
24         case brain_request := <- zbrain_chans.In():
25             if len(brain_request) == 0 { continue }
26             requested_eventname := string(brain_request[0])
27             Debug_.Printf("Brain: received request: %s", requested_eventname)
28             retr_event, is_in_map := h[requested_eventname]
29             if is_in_map {
30                 data, err := r3events.MarshalEvent2ByteByte(retr_event)
31                 if err == nil {
32                     zbrain_chans.Out() <- data
33                     continue
34                 } else {
35                     Syslog_.Print("BrainCenter", err)
36                     Debug_.Print("BrainCenter", err)
37                 }
38             }
39             zbrain_chans.Out() <- [][]byte{[]byte("UNKNOWN")}
40     } }
41 }