-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from ww24/manager
Manager
- Loading branch information
Showing
8 changed files
with
241 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
language: go | ||
go: | ||
- tip | ||
env: | ||
- "PATH=/home/travis/gopath/bin:$PATH" | ||
before_install: | ||
- go get github.com/mitchellh/gox | ||
- go get github.com/tcnksm/ghr | ||
- go get github.com/axw/gocov/gocov | ||
- go get github.com/mattn/goveralls | ||
- go get golang.org/x/tools/cmd/cover | ||
script: | ||
- go test -v -covermode=count -coverprofile=coverage.out | ||
- goveralls -coverprofile=coverage.out -service travis-ci -repotoken $COVERALLS_TOKEN | ||
before_deploy: | ||
- gox -output dist/{{.OS}}_{{.Arch}}/{{.Dir}} | ||
- find dist -name "*_*" -type d -exec cp -R static {} \; | ||
- find dist -name "*_*" -type d -exec zip -r {}".zip" {}"/" \; | ||
- zip -r all-in-one.zip dist/* | ||
- tar czf all-in-one.tar.gz dist/* | ||
deploy: | ||
provider: releases | ||
api_key: $GITHUB_TOKEN | ||
file: | ||
- all-in-one.zip | ||
- all-in-one.tar.gz | ||
skip_cleanup: true | ||
on: | ||
tags: true | ||
after_deploy: | ||
- ghr --username $(echo $TRAVIS_REPO_SLUG | awk 'BEGIN{FS="/"}{print$1}') --token $GITHUB_TOKEN --replace $TRAVIS_TAG dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package pham | ||
|
||
import ( | ||
"encoding/json" | ||
"log" | ||
"sync" | ||
) | ||
|
||
var ( | ||
manager = &ConnectionManager{ | ||
connections: make([]Connection, 0, 100), | ||
connMutex: new(sync.Mutex), | ||
connAdd: make(chan Connection, 1), | ||
connDel: make(chan Connection, 1), | ||
} | ||
) | ||
|
||
// NewConnectionManager is ConnectionManager constructor | ||
func NewConnectionManager() (connectionManager *ConnectionManager) { | ||
connectionManager = manager | ||
|
||
// watch add & delete event | ||
go func() { | ||
for { | ||
func() { | ||
select { | ||
case conn := <-manager.connAdd: | ||
log.Println("server: new connection") | ||
manager.connMutex.Lock() | ||
defer manager.connMutex.Unlock() | ||
|
||
manager.connections = append(manager.connections, conn) | ||
log.Println("connections:", len(manager.connections)) | ||
|
||
case conn := <-manager.connDel: | ||
log.Println("server: connection closed") | ||
manager.connMutex.Lock() | ||
defer manager.connMutex.Unlock() | ||
|
||
for i, ws := range manager.connections { | ||
if ws == conn { | ||
manager.connections = append(manager.connections[:i], manager.connections[i+1:]...) | ||
log.Println("connections:", len(manager.connections)) | ||
break | ||
} | ||
} | ||
} | ||
}() | ||
} | ||
}() | ||
|
||
return | ||
} | ||
|
||
// AddConnection add connection to connections | ||
func (manager *ConnectionManager) AddConnection(conn Connection) { | ||
manager.connAdd <- conn | ||
} | ||
|
||
// DelConnection delete connection from connections | ||
func (manager *ConnectionManager) DelConnection(conn Connection) { | ||
manager.connDel <- conn | ||
} | ||
|
||
// Broadcast message | ||
func (manager *ConnectionManager) Broadcast(data JSON) (length int, err error) { | ||
defer manager.connMutex.Unlock() | ||
manager.connMutex.Lock() | ||
|
||
// get connections length | ||
length = len(manager.connections) | ||
|
||
// encode json | ||
bytes, err := json.Marshal(data) | ||
if err != nil { | ||
return | ||
} | ||
|
||
// broadcast | ||
for _, connection := range manager.connections { | ||
err = connection.Send(bytes) | ||
if err != nil { | ||
return | ||
} | ||
} | ||
|
||
return | ||
} | ||
|
||
// ConnEach get connections safety | ||
func (manager *ConnectionManager) ConnEach(f func([]Connection)) { | ||
defer manager.connMutex.Unlock() | ||
manager.connMutex.Lock() | ||
f(manager.connections) | ||
} |
Oops, something went wrong.