Permalink
Browse files

Merge pull request #1 from surma-dump/asu/refactor

Some splitting
  • Loading branch information...
2 parents 6253a3e + 9df6a7d commit 61375acd928569a5d2f651c724a4d6da24132d1d @akrennmair committed Sep 4, 2012
Showing with 58 additions and 55 deletions.
  1. +58 −55 03_chat/chat.go
View
@@ -3,13 +3,17 @@ package main
import (
"bufio"
"fmt"
+ "io"
+ "log"
"net"
"os"
+ "strings"
)
type Client struct {
- conn net.Conn
- ch chan<- string
+ conn net.Conn
+ nickname string
+ ch chan string
}
func main() {
@@ -21,7 +25,7 @@ func main() {
msgchan := make(chan string)
addchan := make(chan Client)
- rmchan := make(chan net.Conn)
+ rmchan := make(chan Client)
go handleMessages(msgchan, addchan, rmchan)
@@ -36,78 +40,77 @@ func main() {
}
}
-func handleConnection(c net.Conn, msgchan chan<- string, addchan chan<- Client, rmchan chan<- net.Conn) {
- ch := make(chan string)
-
- msgs := make(chan string)
-
- addchan <- Client{c, ch}
-
- go func() {
- defer close(msgs)
-
- bufc := bufio.NewReader(c)
+func (c Client) ReadLinesInto(ch chan<- string) {
+ bufc := bufio.NewReader(c.conn)
+ for {
+ line, err := bufc.ReadString('\n')
+ if err != nil {
+ break
+ }
+ ch <- fmt.Sprintf("%s: %s", c.nickname, line)
+ }
+}
- c.Write([]byte("\033[1;30;41mWelcome to the fancy demo chat!\033[0m\r\nWhat is your nick? "))
- nick, _, err := bufc.ReadLine()
+func (c Client) WriteLinesFrom(ch <-chan string) {
+ for msg := range ch {
+ _, err := io.WriteString(c.conn, msg)
if err != nil {
return
}
+ }
+}
- nickname := string(nick)
-
- c.Write([]byte("Welcome, " + nickname + "!\r\n\r\n"))
-
- msgs <- "New user " + nickname + " has joined the chat room."
+func promptNick(c net.Conn, bufc *bufio.Reader) string {
+ io.WriteString(c, "\033[1;30;41mWelcome to the fancy demo chat!\033[0m\n")
+ io.WriteString(c, "What is your nick? ")
+ nick, _, _ := bufc.ReadLine()
+ return string(nick)
+}
- for {
- line, _, err := bufc.ReadLine()
- if err != nil {
- break
- }
- msgs <- nickname + ": " + string(line)
- }
+func handleConnection(c net.Conn, msgchan chan<- string, addchan chan<- Client, rmchan chan<- Client) {
+ bufc := bufio.NewReader(c)
+ defer c.Close()
+ client := Client{
+ conn: c,
+ nickname: promptNick(c, bufc),
+ ch: make(chan string),
+ }
+ if strings.TrimSpace(client.nickname) == "" {
+ io.WriteString(c, "Invalid Username\n")
+ return
+ }
- msgs <- "User " + nickname + " left the chat room."
+ // Register user
+ addchan <- client
+ defer func() {
+ msgchan <- fmt.Sprintf("User %s left the chat room.\n", client.nickname)
+ log.Printf("Connection from %v closed.\n", c.RemoteAddr())
+ rmchan <- client
}()
+ io.WriteString(c, fmt.Sprintf("Welcome, %s!\n\n", client.nickname))
+ msgchan <- fmt.Sprintf("New user %s has joined the chat room.\n", client.nickname)
-LOOP:
- for {
- select {
- case msg, ok := <-msgs:
- if !ok {
- break LOOP
- }
- msgchan <- msg
- case msg := <-ch:
- _, err := c.Write([]byte(msg))
- if err != nil {
- break LOOP
- }
- }
- }
-
- c.Close()
- fmt.Printf("Connection from %v closed.\n", c.RemoteAddr())
- rmchan <- c
+ // I/O
+ go client.ReadLinesInto(msgchan)
+ client.WriteLinesFrom(client.ch)
}
-func handleMessages(msgchan <-chan string, addchan <-chan Client, rmchan <-chan net.Conn) {
+func handleMessages(msgchan <-chan string, addchan <-chan Client, rmchan <-chan Client) {
clients := make(map[net.Conn]chan<- string)
for {
select {
case msg := <-msgchan:
- fmt.Printf("new message: %s\n", msg)
+ log.Printf("New message: %s", msg)
for _, ch := range clients {
- go func(mch chan<- string) { mch <- "\033[1;33;40m" + msg + "\033[m\r\n" }(ch)
+ go func(mch chan<- string) { mch <- "\033[1;33;40m" + msg + "\033[m" }(ch)
}
case client := <-addchan:
- fmt.Printf("New client: %v\n", client.conn)
+ log.Printf("New client: %v\n", client.conn)
clients[client.conn] = client.ch
- case conn := <-rmchan:
- fmt.Printf("Client disconnects: %v\n", conn)
- delete(clients, conn)
+ case client := <-rmchan:
+ log.Printf("Client disconnects: %v\n", client.conn)
+ delete(clients, client.conn)
}
}
}

0 comments on commit 61375ac

Please sign in to comment.