forked from sunfmin/assemblyline
/
web.go
136 lines (111 loc) · 2.12 KB
/
web.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
package main
import (
"code.google.com/p/go.net/websocket"
"log"
"math/rand"
"net/http"
"net/rpc"
"net/rpc/jsonrpc"
"time"
)
type Movement struct {
From string
To string
Thing interface{}
}
type GoroutineGroup struct {
Name string
Status map[string]string
}
var GoroutineGroups = make(map[string]*GoroutineGroup)
func GGroup(name string) (r *GoroutineGroup) {
r = GoroutineGroups[name]
if r != nil {
return
}
r = &GoroutineGroup{
Name: name,
Status: make(map[string]string),
}
GoroutineGroups[name] = r
return
}
type Thing int
type Input struct {
Workers map[string]int
}
type Output struct {
MethodName string
Workers map[string]*Worktime
}
func (t Thing) UpdateConfig(ri *Input, reply *Output) (err error) {
reply.MethodName = "Thing.UpdateConfig"
RC.Restart(ri.Workers, false)
reply.Workers = CurrentWorkTime
return
}
func init() {
rpc.Register(new(Thing))
}
func Server() {
rand.Seed(int64(time.Now().Nanosecond()))
http.Handle("/connect", websocket.Handler(Connect))
err := http.ListenAndServe(":7890", nil)
if err != nil {
panic(err)
}
}
func ForDemoPause(worktime time.Duration) {
time.Sleep(worktime)
}
var Sockets []*websocket.Conn
type Command struct {
MethodName string
Data interface{}
}
func Connect(ws *websocket.Conn) {
Sockets = append(Sockets, ws)
SendCommand("Connection.Ready", time.Now())
jsonrpc.ServeConn(ws)
var newsockets []*websocket.Conn
for _, s := range Sockets {
if s == ws {
continue
}
newsockets = append(newsockets, s)
}
Sockets = newsockets
}
func SendCommand(methodName string, data interface{}) {
cmd := Command{methodName, data}
for _, s := range Sockets {
err := websocket.JSON.Send(s, cmd)
if err != nil {
log.Println(err)
}
}
}
func SendGoroutineStatus() {
for {
SendCommand("Goroutine.Status", GoroutineGroups)
time.Sleep(time.Millisecond * 500)
}
}
var ids = make(map[string]chan int64)
func NewId(t string) (r int64) {
if ids[t] == nil {
ids[t] = make(chan int64)
go func() {
var i int64
for {
i = i + 1
ids[t] <- i
if i > 10000 {
i = 0
}
}
}()
}
r = <-ids[t]
return
}