-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathchat_server.go
164 lines (136 loc) · 3.12 KB
/
chat_server.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"flag"
"fmt"
"net"
"os"
"regexp"
"runtime/pprof"
"strconv"
"strings"
)
var connectionCount int
var messagePool chan (string)
const (
INPUT_BUFFER_LENGTH = 140
)
type User struct {
Name string
ID int
Initiated bool
UChannel chan []byte
Connection *net.Conn
}
func (u *User) Listen() {
fmt.Println("Listening for", u.Name)
for {
select {
case msg := <-u.UChannel:
fmt.Println("Sending new message to", u.Name)
fmt.Fprintln(*u.Connection, string(msg))
}
}
}
type ConnectionManager struct {
name string
initiated bool
}
func Initiate() *ConnectionManager {
return &ConnectionManager{
name: "Chat Server 1.0",
initiated: false,
}
}
func evalMessageReceipient(msg []byte, uName string) bool {
eval := true
expression := "@"
re, err := regexp.MatchString(expression, string(msg))
if err != nil {
fmt.Println("Error:", err)
}
if re == true {
eval = false
pmExpression := "@" + uName
pmRe, pmErr := regexp.MatchString(pmExpression, string(msg))
if pmErr != nil {
fmt.Println("Regex Error", err)
}
if pmRe == true {
eval = true
}
}
return eval
}
func (cM *ConnectionManager) Listen(listener net.Listener) {
fmt.Println(cM.name, "started")
for {
conn, err := listener.Accept()
if err != nil {
fmt.Println("Connection error", err)
}
connectionCount++
fmt.Println(conn.RemoteAddr(), "connected")
user := User{Name: "anonymous", ID: 0, Initiated: false}
Users := append(Users, &user)
for _, u := range Users {
fmt.Println("User Online", u.Name)
}
fmt.Println(connectionCount, "connection active")
go cM.messageReady(conn, &user)
}
}
func (cM *ConnectionManager) messageReady(conn net.Conn, user *User) {
uChan := make(chan []byte)
for {
buf := make([]byte, INPUT_BUFFER_LENGTH)
n, err := conn.Read(buf)
if err != nil {
conn.Close()
conn = nil
}
if n == 0 {
conn.Close()
conn = nil
}
fmt.Println(n, "character message from user", user.Name)
if user.Initiated == false {
fmt.Println("New User is", string(buf))
user.Initiated = true
user.Name = string(buf[:n])
user.UChannel = uChan
user.Connection = &conn
go user.Listen()
minuYouCount := strconv.FormatInt(int64(connectionCount-1), 10)
conn.Write([]byte("Welcome to the chat, " + user.Name + ",there are " + minuYouCount + " other users"))
} else {
sendMessage := []byte(user.Name + ": " + strings.TrimRight(string(buf), " \t\r\n"))
for _, u := range Users {
if evalMessageReceipient(sendMessage, u.Name) == true {
u.UChannel <- sendMessage
}
}
}
}
}
var Users []*User
var profile = flag.String("cpuprofile", "", "output pprof data to file")
func main() {
connectionCount = 0
serverClosed := make(chan bool)
flag.Parse()
if *profile != "" {
flag, err := os.Create(*profile)
if err != nil {
fmt.Println("Could not create profile", err)
}
pprof.StartCPUProfile(flag)
defer pprof.StopCPUProfile()
}
listener, err := net.Listen("tcp", ":9000")
if err != nil {
fmt.Println("Could not start server!", err)
}
connManage := Initiate()
go connManage.Listen(listener)
<-serverClosed
}