-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlocalconn.go
More file actions
68 lines (60 loc) · 1.54 KB
/
localconn.go
File metadata and controls
68 lines (60 loc) · 1.54 KB
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
package main
import (
"net"
"fmt"
"strings"
"os"
)
// listener to fron end
func (n *Node) localConnection(addr string){
tcpAddr, err := net.ResolveTCPAddr("tcp4", addr)
if err != nil{
fmt.Println("User not found")
}
listener, err2 := net.ListenTCP("tcp", tcpAddr)
if err2 != nil{
fmt.Println(err2)
}
defer listener.Close()
for {
conn, err3 := listener.Accept()
if err3 != nil {
fmt.Println(err3)
}
go n.handleClient(conn)
}
}
// handle msg from front end
func (n *Node) handleClient(conn net.Conn) {
defer conn.Close()
buf := make([]byte, 1024)
relen, err := conn.Read(buf)
if err != nil {
fmt.Println("Error reading:", err.Error())
}
s := string(buf[:relen])
if (strings.HasPrefix(s,"invite")) {
s = strings.TrimPrefix(s,"invite")
fmt.Println(s)
n.invite(s)
} else if (strings.HasPrefix(s, "PATH")){
s = strings.TrimPrefix(s, "PATH:")
fmt.Println("this is the image path", s)
n.Image_path = s
n.HasImage = true
} else if (strings.HasPrefix(s, "quit")) {
n.sendToFront("quit")
n.HasImage = false
os.Exit(0)
} else {
msg := n.createDataMessage(PUBLIC, s)
chans[msg.Ety.Time_stamp] = make(chan bool)
n.updateToAll(msg, chans[msg.Ety.Time_stamp])
}
}
// send to front end
func (n *Node) sendToFront(logEty string) {
conn,_ := net.Dial("tcp", n.localsendAddr)
defer conn.Close()
conn.Write([]byte(logEty))
}