Skip to content

Commit

Permalink
- services architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
ryaben17 committed Apr 2, 2021
1 parent 19e048e commit c3600f5
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 59 deletions.
46 changes: 0 additions & 46 deletions riotpot/echod/echo.go

This file was deleted.

131 changes: 131 additions & 0 deletions riotpot/echod/echod.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package main

import (
"bufio"
"fmt"
"net"
"riotpot/services"
"riotpot/utils/errors"
)

var Name string

func init() {
Name = "Echod"
}

func Echod() services.Service {
return &Echo{
name: Name,
// the natural port for `echo` is 7
port: 7,
}
}

type Echo struct {
id int
name string
port int
stop chan int
}

func (e *Echo) Init(map[string]interface{}) {}

func (e *Echo) Run() error {
var err error

// convert the port number to a string that we can use in the server
var port = fmt.Sprintf(":%b", e.port)

// start a service in the `echo` port
listener, err := net.Listen("tcp", port)
errors.Raise(err)

// build a channel stack to receive connections to the service
conn := make(chan net.Conn)
go e.serve(conn, listener)

// handle the connections from the channel
e.handlePool(conn)

return err
}

// Open the service and listen for connections
// inspired on https://gist.github.com/paulsmith/775764#file-echo-go
func (e *Echo) serve(ch chan net.Conn, listener net.Listener) {
// open an infinite loop to receive connections
for {
// Accept the client connection
client, err := listener.Accept()
errors.Raise(err)

// push the client connection to the channel
ch <- client
}
}

// Handle the pool of connections to the service
func (e *Echo) handlePool(ch chan net.Conn) {
// open an infinite loop to handle the connections
for {
// while the `stop` channel remains empty, continue handling
// new connections.
select {
case conn := <-ch:
// use one goroutine per connection.
go e.handleConn(conn)
case <-e.stop:
fmt.Printf("[x] Stopping %s service...", e.name)
return
}
}
}

// Handle a connection made to the service
func (e *Echo) handleConn(conn net.Conn) {
//opens a new small buffer
br := bufio.NewReader(conn)

for {
// Read the message sent from the client.
msg, err := br.ReadBytes('\n')
errors.Raise(err)

// save the connection in the database
e.save(conn.RemoteAddr(), msg)
// Respond with the same message
conn.Write(msg)
}
}

func (e *Echo) save(address net.Addr, msg []byte) {
// TODO: add logic here to store the connections made
// to the server
fmt.Printf("Bip Bop... \nSaving connection:\n> address: %v\n> msg: %v", address, msg)
}

func (e *Echo) Stop() error {
var err error
// send a stop signal to the channel
e.stop <- 1
return err
}

func (e *Echo) Restart() error {
var err error
return err
}

func (e *Echo) Status() error {
var err error
return err
}

func (e *Echo) Logger(ch chan<- error) (services.Logger, error) {
var (
logger services.Logger
err error
)
return logger, err
}
19 changes: 19 additions & 0 deletions riotpot/echod/echod_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"testing"
)

func TestEcho(t *testing.T) {
// testing of the echo service
service := Echod()

// send the message to the server by typing in another terminal
// $ nc localhost 7
t.Log("For this test you will need a packet capture tool !!")
t.Log("Please run the following command in another terminal:")
t.Log("$ nc localhost 7")

// run the Echo host in a blocking thread until the test is interrupted
service.Run()
}
1 change: 1 addition & 0 deletions riotpot/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be // indirect
github.com/gliderlabs/ssh v0.3.2
github.com/kr/pty v1.1.8
github.com/labstack/gommon v0.3.0
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83 // indirect
golang.org/x/sys v0.0.0-20210304152209-afaa3650a925 // indirect
gopkg.in/yaml.v2 v2.4.0
Expand Down
18 changes: 18 additions & 0 deletions riotpot/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,40 @@ github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFI
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4=
github.com/creack/pty v1.1.7 h1:6pwm8kMQKCmgUg0ZHTm5+/YvRK0s3THD/28+T6/kk4A=
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gliderlabs/ssh v0.3.2 h1:gcfd1Aj/9RQxvygu4l3sak711f/5+VOwBw9C/7+N4EI=
github.com/gliderlabs/ssh v0.3.2/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
github.com/kr/pty v1.1.8 h1:AkaSdXYQOWeaO3neb8EM634ahkXXe3jYbVh/F9lq+GI=
github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw=
github.com/labstack/gommon v0.3.0 h1:JEeO0bvc78PKdyHxloTKiF8BD5iGrH8T6MSeGvSgob0=
github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k=
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.9 h1:d5US/mDsogSGW37IV293h//ZFaeajb69h+EHFsv2xGg=
github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.0.1 h1:tY9CJiPnMXf1ERmG2EyK7gNUd+c6RKGD0IfU8WdUSz8=
github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83 h1:/ZScEX8SfEmUGRHs0gxpqteO5nfNW6axyZbBdw9A12g=
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210304152209-afaa3650a925 h1:Ee/Y8w57dY5pI4wYh0ZdFQn++NMCNRNIOKXCZ/82iUM=
golang.org/x/sys v0.0.0-20210304152209-afaa3650a925/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221 h1:/ZHdbVpdR/jk3g30/d4yUL0JU9kksj8+F/bnQUVLGDM=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
Expand Down
39 changes: 39 additions & 0 deletions riotpot/services/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package services

import (
"io"

"github.com/labstack/gommon/log"
)

// Logger writes to the system log.
type Logger interface {
Output() io.Writer
SetOutput(w io.Writer)
Prefix() string
SetPrefix(p string)
Level() log.Lvl
SetLevel(v log.Lvl)
SetHeader(h string)
Print(i ...interface{})
Printf(format string, args ...interface{})
Printj(j log.JSON)
Debug(i ...interface{})
Debugf(format string, args ...interface{})
Debugj(j log.JSON)
Info(i ...interface{})
Infof(format string, args ...interface{})
Infoj(j log.JSON)
Warn(i ...interface{})
Warnf(format string, args ...interface{})
Warnj(j log.JSON)
Error(i ...interface{})
Errorf(format string, args ...interface{})
Errorj(j log.JSON)
Fatal(i ...interface{})
Fatalj(j log.JSON)
Fatalf(format string, args ...interface{})
Panic(i ...interface{})
Panicj(j log.JSON)
Panicf(format string, args ...interface{})
}
17 changes: 5 additions & 12 deletions riotpot/services/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ func getServicePlugin(path string) Service {
pg, err := plugin.Open(path)
errors.Raise(err)

// check the name of the function that exports the service
s, err := pg.Lookup("Name")
errors.Raise(err)

// check if the reference symbol exists in the plugin
rf, err := pg.Lookup("NewService")
rf, err := pg.Lookup(*s.(*string))
errors.Raise(err)

// Load the service in a variable as the interface Service.
Expand All @@ -27,17 +31,6 @@ func getServicePlugin(path string) Service {
return newservice
}

// Logger writes to the system log.
type Logger interface {
Error(v ...interface{}) error
Warning(v ...interface{}) error
Info(v ...interface{}) error

Errorf(format string, a ...interface{}) error
Warningf(format string, a ...interface{}) error
Infof(format string, a ...interface{}) error
}

// Interface used by every service plugin that offers a service. At the very least, every plugin
// must contain the set of methods and attributes from this interface.
// It is up to the plugin to determine the implementation of these methods for the most part.
Expand Down
6 changes: 5 additions & 1 deletion riotpot/templated/templated.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ import (
"riotpot/services"
)

var Name string

// Place here the name of the function which returns the service interface.
// This name will be used as a lookup symbol.
var Name string = "Templated"
func init() {
Name = "Templated"
}

// The function must be capitalize or exported, and return a `Service`
// interface compatible struct.
Expand Down

0 comments on commit c3600f5

Please sign in to comment.