Skip to content

Commit

Permalink
Add config file
Browse files Browse the repository at this point in the history
  • Loading branch information
watal authored and Motok1 committed Jun 2, 2022
1 parent bbb88ca commit 2c27cd3
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 19 deletions.
20 changes: 19 additions & 1 deletion cmd/polad/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,31 @@
package main

import (
"flag"
"log"

"github.com/nttcom/pola/internal/config"
"github.com/nttcom/pola/pkg/server"
)

type Flags struct {
ConfigFile string
}

func main() {
if err := server.NewPce(); err != nil {
f := new(Flags)
flag.StringVar(&f.ConfigFile, "f", "polad.yaml", "Specify a configuration file")
flag.Parse()

c, err := config.ReadConfigFile(f.ConfigFile)
if err != nil {
log.Fatal(err)
}

o := new(server.PceOptions)
o.PcepAddr = c.Global.Address
o.PcepPort = c.Global.Port
if err := server.NewPce(o); err != nil {
log.Fatal(err)
}
}
3 changes: 3 additions & 0 deletions configs/polad.yaml.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
global:
address: "127.0.0.1"
port: 4189
15 changes: 8 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ module github.com/nttcom/pola
go 1.17

require (
google.golang.org/grpc v1.45.0
google.golang.org/protobuf v1.28.0
google.golang.org/grpc v1.45.0
google.golang.org/protobuf v1.28.0
)

require (
github.com/golang/protobuf v1.5.2 // indirect
golang.org/x/net v0.0.0-20200822124328-c89045814202 // indirect
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd // indirect
golang.org/x/text v0.3.0 // indirect
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect
github.com/go-yaml/yaml v2.1.0+incompatible // indirect
github.com/golang/protobuf v1.5.2 // indirect
golang.org/x/net v0.0.0-20200822124328-c89045814202 // indirect
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd // indirect
golang.org/x/text v0.3.0 // indirect
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.m
github.com/envoyproxy/go-control-plane v0.9.10-0.20210907150352-cf90f659a021/go.mod h1:AFq3mo9L8Lqqiid3OhADV3RfLJnjiw63cSpi+fDTRC0=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-yaml/yaml v2.1.0+incompatible h1:RYi2hDdss1u4YE7GwixGzWwVo47T8UQwnTLB6vQiq+o=
github.com/go-yaml/yaml v2.1.0+incompatible/go.mod h1:w2MrLa16VYP0jy6N7M5kHaCkaLENm+P+Tv+MfurjSw0=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand Down
31 changes: 31 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package config

import (
"log"
"os"

"github.com/go-yaml/yaml"
)

type Global struct {
Address string `yaml:"address"`
Port string `yaml:"port"`
}

type Config struct {
Global Global `yaml:"global"`
}

func ReadConfigFile(configFile string) (Config, error) {
c := new(Config)

f, err := os.Open(configFile)
if err != nil {
log.Fatal(err)
return *c, err
}
defer f.Close()

err = yaml.NewDecoder(f).Decode(&c)
return *c, err
}
21 changes: 10 additions & 11 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ import (
"github.com/nttcom/pola/pkg/packet/pcep"
)

// TODO: 引数やconfファイルで上書きできるようにする
const PCEPORT = "4189"
const PCEADDR = "10.100.0.252"

type lsp struct {
peerAddr net.IP // 後々 router ID, router name などに変更したい
plspId uint32
Expand All @@ -36,11 +32,16 @@ type Server struct {
pb.UnimplementedPceServiceServer
}

func NewPce() error {
type PceOptions struct {
PcepAddr string
PcepPort string
}

func NewPce(o *PceOptions) error {
s := &Server{}
// PCEP の Listen を開始する
go func() {
if err := s.Listen(); err != nil {
if err := s.Listen(o.PcepAddr, o.PcepPort); err != nil {
fmt.Printf("PCEP listen Error\n")
}

Expand All @@ -61,13 +62,12 @@ func NewPce() error {
}
}

func (s *Server) Listen() error {
func (s *Server) Listen(address string, port string) error {
// PCEP の listen を行う
// PCEPPORT = "4189" 宛の SYN は全て accept
var listenInfo strings.Builder
listenInfo.WriteString(PCEADDR)
listenInfo.WriteString(address)
listenInfo.WriteString(":")
listenInfo.WriteString(PCEPORT)
listenInfo.WriteString(port)
fmt.Printf("[server] PCE Listen: %s\n", listenInfo.String())
listener, err := net.Listen("tcp", listenInfo.String())
if err != nil {
Expand All @@ -77,7 +77,6 @@ func (s *Server) Listen() error {
defer listener.Close()
sessionId := uint8(1)
for {
// PCEPPORT = "4189" へ SYN が来るたびに Accept
session := NewSession(sessionId)

fmt.Printf("%#v\n", s)
Expand Down

0 comments on commit 2c27cd3

Please sign in to comment.