OK, so let us begin implementation, and let us promise (this time) to Keep It As Simple As Possible. Never implement any complexity because you think it might be important later. Only implement complexity when it is clearly needed to achieve the next goal.
So what's the first goal? I want a Message structure, and a Bulletin Board.
I want a nice general message structure that can do about anything, and I think this is it:
type Message map [ string ] interface{}
That's a bunch of names where each name is associated with anything at all. Ah, but how does the receiver interpret those anythings? The message must have a type. Let's just make that a string, too.
type Message struct {
Type string
Data map [ string ] interface{}
}
OK, that's better. Now the idea is that any receiver that knows this message type will also know how to interpret all of the values that the Data map contains.
So, what is the Bulletin Board?
Right now it is just a place for Abstractors to store their output channels. Every time an Abstractor starts up it will present its output channel -- the channel on which it sends the Abstractions it produces -- to the BB, and the BB will store it.
Here's a message channel:
type Message_Channel chan Message
And here's the initial implementation of the Bulletin Board:
type Bulletin_Board struct {
message_channels map [string] []Message_Channel
}
For each channel-type (which is the same as the type of the messages that flow through it) the BB stores an array of channels. This is because there may be multiple Abstractors putting out the same message type.
And now we can write the BB's Register_Channel function.
func ( bb * Bulletin_Board ) Register_Channel ( message_type string, channel Message_Channel ) {
bb.message_channels[message_type] = append(bb.message_channels[message_type], channel )
fp ( os.Stdout, "BB now has : \n" )
for message_type, channels := range bb.message_channels {
fp ( os.Stdout, " message type: %s channels: %d\n", message_type, len(channels) )
}
}
Yeck. I guess it's not going to be very nice to paste much code here.
Well, you can see the code by cloning:
https://github.com/mgoulish/tachyon
and looking at commit:
0755cf718c71d296d29e314778a67fb5740f0ff3
From main (for now standing in for an Abstractor) we can now do this:
bb := t.New_Bulletin_Board ( )
var my_output_channel t.Message_Channel
bb.Register_Channel ( "my_type", my_output_channel )
bb.Register_Channel ( "my_type", my_output_channel )
and get this output:
BB now has :
message type: my_type channels: 1
BB now has :
message type: my_type channels: 2
So! That's a start.
... a good start!
ReplyDelete