Make channels unidirectional.

This commit is contained in:
Thomas Habets 2014-06-08 02:21:06 +02:00
parent 1f4a463c0a
commit de49f5eeba
5 changed files with 14 additions and 16 deletions

View File

@ -52,7 +52,7 @@ func NewClient(hostname string, conn net.Conn) *Client {
// Client processor blockingly reads everything remote client sends, // Client processor blockingly reads everything remote client sends,
// splits messages by CRLF and send them to Daemon gorouting for processing // splits messages by CRLF and send them to Daemon gorouting for processing
// it futher. Also it can signalize that client is unavailable (disconnected). // it futher. Also it can signalize that client is unavailable (disconnected).
func (client *Client) Processor(sink chan ClientEvent) { func (client *Client) Processor(sink chan<- ClientEvent) {
var buf_net []byte var buf_net []byte
buf := make([]byte, 0) buf := make([]byte, 0)
log.Println("New client", client) log.Println("New client", client)

View File

@ -46,11 +46,11 @@ type Daemon struct {
rooms map[string]*Room rooms map[string]*Room
room_sinks map[*Room]chan ClientEvent room_sinks map[*Room]chan ClientEvent
last_aliveness_check time.Time last_aliveness_check time.Time
log_sink chan LogEvent log_sink chan<- LogEvent
state_sink chan StateEvent state_sink chan<- StateEvent
} }
func NewDaemon(hostname, motd string, log_sink chan LogEvent, state_sink chan StateEvent) *Daemon { func NewDaemon(hostname, motd string, log_sink chan<- LogEvent, state_sink chan<- StateEvent) *Daemon {
daemon := Daemon{hostname: hostname, motd: motd} daemon := Daemon{hostname: hostname, motd: motd}
daemon.clients = make(map[*Client]bool) daemon.clients = make(map[*Client]bool)
daemon.rooms = make(map[string]*Room) daemon.rooms = make(map[string]*Room)
@ -195,7 +195,7 @@ func (daemon *Daemon) ClientRegister(client *Client, command string, cols []stri
// Register new room in Daemon. Create an object, events sink, save pointers // Register new room in Daemon. Create an object, events sink, save pointers
// to corresponding daemon's places and start room's processor goroutine. // to corresponding daemon's places and start room's processor goroutine.
func (daemon *Daemon) RoomRegister(name string) (*Room, chan ClientEvent) { func (daemon *Daemon) RoomRegister(name string) (*Room, chan<- ClientEvent) {
room_new := NewRoom(daemon.hostname, name, daemon.log_sink, daemon.state_sink) room_new := NewRoom(daemon.hostname, name, daemon.log_sink, daemon.state_sink)
room_sink := make(chan ClientEvent) room_sink := make(chan ClientEvent)
daemon.rooms[name] = room_new daemon.rooms[name] = room_new
@ -252,7 +252,7 @@ func (daemon *Daemon) HandlerJoin(client *Client, cmd string) {
} }
} }
func (daemon *Daemon) Processor(events chan ClientEvent) { func (daemon *Daemon) Processor(events <-chan ClientEvent) {
for event := range events { for event := range events {
// Check for clients aliveness // Check for clients aliveness

View File

@ -60,7 +60,7 @@ type LogEvent struct {
// Logging events logger itself // Logging events logger itself
// Each room's events are written to separate file in logdir // Each room's events are written to separate file in logdir
// Events include messages, topic and keys changes, joining and leaving // Events include messages, topic and keys changes, joining and leaving
func Logger(logdir string, events chan LogEvent) { func Logger(logdir string, events <-chan LogEvent) {
mode := os.O_CREATE | os.O_WRONLY | os.O_APPEND mode := os.O_CREATE | os.O_WRONLY | os.O_APPEND
perm := os.FileMode(0660) perm := os.FileMode(0660)
var format string var format string
@ -93,7 +93,7 @@ type StateEvent struct {
// Room state events saver // Room state events saver
// Room states shows that either topic or key has been changed // Room states shows that either topic or key has been changed
// Each room's state is written to separate file in statedir // Each room's state is written to separate file in statedir
func StateKeeper(statedir string, events chan StateEvent) { func StateKeeper(statedir string, events <-chan StateEvent) {
mode := os.O_CREATE | os.O_TRUNC | os.O_WRONLY mode := os.O_CREATE | os.O_TRUNC | os.O_WRONLY
perm := os.FileMode(0660) perm := os.FileMode(0660)
for event := range events { for event := range events {

View File

@ -50,8 +50,7 @@ func Run() {
if *logdir == "" { if *logdir == "" {
// Dummy logger // Dummy logger
go func() { go func() {
for { for _ = range log_sink {
<-log_sink
} }
}() }()
} else { } else {
@ -68,8 +67,7 @@ func Run() {
if *statedir == "" { if *statedir == "" {
// Dummy statekeeper // Dummy statekeeper
go func() { go func() {
for { for _ = range state_sink {
<-state_sink
} }
}() }()
} else { } else {

View File

@ -41,11 +41,11 @@ type Room struct {
key string key string
members map[*Client]bool members map[*Client]bool
hostname string hostname string
log_sink chan LogEvent log_sink chan<- LogEvent
state_sink chan StateEvent state_sink chan<- StateEvent
} }
func NewRoom(hostname, name string, log_sink chan LogEvent, state_sink chan StateEvent) *Room { func NewRoom(hostname, name string, log_sink chan<- LogEvent, state_sink chan<- StateEvent) *Room {
room := Room{name: name} room := Room{name: name}
room.members = make(map[*Client]bool) room.members = make(map[*Client]bool)
room.topic = "" room.topic = ""
@ -78,7 +78,7 @@ func (room *Room) StateSave() {
room.state_sink <- StateEvent{room.name, room.topic, room.key} room.state_sink <- StateEvent{room.name, room.topic, room.key}
} }
func (room *Room) Processor(events chan ClientEvent) { func (room *Room) Processor(events <-chan ClientEvent) {
var client *Client var client *Client
for event := range events { for event := range events {
client = event.client client = event.client