1
0
Fork 0
forked from External/ergo

Make Message an interface with attached handling behavior.

This commit is contained in:
Jeremy Latt 2012-04-17 18:16:57 -07:00
parent d1f8c7657b
commit fed72a7aa3
6 changed files with 190 additions and 96 deletions

View file

@ -1,21 +1,13 @@
package irc
import (
"log"
"net"
"strings"
)
type Message struct {
command string
args string
client *Client
}
type Client struct {
addr net.Addr
send chan string
recv chan string
send chan<- string
recv <-chan string
username string
realname string
nick string
@ -31,19 +23,12 @@ func NewClient(conn net.Conn) *Client {
}
// Adapt `chan string` to a `chan Message`.
func (c *Client) Communicate(server *Server) {
go func() {
for str := range c.recv {
parts := strings.SplitN(str, " ", 2)
server.Send(Message{parts[0], parts[1], c})
func (c *Client) Communicate(server chan<- *ClientMessage) {
for str := range c.recv {
m := ParseMessage(str)
if m != nil {
server <- &ClientMessage{c, m}
}
}()
}
func (c *Client) Send(lines ...string) {
for _, line := range lines {
log.Printf("C <- S: %s", line)
c.send <- line
}
}