diff --git a/Gopkg.toml b/Gopkg.toml index d649301..86d967a 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -14,3 +14,7 @@ name = "github.com/gorilla/websocket" version = "1.4.0" +[[constraint]] + name = "github.com/shirou/gopsutil" + version = "2.18.10" + diff --git a/cse/cse.go b/cse/cse.go index 1e5acc3..106b97a 100644 --- a/cse/cse.go +++ b/cse/cse.go @@ -8,6 +8,8 @@ import ( "cse-server-go/metadata" "cse-server-go/structs" "fmt" + "github.com/shirou/gopsutil/cpu" + "github.com/shirou/gopsutil/mem" "io/ioutil" "log" "os" @@ -232,43 +234,56 @@ func getModuleNames(metaData *structs.MetaData) []string { return modules } -func getModuleData(status *structs.SimulationStatus, metaData *structs.MetaData, observer *C.cse_observer) (module structs.Module) { +func getModuleData(observer *C.cse_observer, fmu structs.FMU) (module structs.Module) { + realSignals := observerGetReals(observer, fmu) + intSignals := observerGetIntegers(observer, fmu) + var signals []structs.Signal + signals = append(signals, realSignals...) + signals = append(signals, intSignals...) + module.Name = fmu.Name + module.Signals = signals + return +} + +func findModuleData(status *structs.SimulationStatus, metaData *structs.MetaData, observer *C.cse_observer) (module structs.Module) { if len(status.Module.Name) > 0 { for _, fmu := range metaData.FMUs { if fmu.Name == status.Module.Name { - realSignals := observerGetReals(observer, fmu) - intSignals := observerGetIntegers(observer, fmu) - var signals []structs.Signal - signals = append(signals, realSignals...) - signals = append(signals, intSignals...) - module.Name = fmu.Name - module.Signals = signals + module = getModuleData(observer, fmu) } } } + return +} + +func SimulationStatus(simulationStatus *structs.SimulationStatus, sim *Simulation) structs.JsonResponse { + virtualMemoryStats, _ := mem.VirtualMemory() + cpuInfo, _ := cpu.PerfInfo() + if simulationStatus.Loaded { + return structs.JsonResponse{ + SimulationTime: getSimulationTime(sim.Execution), + Modules: getModuleNames(&sim.MetaData), + Module: findModuleData(simulationStatus, &sim.MetaData, sim.Observer), + Loaded: simulationStatus.Loaded, + Status: simulationStatus.Status, + ConfigDir: simulationStatus.ConfigDir, + TrendSignals: simulationStatus.TrendSignals, + Memory: virtualMemoryStats, + Cpu: cpuInfo, + } + } else { + return structs.JsonResponse{ + Loaded: simulationStatus.Loaded, + Status: simulationStatus.Status, + } + } - return module } 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) } } diff --git a/layout.html b/layout.html deleted file mode 100644 index 8b9e3dc..0000000 --- a/layout.html +++ /dev/null @@ -1,4 +0,0 @@ -

CSE Simulator

-
- - \ No newline at end of file diff --git a/main.go b/main.go index f551252..4876540 100644 --- a/main.go +++ b/main.go @@ -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) } diff --git a/server/server.go b/server/server.go index 6f8ad3a..1e92753 100644 --- a/server/server.go +++ b/server/server.go @@ -1,34 +1,34 @@ package server import ( + "cse-server-go/cse" "cse-server-go/structs" - "html/template" - "net/http" + "encoding/json" + "github.com/gobuffalo/packr" "github.com/gorilla/mux" + "io/ioutil" "log" - "github.com/gobuffalo/packr" + "net/http" ) -type PageData struct { - PageTitle string - CseAnswer string -} - -var data = PageData{ - PageTitle: "CSE Hello World", - 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")) + box := packr.NewBox("../resources/public") - router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - tmpl.Execute(w, data) + router.Handle("/", http.FileServer(box)) + 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.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(box))) + router.HandleFunc("/command", func(w http.ResponseWriter, r *http.Request) { + body, _ := ioutil.ReadAll(r.Body) + commandRequest := []string{} + json.Unmarshal(body, &commandRequest) + command <- commandRequest + }).Methods("PUT") router.HandleFunc("/ws", WebsocketHandler(command, state)) diff --git a/structs/structs.go b/structs/structs.go index a6cd5a6..27c0a46 100644 --- a/structs/structs.go +++ b/structs/structs.go @@ -1,5 +1,10 @@ package structs +import ( + "github.com/shirou/gopsutil/cpu" + "github.com/shirou/gopsutil/mem" +) + type Signal struct { Name string `json:"name"` Causality string `json:"causality"` @@ -13,12 +18,14 @@ type Module struct { } type JsonResponse struct { - Loaded bool `json:"loaded"` - SimulationTime float64 `json:"time"` - ConfigDir string `json:"configDir,omitempty"` - Status string `json:"status,omitempty"` - Modules []string `json:"modules"` - Module Module `json:"module,omitempty"` + Loaded bool `json:"loaded"` + SimulationTime float64 `json:"time"` + ConfigDir string `json:"configDir,omitempty"` + Status string `json:"status,omitempty"` + Modules []string `json:"modules"` + Module Module `json:"module,omitempty"` + Memory *mem.VirtualMemoryStat + Cpu []cpu.Win32_PerfFormattedData_Counters_ProcessorInformation TrendSignals []TrendSignal `json:"trendSignals,omitempty"` }