// readConnectionPump pumps messages from an individual websocket connection to the dispatcher func (proxy *ProxyConnection) readConnectionPump(sock *NamedWebSocket) { defer func() { proxy.removeConnection(sock) }() proxy.ws.SetReadLimit(maxMessageSize) proxy.ws.SetReadDeadline(time.Now().Add(pongWait)) proxy.ws.SetPongHandler(func(string) error { proxy.ws.SetReadDeadline(time.Now().Add(pongWait)); return nil }) for { opCode, buf, err := proxy.ws.ReadMessage() if err != nil || opCode != websocket.TextMessage { break } var message ProxyWireMessage err = json.Unmarshal(buf, &message) if err != nil { continue } switch message.Action { case "connect": proxy.peers[message.Target] = true // Inform all control connections that this proxy owns this peer connection for _, control := range sock.controllers { control.send("connect", control.id, message.Target, "") } case "disconnect": delete(proxy.peers, message.Target) // Inform all control connections that this proxy no longer owns this peer connection for _, control := range sock.controllers { control.send("disconnect", control.id, message.Target, "") } case "message": // broadcast message on to given target wsBroadcast := &Message{ source: message.Source, target: 0, // target all connections payload: message.Payload, fromProxy: true, } //log.Printf("Received Proxy Message: %s",message.Payload) var notiRecived bool = false for e := proxyNotifiedList.Front(); e != nil; e = e.Next() { if message.Payload == e.Value.(string) { notiRecived = true break } } //create notifications with the url for linux and android if strings.Contains(message.Payload, "http://") && !notiRecived { proxyNotifiedList.PushBack(message.Payload) notify = notificator.New(notificator.Options{ DefaultIcon: "Mediascape.png", AppName: "Mediascape", }) if string(runtime.GOOS) == "linux" && string(runtime.GOARCH) != "arm" { notify.Push("Mediascape", message.Payload, "Mediascape.png") } else if string(runtime.GOOS) == "linux" && string(runtime.GOARCH) == "arm" { completUrl := fmt.Sprintf("http://localhost:8182/discoveragent/notification?url=%s", message.Payload) response, err := http.Get(completUrl) if err != nil { log.Printf("Error: %s", err) } else { defer response.Body.Close() } } } sock.broadcastBuffer <- wsBroadcast case "directmessage": messageSent := false // Relay message to control channel that matches target for _, control := range sock.controllers { if control.id == message.Target { control.send("message", message.Source, message.Target, message.Payload) messageSent = true break } } if !messageSent { fmt.Errorf("P2P message target could not be found. Not sent.") } } } }
// readConnectionPump pumps messages from an individual websocket connection to the dispatcher func (peer *PeerConnection) readConnectionPump(sock *NamedWebSocket) { defer func() { peer.removeConnection(sock) }() peer.ws.SetReadLimit(maxMessageSize) peer.ws.SetReadDeadline(time.Now().Add(pongWait)) peer.ws.SetPongHandler(func(string) error { peer.ws.SetReadDeadline(time.Now().Add(pongWait)); return nil }) for { opCode, message, err := peer.ws.ReadMessage() if err != nil || opCode != websocket.TextMessage { break } //log.Printf("Received Peer Message: %s",message) wsBroadcast := &Message{ source: peer.id, target: 0, // target all connections payload: string(message), fromProxy: false, } var notiRecived bool = false for e := peerNotifiedList.Front(); e != nil; e = e.Next() { if string(message) == e.Value.(string) { notiRecived = true } } //create notifications with the url for linux and android if strings.Contains(string(message), "http://") && !notiRecived { peerNotifiedList.PushBack(string(message)) proxyNotifiedList.PushBack(string(message)) if peer.id != sock.controllers[0].id { path, err2 := filepath.Abs("Mediascape.png") if err2 != nil { log.Fatal(err) } //log.Printf("Notification %s", path) notify = notificator.New(notificator.Options{ DefaultIcon: path, AppName: "Mediascape", }) if string(runtime.GOOS) == "linux" && string(runtime.GOARCH) != "arm" { notify.Push("Mediascape", string(message), path) } else if string(runtime.GOOS) == "linux" && string(runtime.GOARCH) == "arm" { completUrl := fmt.Sprintf("http://localhost:8182/discoveryagent/notification?url=%s", string(message)) response, err := http.Get(completUrl) if err != nil { // handle error log.Printf("Error: %s", err) } else { defer response.Body.Close() } } } } sock.broadcastBuffer <- wsBroadcast } }