Skip to content

Commit

Permalink
#28 Same status on REST as on socket
Browse files Browse the repository at this point in the history
  • Loading branch information
INGSOL committed Nov 8, 2018
1 parent 082bca9 commit a941a0f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
37 changes: 21 additions & 16 deletions cse/cse.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,25 +259,30 @@ func getModuleData(status *structs.SimulationStatus, metaData *structs.MetaData,
return module
}

func SimulationStatus(simulationStatus *structs.SimulationStatus, sim *Simulation) structs.JsonResponse {
if simulationStatus.Loaded {
return structs.JsonResponse{
SimulationTime: getSimulationTime(sim.Execution),
Modules: getModuleNames(&sim.MetaData),
Module: getModuleData(simulationStatus, &sim.MetaData, sim.Observer),
Loaded: simulationStatus.Loaded,
Status: simulationStatus.Status,
ConfigDir: simulationStatus.ConfigDir,
TrendSignals: simulationStatus.TrendSignals,
}
} else {
return structs.JsonResponse{
Loaded: simulationStatus.Loaded,
Status: simulationStatus.Status,
}
}

}

func StateUpdateLoop(state chan structs.JsonResponse, simulationStatus *structs.SimulationStatus, sim *Simulation) {

for {
if simulationStatus.Loaded {
state <- structs.JsonResponse{
SimulationTime: getSimulationTime(sim.Execution),
Modules: getModuleNames(&sim.MetaData),
Module: getModuleData(simulationStatus, &sim.MetaData, sim.Observer),
Loaded: simulationStatus.Loaded,
Status: simulationStatus.Status,
ConfigDir: simulationStatus.ConfigDir,
TrendSignals: simulationStatus.TrendSignals,
}
} else {
state <- structs.JsonResponse{
Loaded: simulationStatus.Loaded,
Status: simulationStatus.Status,
}
}
state <- SimulationStatus(simulationStatus, sim)
time.Sleep(1000 * time.Millisecond)
}
}
Expand Down
10 changes: 5 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ func main() {
cmd := make(chan []string, 10)
state := make(chan structs.JsonResponse, 10)

simulationStatus := &structs.SimulationStatus{
simulationStatus := structs.SimulationStatus{
Loaded: false,
Status: "stopped",
TrendSignals: []structs.TrendSignal{},
}

// Passing the channel to the go routine
go cse.StateUpdateLoop(state, simulationStatus, &sim)
go cse.CommandLoop(&sim, cmd, simulationStatus)
go cse.TrendLoop(&sim, simulationStatus)
go cse.StateUpdateLoop(state, &simulationStatus, &sim)
go cse.CommandLoop(&sim, cmd, &simulationStatus)
go cse.TrendLoop(&sim, &simulationStatus)

//Passing the channel to the server
server.Server(cmd, state)
server.Server(cmd, state, &simulationStatus, &sim)
close(cmd)
}
9 changes: 8 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package server

import (
"cse-server-go/cse"
"cse-server-go/structs"
"encoding/json"
"html/template"
"net/http"
"github.com/gorilla/mux"
Expand All @@ -19,7 +21,7 @@ var data = PageData{
CseAnswer: "",
}

func Server(command chan []string, state chan structs.JsonResponse) {
func Server(command chan []string, state chan structs.JsonResponse, simulationStatus *structs.SimulationStatus, sim *cse.Simulation) {
router := mux.NewRouter()
box := packr.NewBox("./resources/public")
tmpl := template.Must(template.ParseFiles("layout.html"))
Expand All @@ -30,6 +32,11 @@ func Server(command chan []string, state chan structs.JsonResponse) {

router.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(box)))

router.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(cse.SimulationStatus(simulationStatus, sim))
})

router.HandleFunc("/ws", WebsocketHandler(command, state))

log.Fatal(http.ListenAndServe(":8000", router))
Expand Down

0 comments on commit a941a0f

Please sign in to comment.