func (server *TestWebSocketServer) createWebSocketHandler(containerId string) {
	testHandler := func(w http.ResponseWriter, r *http.Request) {
		ws, err := upgrader.Upgrade(w, r, nil)
		if err != nil {
			if _, ok := err.(websocket.HandshakeError); !ok {
				panic(err)
			}
			return
		}
		defer ws.Close()

		server.handlerWS = ws
		for {
			var streamEvent container.ProcessStreamEvent
			err := websocket.ReadJSON(ws, &streamEvent)
			if err != nil {
				continue
			}
			server.events = append(server.events, streamEvent)
			if streamEvent.MessageType == "run" {
				err = websocket.WriteJSON(ws, container.ProcessStreamEvent{
					MessageType: "pid",
					Data:        "5432",
				})
				if err != nil {
					panic(err)
				}
			}
		}
	}

	server.router.HandleFunc("/api/containers/"+containerId+"/run", testHandler)
}
Ejemplo n.º 2
0
func (c *connection) reader() {
	for {
		var message MData
		err := websocket.ReadJSON(c.ws, &message)
		if err != nil {
			break
		}
		// r.broadcast <- message
	}
	c.ws.Close()
}
Ejemplo n.º 3
0
func streamWebsocketIOFromContainerizer(ws *websocket.Conn, pidChannel chan<- string, processIO garden.ProcessIO) (int, error) {
	defer close(pidChannel)

	defer func() {
		ws.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))

		go func() {
			for {
				_, _, err := ws.NextReader()
				if err != nil {
					ws.Close()
					break
				}
			}
		}()

	}()

	receiveStream := ProcessStreamEvent{}
	for {
		err := websocket.ReadJSON(ws, &receiveStream)
		if err != nil {
			return -1, err
		}

		if receiveStream.MessageType == "pid" {
			pidChannel <- receiveStream.Data
		}

		if receiveStream.MessageType == "stdout" && processIO.Stdout != nil {
			io.WriteString(processIO.Stdout, receiveStream.Data)
		}
		if receiveStream.MessageType == "stderr" && processIO.Stderr != nil {
			io.WriteString(processIO.Stderr, receiveStream.Data)
		}

		if receiveStream.MessageType == "error" {
			return -1, errors.New(receiveStream.Data)
		}
		if receiveStream.MessageType == "close" {
			exitCode, err := strconv.Atoi(receiveStream.Data)
			if err != nil {
				return -1, err
			}
			return exitCode, nil
		}
	}
}
Ejemplo n.º 4
0
// ServeHTTP handles web requests
func (s LocationSocket) ServeHTTP(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
	conn, err := upgrader.Upgrade(w, r, nil)
	if err != nil {
		s.logger.Log("websocket connection open error:", err)
	}
	defer conn.Close()

	conn.SetReadLimit(maxMessageSize)
	conn.SetReadDeadline(time.Now().Add(pongWait))
	conn.SetPongHandler(func(string) error { conn.SetReadDeadline(time.Now().Add(pongWait)); return nil })

	id := mux.Vars(r)["id"]
	if id == "" || !bson.IsObjectIdHex(id) {
		conn.Close()
		s.logger.Log("LocationSocket ServeHTTP id is not a bson id.", err)
		json.NewEncoder(w).Encode(models.Response{ErrorCode: 5})
		return
	}

	dbConn := factory.NewDatabase().Connection()
	if err := dbConn.Open(); err != nil {
		s.logger.Log("LocationSocket ServeHTTP could not open db connection.", err)
		json.NewEncoder(w).Encode(models.Response{ErrorCode: 2})
		return
	}
	defer dbConn.Close()

	user, err := factory.NewDatabase().User(dbConn).FindByID(bson.ObjectIdHex(id))
	if err != nil || user == nil {
		s.logger.Log("LocationSocket ServeHTTP find by id.", err)
		json.NewEncoder(w).Encode(models.Response{ErrorCode: 3})
		return
	}

	s.addConnection(LocationInfo{ID: id, Name: user.Name, IconImage: user.Image.Icon, ThumbImage: user.Image.Thumb}, conn)

	for {
		model := models.GeoLocationModel{}
		if err := websocket.ReadJSON(conn, &model); err != nil {
			s.logger.Log("Conn read message error:", err)
			s.disconnect(conn)
			return
		}

		s.sendLocationMessage(model, s.connections[conn])
	}
}
Ejemplo n.º 5
0
func streamWebsocketIOFromContainerizer(ws *websocket.Conn, pidChannel chan<- uint32, processIO garden.ProcessIO) (int, error) {
	defer close(pidChannel)
	defer ws.Close()

	receiveStream := ProcessStreamEvent{}
	for {
		err := websocket.ReadJSON(ws, &receiveStream)
		if err != nil {
			return -1, err
		}

		if receiveStream.MessageType == "pid" {
			pid, err := strconv.ParseInt(receiveStream.Data, 10, 32)
			if err != nil {
				return -1, err
			}
			pidChannel <- uint32(pid)
		}

		if receiveStream.MessageType == "stdout" && processIO.Stdout != nil {
			io.WriteString(processIO.Stdout, receiveStream.Data)
		}
		if receiveStream.MessageType == "stderr" && processIO.Stderr != nil {
			io.WriteString(processIO.Stderr, receiveStream.Data)
		}

		if receiveStream.MessageType == "error" {
			return -1, errors.New(receiveStream.Data)
		}
		if receiveStream.MessageType == "close" {
			exitCode, err := strconv.Atoi(receiveStream.Data)
			if err != nil {
				return -1, err
			}
			return exitCode, nil
		}
	}
}