summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/mrexodia
diff options
context:
space:
mode:
authorWim <wim@42.be>2016-09-04 20:03:07 +0200
committerWim <wim@42.be>2016-09-04 20:04:43 +0200
commit12389d602eecf4adab0e394da19b2b808cb489b1 (patch)
tree80d6d8c00ed31fdcd60540d7c65afa6e7d2d83d6 /vendor/github.com/mrexodia
parent44144587a0314b7e2c719d279116ac86b657657e (diff)
downloadmatterbridge-msglm-12389d602eecf4adab0e394da19b2b808cb489b1.tar.gz
matterbridge-msglm-12389d602eecf4adab0e394da19b2b808cb489b1.tar.bz2
matterbridge-msglm-12389d602eecf4adab0e394da19b2b808cb489b1.zip
Add Gitter support
Diffstat (limited to 'vendor/github.com/mrexodia')
-rw-r--r--vendor/github.com/mrexodia/wray/examples/client.go17
-rw-r--r--vendor/github.com/mrexodia/wray/examples/publish.go15
-rw-r--r--vendor/github.com/mrexodia/wray/go_faye.go140
-rw-r--r--vendor/github.com/mrexodia/wray/http_transport.go55
-rw-r--r--vendor/github.com/mrexodia/wray/response.go61
-rw-r--r--vendor/github.com/mrexodia/wray/schedular.go22
-rw-r--r--vendor/github.com/mrexodia/wray/transport.go21
-rw-r--r--vendor/github.com/mrexodia/wray/utils.go10
8 files changed, 341 insertions, 0 deletions
diff --git a/vendor/github.com/mrexodia/wray/examples/client.go b/vendor/github.com/mrexodia/wray/examples/client.go
new file mode 100644
index 00000000..50edf446
--- /dev/null
+++ b/vendor/github.com/mrexodia/wray/examples/client.go
@@ -0,0 +1,17 @@
+package main
+
+import "github.com/pythonandchips/wray"
+import "fmt"
+
+func main() {
+ wray.RegisterTransports([]wray.Transport{&wray.HttpTransport{}})
+ client := wray.NewFayeClient("http://localhost:5000/faye")
+
+ fmt.Println("subscribing")
+ client.Subscribe("/foo", false, func(message wray.Message) {
+ fmt.Println("-------------------------------------------")
+ fmt.Println(message.Data)
+ })
+
+ client.Listen()
+}
diff --git a/vendor/github.com/mrexodia/wray/examples/publish.go b/vendor/github.com/mrexodia/wray/examples/publish.go
new file mode 100644
index 00000000..9a08a376
--- /dev/null
+++ b/vendor/github.com/mrexodia/wray/examples/publish.go
@@ -0,0 +1,15 @@
+package main
+
+import "github.com/pythonandchips/wray"
+import "fmt"
+
+func main() {
+ wray.RegisterTransports([]wray.Transport{ &gofaye.HttpTransport{} })
+ client := wray.NewFayeClient("http://localhost:5000/faye")
+
+ params := map[string]interface{}{"hello": "from golang"}
+ fmt.Println("sending")
+ client.Publish("/foo", params)
+}
+
+
diff --git a/vendor/github.com/mrexodia/wray/go_faye.go b/vendor/github.com/mrexodia/wray/go_faye.go
new file mode 100644
index 00000000..ab78eed7
--- /dev/null
+++ b/vendor/github.com/mrexodia/wray/go_faye.go
@@ -0,0 +1,140 @@
+package wray
+
+import (
+ "fmt"
+ "path/filepath"
+ "strings"
+ "time"
+)
+
+const (
+ UNCONNECTED = 1
+ CONNECTING = 2
+ CONNECTED = 3
+ DISCONNECTED = 4
+
+ HANDSHAKE = "handshake"
+ RETRY = "retry"
+ NONE = "none"
+
+ CONNECTION_TIMEOUT = 60.0
+ DEFAULT_RETRY = 5.0
+ MAX_REQUEST_SIZE = 2048
+)
+
+var (
+ MANDATORY_CONNECTION_TYPES = []string{"long-polling"}
+ registeredTransports = []Transport{}
+)
+
+type FayeClient struct {
+ state int
+ url string
+ subscriptions []Subscription
+ transport Transport
+ clientId string
+ schedular Schedular
+}
+
+type Subscription struct {
+ channel string
+ callback func(Message)
+}
+
+type SubscriptionPromise struct {
+ subscription Subscription
+}
+
+func NewFayeClient(url string) *FayeClient {
+ schedular := ChannelSchedular{}
+ client := &FayeClient{url: url, state: UNCONNECTED, schedular: schedular}
+ return client
+}
+
+func (self *FayeClient) handshake() {
+ t, err := SelectTransport(self, MANDATORY_CONNECTION_TYPES, []string{})
+ if err != nil {
+ panic("No usable transports available")
+ }
+ self.transport = t
+ self.transport.setUrl(self.url)
+ self.state = CONNECTING
+ handshakeParams := map[string]interface{}{"channel": "/meta/handshake",
+ "version": "1.0",
+ "supportedConnectionTypes": []string{"long-polling"}}
+ response, err := self.transport.send(handshakeParams)
+ if err != nil {
+ fmt.Println("Handshake failed. Retry in 10 seconds")
+ self.state = UNCONNECTED
+ self.schedular.wait(10*time.Second, func() {
+ fmt.Println("retying handshake")
+ self.handshake()
+ })
+ return
+ }
+ self.clientId = response.clientId
+ self.state = CONNECTED
+ self.transport, err = SelectTransport(self, response.supportedConnectionTypes, []string{})
+ if err != nil {
+ panic("Server does not support any available transports. Supported transports: " + strings.Join(response.supportedConnectionTypes, ","))
+ }
+}
+
+func (self *FayeClient) Subscribe(channel string, force bool, callback func(Message)) SubscriptionPromise {
+ if self.state == UNCONNECTED {
+ self.handshake()
+ }
+ subscriptionParams := map[string]interface{}{"channel": "/meta/subscribe", "clientId": self.clientId, "subscription": channel, "id": "1"}
+ subscription := Subscription{channel: channel, callback: callback}
+ //TODO: deal with subscription failures
+ self.transport.send(subscriptionParams)
+ self.subscriptions = append(self.subscriptions, subscription)
+ return SubscriptionPromise{subscription}
+}
+
+func (self *FayeClient) handleResponse(response Response) {
+ for _, message := range response.messages {
+ for _, subscription := range self.subscriptions {
+ matched, _ := filepath.Match(subscription.channel, message.Channel)
+ if matched {
+ go subscription.callback(message)
+ }
+ }
+ }
+}
+
+func (self *FayeClient) connect() {
+ connectParams := map[string]interface{}{"channel": "/meta/connect", "clientId": self.clientId, "connectionType": self.transport.connectionType()}
+ responseChannel := make(chan Response)
+ go func() {
+ response, _ := self.transport.send(connectParams)
+ responseChannel <- response
+ }()
+ self.listen(responseChannel)
+}
+
+func (self *FayeClient) listen(responseChannel chan Response) {
+ response := <-responseChannel
+ if response.successful == true {
+ go self.handleResponse(response)
+ } else {
+ }
+}
+
+func (self *FayeClient) Listen() {
+ for {
+ self.connect()
+ }
+}
+
+func (self *FayeClient) Publish(channel string, data map[string]interface{}) {
+ if self.state != CONNECTED {
+ self.handshake()
+ }
+ publishParams := map[string]interface{}{"channel": channel, "data": data, "clientId": self.clientId}
+ self.transport.send(publishParams)
+}
+
+func RegisterTransports(transports []Transport) {
+ registeredTransports = transports
+}
diff --git a/vendor/github.com/mrexodia/wray/http_transport.go b/vendor/github.com/mrexodia/wray/http_transport.go
new file mode 100644
index 00000000..f9465832
--- /dev/null
+++ b/vendor/github.com/mrexodia/wray/http_transport.go
@@ -0,0 +1,55 @@
+package wray
+
+import (
+ "bytes"
+ "encoding/json"
+ "errors"
+ "io/ioutil"
+ "net/http"
+ "net/url"
+)
+
+type HttpTransport struct {
+ url string
+ SendHook func(data map[string]interface{})
+}
+
+func (self HttpTransport) isUsable(clientUrl string) bool {
+ parsedUrl, err := url.Parse(clientUrl)
+ if err != nil {
+ return false
+ }
+ if parsedUrl.Scheme == "http" || parsedUrl.Scheme == "https" {
+ return true
+ }
+ return false
+}
+
+func (self HttpTransport) connectionType() string {
+ return "long-polling"
+}
+
+func (self HttpTransport) send(data map[string]interface{}) (Response, error) {
+ if self.SendHook != nil {
+ self.SendHook(data)
+ }
+ dataBytes, _ := json.Marshal(data)
+ buffer := bytes.NewBuffer(dataBytes)
+ responseData, err := http.Post(self.url, "application/json", buffer)
+ if err != nil {
+ return Response{}, err
+ }
+ if responseData.StatusCode != 200 {
+ return Response{}, errors.New(responseData.Status)
+ }
+ readData, _ := ioutil.ReadAll(responseData.Body)
+ responseData.Body.Close()
+ var jsonData []interface{}
+ json.Unmarshal(readData, &jsonData)
+ response := newResponse(jsonData)
+ return response, nil
+}
+
+func (self *HttpTransport) setUrl(url string) {
+ self.url = url
+}
diff --git a/vendor/github.com/mrexodia/wray/response.go b/vendor/github.com/mrexodia/wray/response.go
new file mode 100644
index 00000000..e9815c3f
--- /dev/null
+++ b/vendor/github.com/mrexodia/wray/response.go
@@ -0,0 +1,61 @@
+package wray
+
+type Response struct {
+ id string
+ channel string
+ successful bool
+ clientId string
+ supportedConnectionTypes []string
+ messages []Message
+ error error
+}
+
+type Message struct {
+ Channel string
+ Id string
+ Data map[string]interface{}
+}
+
+func newResponse(data []interface{}) Response {
+ headerData := data[0].(map[string]interface{})
+ messagesData := data[1.:]
+ messages := parseMessages(messagesData)
+ var id string
+ if headerData["id"] != nil {
+ id = headerData["id"].(string)
+ }
+ supportedConnectionTypes := []string{}
+ if headerData["supportedConnectionTypes"] != nil {
+ d := headerData["supportedConnectionTypes"].([]interface{})
+ for _, sct := range(d) {
+ supportedConnectionTypes = append(supportedConnectionTypes, sct.(string))
+ }
+ }
+ var clientId string
+ if headerData["clientId"] != nil {
+ clientId = headerData["clientId"].(string)
+ }
+ return Response{id: id,
+ clientId: clientId,
+ channel: headerData["channel"].(string),
+ successful: headerData["successful"].(bool),
+ messages: messages,
+ supportedConnectionTypes: supportedConnectionTypes}
+}
+
+func parseMessages(data []interface{}) []Message {
+ messages := []Message{}
+ for _, messageData := range(data) {
+ m := messageData.(map[string]interface{})
+ var id string
+ if m["id"] != nil {
+ id = m["id"].(string)
+ }
+ message := Message{Channel: m["channel"].(string),
+ Id: id,
+ Data: m["data"].(map[string]interface{})}
+ messages = append(messages, message)
+ }
+ return messages
+}
+
diff --git a/vendor/github.com/mrexodia/wray/schedular.go b/vendor/github.com/mrexodia/wray/schedular.go
new file mode 100644
index 00000000..3453fd61
--- /dev/null
+++ b/vendor/github.com/mrexodia/wray/schedular.go
@@ -0,0 +1,22 @@
+package wray
+
+import "time"
+
+type Schedular interface {
+ wait(time.Duration, func())
+ delay() time.Duration
+}
+
+type ChannelSchedular struct {
+}
+
+func (self ChannelSchedular) wait(delay time.Duration, callback func()) {
+ go func() {
+ time.Sleep(delay)
+ callback()
+ }()
+}
+
+func (self ChannelSchedular) delay() time.Duration {
+ return (1 * time.Minute)
+}
diff --git a/vendor/github.com/mrexodia/wray/transport.go b/vendor/github.com/mrexodia/wray/transport.go
new file mode 100644
index 00000000..82f4d4d3
--- /dev/null
+++ b/vendor/github.com/mrexodia/wray/transport.go
@@ -0,0 +1,21 @@
+package wray
+
+import (
+ "errors"
+)
+
+type Transport interface {
+ isUsable(string) bool
+ connectionType() string
+ send(map[string]interface{}) (Response, error)
+ setUrl(string)
+}
+
+func SelectTransport(client *FayeClient, transportTypes []string, disabled []string) (Transport, error) {
+ for _, transport := range registeredTransports {
+ if contains(transport.connectionType(), transportTypes) && transport.isUsable(client.url) {
+ return transport, nil
+ }
+ }
+ return nil, errors.New("No usable transports available")
+}
diff --git a/vendor/github.com/mrexodia/wray/utils.go b/vendor/github.com/mrexodia/wray/utils.go
new file mode 100644
index 00000000..c587f7c6
--- /dev/null
+++ b/vendor/github.com/mrexodia/wray/utils.go
@@ -0,0 +1,10 @@
+package wray
+
+func contains(target string, slice []string) bool {
+ for _, t := range(slice) {
+ if t == target {
+ return true
+ }
+ }
+ return false
+}