Skip to content

Commit

Permalink
Merge pull request #2 from MysteriumNetwork/feature/node-pings-api
Browse files Browse the repository at this point in the history
API client for sending stats
  • Loading branch information
Waldz authored May 4, 2017
2 parents 1eaf0b8 + daefdaf commit 357f63b
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 7 deletions.
15 changes: 12 additions & 3 deletions cmd/mysterium_server/command_run/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ import (
"github.com/mysterium/node/ipify"
"github.com/mysterium/node/openvpn"
"github.com/mysterium/node/server"
"github.com/mysterium/node/server/dto"
"io"
"time"
)

type commandRun struct {
output io.Writer
outputError io.Writer

ipifyClient ipify.Client
ipifyClient ipify.Client
mysteriumClient server.Client
vpnServer *openvpn.Server
vpnServer *openvpn.Server
}

func (cmd *commandRun) Run(options CommandOptions) error {
Expand All @@ -38,6 +40,13 @@ func (cmd *commandRun) Run(options CommandOptions) error {
return err
}

go func() {
for {
time.Sleep(1 * time.Minute)
cmd.mysteriumClient.NodeSendStats(options.NodeKey, []dto.SessionStats{})
}
}()

vpnServerConfig := openvpn.NewServerConfig(
"10.8.0.0", "255.255.255.0",
options.DirectoryConfig+"/ca.crt",
Expand All @@ -61,4 +70,4 @@ func (cmd *commandRun) Wait() error {

func (cmd *commandRun) Kill() {
cmd.vpnServer.Stop()
}
}
12 changes: 9 additions & 3 deletions server/client_fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package server
import (
"github.com/mysterium/node/server/dto"

log "github.com/cihub/seelog"
"fmt"
log "github.com/cihub/seelog"
)

func NewClientFake() Client {
Expand All @@ -24,15 +24,21 @@ func (client *clientFake) NodeRegister(nodeKey, connectionConfig string) (err er
return nil
}

func (client *clientFake) NodeSendStats(nodeKey string, sessionStats []dto.SessionStats) (err error) {
log.Info(MYSTERIUM_API_LOG_PREFIX, "Node stats sent: ", nodeKey)

return nil
}

func (client *clientFake) SessionCreate(nodeKey string) (session dto.Session, err error) {
if connectionConfig, ok := client.connectionConfigByNode[nodeKey]; ok {
session = dto.Session{
Id: nodeKey + "-session",
Id: nodeKey + "-session",
ConnectionConfig: connectionConfig,
}
return
}

err = fmt.Errorf("Fake node not found: %s", nodeKey)
return
}
}
13 changes: 13 additions & 0 deletions server/client_rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ func (client *clientRest) NodeRegister(nodeKey, connectionConfig string) (err er
return
}

func (client *clientRest) NodeSendStats(nodeKey string, sessionList []dto.SessionStats) (err error) {
response, err := client.doRequest("POST", "node_send_stats", dto.NodeStatsRequest{
NodeKey: nodeKey,
Sessions: sessionList,
})
if err == nil {
defer response.Body.Close()
log.Info(MYSTERIUM_API_LOG_PREFIX, "Node stats sent: ", nodeKey)
}

return nil
}

func (client *clientRest) doRequest(method string, path string, payload interface{}) (*http.Response, error) {
payloadJson, err := json.Marshal(payload)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions server/dto/node_stats_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package dto

type NodeStatsRequest struct {
NodeKey string `json:"node_key"`
Sessions []SessionStats `json:"sessions"`
}
7 changes: 7 additions & 0 deletions server/dto/session_stats.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dto

type SessionStats struct {
Id string `json:"session_key"`
BytesSent int `json:"bytes_sent"`
BytesReceived int `json:"bytes_received"`
}
3 changes: 2 additions & 1 deletion server/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ import "github.com/mysterium/node/server/dto"

type Client interface {
NodeRegister(nodeKey, connectionConfig string) (err error)
NodeSendStats(nodeKey string, sessionStats []dto.SessionStats) (err error)
SessionCreate(nodeKey string) (session dto.Session, err error)
}
}

0 comments on commit 357f63b

Please sign in to comment.