Skip to content

Commit

Permalink
Merge pull request #7 from neo-ngd/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
zhangtao authored Mar 13, 2019
2 parents b3d0b21 + dd3875a commit 1dd84ec
Show file tree
Hide file tree
Showing 432 changed files with 43,619 additions and 71 deletions.
38 changes: 27 additions & 11 deletions api/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,51 @@ import (

type logcache struct {
mutex sync.Mutex
cache []logBody
cache map[string]([]logBody)
count int
}

func NewLogCache(n int) *logcache {
c := &logcache{
cache: []logBody{},
cache: make(map[string]([]logBody)),
count: n,
}
return c
}

func (lc *logcache) Append(log logBody) {
func (lc *logcache) append(name string, log logBody) {
lc.mutex.Lock()
defer lc.mutex.Unlock()
tmp := append(lc.cache, log)
if len(tmp) <= lc.count {
lc.cache = tmp
logs, ok := lc.cache[name]
if !ok {
lc.cache[name] = []logBody{log}
}
logs = append(logs, log)
if len(logs) <= lc.count {
lc.cache[name] = logs
} else {
lc.cache = tmp[1:]
lc.cache[name] = logs[1:]
}
}
func (lc *logcache) Append(name string, log logBody) {
lc.append(name, log)
lc.append("all", log)
}

func (lc *logcache) GetCached() []logBody {
func (lc *logcache) GetCached(name string) []logBody {
lc.mutex.Lock()
defer lc.mutex.Unlock()
result := []logBody{}
for _, v := range lc.cache {
result = append(result, v)
result, ok := lc.cache[name]
if !ok {
return []logBody{}
}
return result
}

func (lc *logcache) GetNames() []string {
result := []string{}
for name := range lc.cache {
result = append(result, name)
}
return result
}
56 changes: 56 additions & 0 deletions api/distributor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package api

import socketio "github.com/googollee/go-socket.io"

var distributor = make(map[string][]socketio.Socket)

func AddSubscriber(name string, s socketio.Socket) {
ss, ok := distributor[name]
if !ok {
distributor[name] = []socketio.Socket{s}
}
distributor[name] = append(ss, s)
}

func FindSocket(so socketio.Socket) (string, bool) {
for name, ss := range distributor {
for _, s := range ss {
if s == so {
return name, true
}
}
}
return "", false
}
func RemoveSubscriber(so socketio.Socket) {
name, ok := FindSocket(so)
if !ok {
return
}
ss := distributor[name]
ts := make([]socketio.Socket, len(ss)-1)
j := 0
for _, v := range ss {
if v == so {
continue
}
ts[j] = v
j++
}
distributor[name] = ts
}

func Distribute(name string, log logBody) {
ss, ok := distributor[name]
if ok {
for _, s := range ss {
s.Emit("log:log", log)
}
}
ss, ok = distributor["all"]
if ok {
for _, s := range ss {
s.Emit("log:log", log)
}
}
}
16 changes: 10 additions & 6 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,20 @@ func (s *SoServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

func (s *SoServer) handler(so socketio.Socket) {
golog.Info("on connection.")
so.Join("log")
so.On("log:subscribe", func(msg string) {
cache := s.cache.GetCached()
so.On("log:subscribe", func(name string) {
if _, ok := FindSocket(so); ok {
RemoveSubscriber(so)
}
AddSubscriber(name, so)
cache := s.cache.GetCached(name)
for _, v := range cache {
so.Emit("log:log", v)
}
golog.Info("on subscribe", msg)
golog.Info("on subscribe: ", name)
})
so.On("disconnection", func() {
RemoveSubscriber(so)
golog.Info("on disconnect")
})
}
Expand Down Expand Up @@ -66,6 +70,6 @@ func (s *SoServer) SendLog(name, log string) {
Name: name,
Text: log,
}
s.cache.Append(l)
s.sosrv.BroadcastTo("log", "log:log", l)
s.cache.Append(name, l)
Distribute(name, l)
}
27 changes: 8 additions & 19 deletions public/css/base.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,6 @@ body {
padding: 30px;
}

a {
color: #4183c4;
text-decoration: none;
}

a:hover {
text-decoration: underline;
}

code {
background-color: #f8f8f8;
border: 1px solid #ddd;
border-radius: 3px;
font-family: "Bitstream Vera Sans Mono", Consolas, Courier, monospace;
font-size: 12px;
margin: 0 2px;
padding: 0 5px;
}

h1, h2, h3, h4 {
font-weight: bold;
margin: 0 0 15px;
Expand Down Expand Up @@ -57,6 +38,14 @@ p, ul {
ul {
padding-left: 30px;
}
#title{
flex-grow: 1;
}
.header {
display: flex;
justify-content: space-between;
align-items: center;
}

.log {
color: #f1f1f1;
Expand Down
5 changes: 3 additions & 2 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
<meta charset="utf-8">
<title>Neo Log Monitor</title>
<!-- Not present in the tutorial. Just for basic styling. -->
<link rel="stylesheet" href="css/base.css" />
<link href="css/base.css" rel="stylesheet"/>
<link href="node_modules/react-widgets/dist/css/react-widgets.css" rel="stylesheet"/>
<script src="https://unpkg.com/[email protected]/dist/react.js"></script>
<script src="https://unpkg.com/[email protected]/dist/react-dom.js"></script>
<script src="https://unpkg.com/[email protected]/babel.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/jquery.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/remarkable.min.js"></script>
<script src="https://cdn.socket.io/socket.io-1.2.1.js"></script>
<script src="node_modules/react-widgets/dist/react-widgets.js"></script>
</head>
<body>
<div id="content"></div>
Expand Down
1 change: 1 addition & 0 deletions public/node_modules/.bin/loose-envify

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions public/node_modules/@babel/runtime/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions public/node_modules/@babel/runtime/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

100 changes: 100 additions & 0 deletions public/node_modules/@babel/runtime/helpers/AsyncGenerator.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions public/node_modules/@babel/runtime/helpers/AwaitValue.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1dd84ec

Please sign in to comment.