-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
124 lines (105 loc) · 2.65 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"bufio"
"fmt"
"net"
"time"
)
type Client struct {
nickname string
realname string
conn net.Conn
reader *bufio.Reader
writer *bufio.Writer
serverInput chan Message
output chan string
idle time.Time
}
// NewClient returns a pointer to a Client object
func NewClient(name string, conn net.Conn, serverInput chan Message) *Client {
reader := bufio.NewReader(conn)
writer := bufio.NewWriter(conn)
client := &Client{
nickname: name,
realname: "",
conn: conn,
reader: reader,
writer: writer,
serverInput: serverInput,
output: make(chan string),
idle: time.Now(),
}
// Start the loops that constantly monitor for input and output
// from the client
client.run()
return client
}
// run starts two loops, concurrently, that monitor the Client's input and
// output channels
func (c *Client) run() {
go c.processInput()
go c.SendOutput()
}
// GetInput receives data from the Client and sends it to the server
func (c *Client) processInput() {
for {
// The buffer will stop reading when it detects a new line chatacter
input, _ := c.reader.ReadString('\n')
c.idle = time.Now()
// If the client sends an empty message just ignore it
if input := trimMessage(input); !isEmpty(input) {
msg := NewMessage(c, input)
c.serverInput <- *msg
}
}
}
// Close closes the clients connection and frees resources
func (c *Client) Close(msg string) {
c.output <- msg
c.conn.Close()
close(c.output)
}
// SendOutput monitors the output channel. When data is received from the server
// it is sent to the Client
func (c *Client) SendOutput() {
for data := range c.output {
c.writer.WriteString(data)
c.writer.Flush()
}
}
// isIdle checks the last time the Client was active.
func (c *Client) isIdle() bool {
now := time.Now()
if now.Sub(c.idle) >= IdleTime*time.Minute {
return true
}
return false
}
// getIdle returns a string with the last time the client
// was active
func (c *Client) getIdle() string {
if c.isIdle() {
return fmt.Sprintf("%v", c.idle)
}
return "active"
}
// Connection returns the clients connection
func (c *Client) Connection() net.Conn {
return c.conn
}
// ChannelOut returns the clients output channel
func (c *Client) ChannelOut() chan string {
return c.output
}
// Nickname returns a string containing the Clients nickname
func (c *Client) Nickname() string {
return c.nickname
}
// RealName returns a string containing the Clients real name
func (c *Client) Realname() string {
return c.realname
}
// Receive provides an interface to the clients output channel
func (c *Client) Receive(str string) {
c.output <- str
}