summaryrefslogtreecommitdiff
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
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.
-rw-r--r--chat/buddy.go (renamed from buddy.go)2
-rw-r--r--chat/messages.go (renamed from messages.go)2
-rw-r--r--chat/rooms.go (renamed from rooms.go)14
-rw-r--r--main.go2
-rw-r--r--websock.go3
5 files changed, 17 insertions, 6 deletions
diff --git a/buddy.go b/chat/buddy.go
index 1128956..c6f07ad 100644
--- a/buddy.go
+++ b/chat/buddy.go
@@ -1,4 +1,4 @@
-package main
+package chat
import (
"time"
diff --git a/messages.go b/chat/messages.go
index 44e5662..793fc6c 100644
--- a/messages.go
+++ b/chat/messages.go
@@ -1,4 +1,4 @@
-package main
+package chat
import (
"encoding/json"
diff --git a/rooms.go b/chat/rooms.go
index ff441f2..e954e29 100644
--- a/rooms.go
+++ b/chat/rooms.go
@@ -1,4 +1,4 @@
-package main
+package chat
import (
"errors"
@@ -48,7 +48,15 @@ func (r *Room) ListBuddies() (buddies []string) {
return
}
-var rooms = make(map[string]*Room)
+var (
+ rooms map[string]*Room
+ perroom int
+)
+
+func InitRooms(room_limit int) {
+ rooms = make(map[string]*Room)
+ perroom = room_limit
+}
func Join(room, nick string) (*Buddy, *Room, error) {
r, ok := rooms[room]
@@ -61,7 +69,7 @@ func Join(room, nick string) (*Buddy, *Room, error) {
return nil, nil, errors.New("Nickname is already in use")
}
- if len(r.Buddies) >= *perroom {
+ if len(r.Buddies) >= perroom {
return nil, nil, errors.New("Room is full")
}
diff --git a/main.go b/main.go
index 9502f0c..4efcc9c 100644
--- a/main.go
+++ b/main.go
@@ -3,6 +3,7 @@ package main
import (
"flag"
"github.com/gorilla/mux"
+ "github.com/kch42/simplechat/chat"
"log"
"math"
"net/http"
@@ -25,6 +26,7 @@ func main() {
}
PrepTemplates()
+ chat.InitRooms(*perroom)
r := mux.NewRouter()
r.HandleFunc("/", Home)
diff --git a/websock.go b/websock.go
index 6b69788..bf4fabd 100644
--- a/websock.go
+++ b/websock.go
@@ -3,6 +3,7 @@ package main
import (
"code.google.com/p/go.net/websocket"
"github.com/gorilla/mux"
+ "github.com/kch42/simplechat/chat"
"net/http"
)
@@ -25,7 +26,7 @@ func AcceptWebSock(rw http.ResponseWriter, req *http.Request) {
return
}
- buddy, room, err := Join(roomname, nick)
+ buddy, room, err := chat.Join(roomname, nick)
if err != nil {
send(JoinResponse{
OK: false,