summaryrefslogtreecommitdiff
path: root/rooms.go
diff options
context:
space:
mode:
authorKevin Chabowski <kevin@kch42.de>2014-03-25 15:54:32 +0100
committerKevin Chabowski <kevin@kch42.de>2014-03-25 15:54:32 +0100
commit966c17bb84866f10fc9e995df4107ed11ed2bdce (patch)
tree5e0446c4bb80dcefd7e705ce8de38d1a06989418 /rooms.go
parent3aa1c450a3376a4339354579f90b6111b963c78a (diff)
downloadsimplechat-966c17bb84866f10fc9e995df4107ed11ed2bdce.tar.gz
simplechat-966c17bb84866f10fc9e995df4107ed11ed2bdce.tar.bz2
simplechat-966c17bb84866f10fc9e995df4107ed11ed2bdce.zip
Move protocol agnostic stuff into own package.
So we can test it more easily.
Diffstat (limited to 'rooms.go')
-rw-r--r--rooms.go76
1 files changed, 0 insertions, 76 deletions
diff --git a/rooms.go b/rooms.go
deleted file mode 100644
index ff441f2..0000000
--- a/rooms.go
+++ /dev/null
@@ -1,76 +0,0 @@
-package main
-
-import (
- "errors"
-)
-
-type Room struct {
- Messages chan Message
- Buddies map[string]*Buddy
-}
-
-func NewRoom() (r *Room) {
- r = new(Room)
- r.Messages = make(chan Message)
- r.Buddies = make(map[string]*Buddy)
- go r.Broadcast()
- return
-}
-
-func (r *Room) Leave(nick string) {
- if _, ok := r.Buddies[nick]; !ok {
- return
- }
-
- delete(r.Buddies, nick)
- if len(r.Buddies) == 0 {
- close(r.Messages)
- } else {
- r.Messages <- Message{
- Type: MsgLeave,
- User: nick,
- }
- }
-}
-
-func (r *Room) Broadcast() {
- for m := range r.Messages {
- for _, buddy := range r.Buddies {
- buddy.Push(m)
- }
- }
-}
-
-func (r *Room) ListBuddies() (buddies []string) {
- for nick := range r.Buddies {
- buddies = append(buddies, nick)
- }
- return
-}
-
-var rooms = make(map[string]*Room)
-
-func Join(room, nick string) (*Buddy, *Room, error) {
- r, ok := rooms[room]
- if !ok {
- r = NewRoom()
- rooms[room] = r
- }
-
- if _, there := r.Buddies[nick]; there {
- return nil, nil, errors.New("Nickname is already in use")
- }
-
- if len(r.Buddies) >= *perroom {
- return nil, nil, errors.New("Room is full")
- }
-
- r.Messages <- Message{
- Type: MsgJoin,
- User: nick,
- }
-
- b := NewBuddy(nick, r)
- r.Buddies[nick] = b
- return b, r, nil
-}