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,
// splits messages by CRLF and send them to Daemon gorouting for processing
// 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
buf := make([]byte, 0)
log.Println("New client", client)

View File

@ -46,11 +46,11 @@ type Daemon struct {
rooms map[string]*Room
room_sinks map[*Room]chan ClientEvent
last_aliveness_check time.Time
log_sink chan LogEvent
state_sink chan StateEvent
log_sink chan<- LogEvent
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.clients = make(map[*Client]bool)
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
// 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_sink := make(chan ClientEvent)
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 {
// Check for clients aliveness

View File

@ -60,7 +60,7 @@ type LogEvent struct {
// Logging events logger itself
// Each room's events are written to separate file in logdir
// 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
perm := os.FileMode(0660)
var format string
@ -93,7 +93,7 @@ type StateEvent struct {
// Room state events saver
// Room states shows that either topic or key has been changed
// 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
perm := os.FileMode(0660)
for event := range events {

View File

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

View File

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