Skip to content

Commit

Permalink
Finished CC server
Browse files Browse the repository at this point in the history
It includes handling for heartbeat, init, and normal closure. It reads
the trigger data from cc-server.yaml.
  • Loading branch information
prairir committed Nov 25, 2021
1 parent 5619d71 commit 1a0583f
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cc-server/imacry-cc-server
4 changes: 2 additions & 2 deletions cc-server/cc-server.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# this file is filled with default values
# this file is filled with default values and explanations
---
# encryption/decryption password
password: "123456789012345678901234"
Expand All @@ -11,7 +11,7 @@ port: 80
# a AM/PM marker
# Example:
# 11:30AM
trigger-time: "1:30AM"
trigger-time: "4:00PM"
# length of trigger period
# time can be represented with its symbols
# Example:
Expand Down
2 changes: 0 additions & 2 deletions cc-server/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package cmd

import (
"fmt"

"github.com/apex/log"
"github.com/spf13/cobra"

Expand Down
2 changes: 1 addition & 1 deletion cc-server/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

type config struct {
Password string `mapstructure:"password"`
Port uint `mapstructure:"port"`
Port string `mapstructure:"port"`
TriggerTime time.Time `mapstructure:"trigger-time"`
TriggerLength time.Duration `mapstructure:"trigger-length"`
}
Expand Down
47 changes: 47 additions & 0 deletions cc-server/pkg/handler/heartbeat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package handler

import (
//"fmt"

"time"

"github.com/gorilla/websocket"
"github.com/prairir/imacry/cc-server/pkg/config"
)

// handler.HeartBeat: heart beat and event trigger logic. It reads from
// config.Config.TriggerTime and config.Config.TriggerLength to determine when to
// trigger the event. If the current time is within the trigger period then respond
// with a trigger bit else respond with non triggered bit
//
// response when triggered: `hb: 1`
// response when not triggered: `hb: 0`
//
// params: takes message and connection
// returns: nothing
func HeartBeat(message []byte, conn *websocket.Conn) {
// get now
now := time.Now()

// format the start time with todays year, month, and day
// but with the proper hour and minute
startTime := time.Date(
now.Year(),
now.Month(),
now.Day(),
config.Config.TriggerTime.Hour(),
config.Config.TriggerTime.Minute(),
0, 0, now.Location())

// end time is start + duration
endTime := startTime.Add(config.Config.TriggerLength)

// if current time is within trigger period
// trigger
// else dont
if now.After(startTime) && now.Before(endTime) {
conn.WriteMessage(websocket.TextMessage, []byte("hb: 1"))
} else {
conn.WriteMessage(websocket.TextMessage, []byte("hb: 0"))
}
}
16 changes: 16 additions & 0 deletions cc-server/pkg/handler/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package handler

import (
"fmt"

"github.com/gorilla/websocket"
"github.com/prairir/imacry/cc-server/pkg/config"
)

func Init(message []byte, conn *websocket.Conn) error {
err := conn.WriteMessage(websocket.TextMessage, []byte("pass: "+config.Config.Password))
if err != nil {
return fmt.Errorf("state.Init error: %w", err)
}
return nil
}
55 changes: 47 additions & 8 deletions cc-server/web/web.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,66 @@
package web

import (
"fmt"
"net/http"

"github.com/apex/log"
"github.com/gorilla/websocket"
"net/http"
"github.com/prairir/imacry/cc-server/pkg/config"
"github.com/prairir/imacry/cc-server/pkg/handler"
)

// default options
var upgrader = websocket.Upgrader{}

func initRoute(w http.ResponseWriter, r *http.Request) {
c, err := upgrader.Upgrade(w, r, nil)
fmt.Println("hello")
func wsHandler(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Errorf("init error: %s", err)
return
}
log.Infof("New Connection from %s", r.RemoteAddr)

// ws event loop dispatcher
for {
mt, message, err := conn.ReadMessage()
if err != nil {
log.Errorf("fatal read error: %s", err)
break
}

// if connection closes
if mt == websocket.CloseNormalClosure {
break
}

// init handler
if mt == websocket.TextMessage && string(message[:5]) == "init:" {
err = handler.Init(message, conn)
if err != nil {
log.Errorf("fatal init error: %s", err)
break
}
}

// Heart Beat handler
// Heart Beat doubles as a trigger event
if mt == websocket.TextMessage && string(message[:3]) == "hb:" {
handler.HeartBeat(message, conn)
}

}

// closing the websocket
err = conn.Close()
if err != nil {
log.Errorf("fatal closing error: %s", err)
}
defer c.Close()
}

func Run() {
http.HandleFunc("/init", initRoute)
http.HandleFunc("/", wsHandler)

log.Fatalf("%s", http.ListenAndServe(":80", nil))
log.Infof("Listen and Serve on port %s", config.Config.Port)
log.Fatalf("%s", http.ListenAndServe(":"+config.Config.Port, nil))

}

0 comments on commit 1a0583f

Please sign in to comment.