-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
131 lines (116 loc) · 2.63 KB
/
main.go
File metadata and controls
131 lines (116 loc) · 2.63 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
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
package main
import (
"fmt"
"log"
"net"
"runtime"
"strings"
"time"
console "github.com/asynkron/goconsole"
"github.com/asynkron/protoactor-go/actor"
)
type (
MSGLogin struct{
user string
pass string
}
ServiceActor struct {
listener net.Listener
sessions map[SessionNew]struct{}
}
SessionNew struct {
conn *net.Conn
}
SessionExit struct {
session SessionNew
}
)
func newSession(conn *net.Conn) actor.Producer {
return func() actor.Actor {
return &SessionNew{
conn: conn,
}
}
}
func (this *SessionNew) Receive(context actor.Context) {
switch msg := context.Message().(type) {
case *actor.Started:
log.Println("Sessions Start", msg)
context.Send(context.Parent(), this)
go this.sessionHandler(context)
case *actor.Stopping:
(*this.conn).Close()
case *actor.Stopped:
log.Println("Connection Closed")
}
}
func (this *SessionNew) sessionHandler(context actor.Context) {
buf := make([]byte, 1024)
for {
n, err := (*this.conn).Read(buf)
if err != nil {
if err.Error() == "EOF" {
break
}
log.Printf("\nconn read error:%v", err)
break
}
reply := strings.ToUpper(string(buf[0:n]))
(*this.conn).Write([]byte(reply))
}
log.Printf("\nSession %v Handler exit\n", this)
context.Send(context.Parent(), &SessionExit{session: *this})
}
func newService() actor.Actor {
nl, err := net.Listen("tcp", ":8899")
if err != nil {
log.Fatal(err)
}
return &ServiceActor{
listener: nl,
sessions: make(map[SessionNew]struct{}),
}
}
func (this *ServiceActor) Receive(context actor.Context) {
switch msg := context.Message().(type) {
case *actor.Started:
log.Println("Listening :8899")
go this.hanlder(context)
case *SessionNew:
log.Printf("Add Session: %v\n", msg.conn)
this.sessions[*msg] = struct{}{}
case *SessionExit:
delete(this.sessions, msg.session)
log.Printf("Sessions NO.: %v\n", len(this.sessions))
case *MSGLogin:
log.Printf("%v Says:Hello %v\n", context.Self().GetAddress(), msg.user)
}
}
func (this *ServiceActor) hanlder(context actor.Context) {
for {
conn, _ := this.listener.Accept()
log.Printf("New Connection: %v\n", conn)
props := actor.PropsFromProducer(newSession(&conn))
pid, err := context.SpawnNamed(props, fmt.Sprintf("session/%v", conn))
if err != nil {
log.Println(pid, err)
}
}
}
func init() {
ticker := time.NewTicker(10 * time.Second)
go func() {
for {
select {
case <-ticker.C:
log.Printf("Running Routins: %v\n", runtime.NumGoroutine())
}
}
}()
}
func main() {
system := actor.NewActorSystem()
props := actor.PropsFromProducer(newService)
system.Root.Spawn(props)
_, _ = console.ReadLine()
}