|
| 1 | +package utils |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "os" |
| 7 | + |
| 8 | + "github.com/delta/codecharacter-lsp-2023/controllers" |
| 9 | + "github.com/delta/codecharacter-lsp-2023/models" |
| 10 | + "github.com/google/uuid" |
| 11 | + "github.com/gorilla/websocket" |
| 12 | + "github.com/labstack/echo/v4" |
| 13 | +) |
| 14 | + |
| 15 | +var upgrader = websocket.Upgrader{} |
| 16 | + |
| 17 | +func InitWebsocket(c echo.Context) error { |
| 18 | + var ws models.WebsocketConnection |
| 19 | + id := uuid.New() |
| 20 | + ws.ID = id |
| 21 | + language := c.Param("language") |
| 22 | + if language != "cpp" && language != "java" && language != "python" { |
| 23 | + return c.String(http.StatusBadRequest, "Invalid Language") |
| 24 | + } |
| 25 | + ws.Language = language |
| 26 | + fmt.Println("WS Connection Created with ID : ", id, " and Language : ", language) |
| 27 | + wsConn, err := upgrader.Upgrade(c.Response(), c.Request(), nil) |
| 28 | + if err != nil { |
| 29 | + return c.String(http.StatusBadRequest, "Error Upgrading to Websocket Connection") |
| 30 | + } |
| 31 | + ws.Connection = wsConn |
| 32 | + err = createWebsocketConnection(&ws) |
| 33 | + if err != nil { |
| 34 | + return c.String(http.StatusBadGateway, "Something went wrong, contact the event administrator.") |
| 35 | + } |
| 36 | + return nil |
| 37 | +} |
| 38 | + |
| 39 | +func dropConnection(ws *models.WebsocketConnection) { |
| 40 | + err := os.RemoveAll(ws.WorkspacePath) |
| 41 | + if err != nil { |
| 42 | + fmt.Println(err) |
| 43 | + } |
| 44 | + if ws.LSPServer != nil { |
| 45 | + err = ws.LSPServer.Process.Signal(os.Interrupt) |
| 46 | + if err != nil { |
| 47 | + fmt.Println(err) |
| 48 | + } |
| 49 | + // Reads process exit state to remove the <defunct> process from the system process table |
| 50 | + err = ws.LSPServer.Wait() |
| 51 | + if err != nil { |
| 52 | + fmt.Println(err) |
| 53 | + } |
| 54 | + } |
| 55 | + ws.Connection.Close() |
| 56 | + fmt.Println("Dropped WS Connection : ", ws.ID) |
| 57 | +} |
| 58 | + |
| 59 | +func createWebsocketConnection(ws *models.WebsocketConnection) error { |
| 60 | + defer dropConnection(ws) |
| 61 | + |
| 62 | + ws.WorkspacePath = "workspaces/" + ws.ID.String() |
| 63 | + err := os.Mkdir(ws.WorkspacePath, os.ModePerm) |
| 64 | + if err != nil { |
| 65 | + fmt.Println(err) |
| 66 | + return err |
| 67 | + } |
| 68 | + err = CreateLSPServer(ws) |
| 69 | + if err != nil { |
| 70 | + fmt.Println(err) |
| 71 | + return err |
| 72 | + } |
| 73 | + err = listen(ws) |
| 74 | + if err != nil { |
| 75 | + fmt.Println(err) |
| 76 | + return err |
| 77 | + } |
| 78 | + return nil |
| 79 | +} |
| 80 | + |
| 81 | +func listen(ws *models.WebsocketConnection) error { |
| 82 | + for { |
| 83 | + fmt.Println("Listening for Messages") |
| 84 | + _, messageBytes, err := ws.Connection.ReadMessage() |
| 85 | + if err != nil { |
| 86 | + if websocket.IsUnexpectedCloseError(err, websocket.CloseNormalClosure, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) { |
| 87 | + fmt.Println("WS Connection ", ws.ID, " closing with error : ", err) |
| 88 | + return err |
| 89 | + } |
| 90 | + return nil |
| 91 | + } |
| 92 | + err = controllers.HandleMessage(ws, messageBytes) |
| 93 | + if err != nil { |
| 94 | + fmt.Println(err) |
| 95 | + return err |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments