Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support multiple .orbit files with new transport #72

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: go

go:
- 1.14.x
- 1.15.x
- 1.16.x

Expand Down
4 changes: 2 additions & 2 deletions AUTHORS
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Roland Singer <roland.singer@desertbit.com>
Sebastian Borchers <sebastian@desertbit.com>
Roland Singer <roland.singer[at]desertbit.com>
Sebastian Borchers <sebastian[at]desertbit.com>
4 changes: 2 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# TODO
- write tests
- complete full example
- finish documenting
- finish documentation
- Include go report in readme (and fix issues that it reports beforehand)
- add orbit fmt cmd for .orbit files
- disconnect session after TTL?
- use tls.dialContext in yamux, once the new go version has come out
- rework sample (it is more of a test right now, would be nicer if the simple one shows the minimal needed setup, and a full one almost all of the features available)
20 changes: 9 additions & 11 deletions cmd/orbit/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,37 +28,35 @@
package main

import (
"errors"

"github.com/desertbit/grumble"
"github.com/desertbit/orbit/internal/codegen/gen"
)

const (
flagForce = "force"

argFiles = "files"
)

var cmdGen = &grumble.Command{
Name: "gen",
Help: "generate go code from .orbit file. Args: <files>",
AllowArgs: true,
Run: runGen,
Name: "gen",
Help: "generate go code from .orbit file",
Run: runGen,
Flags: func(f *grumble.Flags) {
f.Bool("f", flagForce, false, "generate all files, ignoring their last modification time")
},
Args: func(a *grumble.Args) {
a.StringList(argFiles, "The .orbit files the code should be generated for", grumble.Min(1))
},
}

func init() {
App.AddCommand(cmdGen)
}

func runGen(ctx *grumble.Context) (err error) {
if len(ctx.Args) == 0 {
return errors.New("no file given")
}

// Iterate over each provided file path and generate the .orbit file.
for _, fp := range ctx.Args {
for _, fp := range ctx.Args.StringList(argFiles) {
err = gen.Generate(fp, ctx.Flags.Bool(flagForce))
if err != nil {
return
Expand Down
13 changes: 11 additions & 2 deletions examples/full/api/api_orbit_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
errors "errors"
fmt "fmt"
closer "github.com/desertbit/closer/v3"
options "github.com/desertbit/options"
oclient "github.com/desertbit/orbit/pkg/client"
codec "github.com/desertbit/orbit/pkg/codec"
packet "github.com/desertbit/orbit/pkg/packet"
Expand Down Expand Up @@ -334,7 +335,11 @@ type client struct {
maxRetSize int
}

func NewClient(opts *oclient.Options) (c Client, err error) {
func NewClient(opts oclient.Options) (c Client, err error) {
err = options.SetDefaults(&opts, oclient.DefaultOptions())
if err != nil {
return
}
oc, err := oclient.New(opts)
if err != nil {
return
Expand Down Expand Up @@ -512,7 +517,11 @@ type service struct {
maxRetSize int
}

func NewService(h ServiceHandler, opts *oservice.Options) (s Service, err error) {
func NewService(h ServiceHandler, opts oservice.Options) (s Service, err error) {
err = options.SetDefaults(&opts, oservice.DefaultOptions())
if err != nil {
return
}
os, err := oservice.New(opts)
if err != nil {
return
Expand Down
32 changes: 28 additions & 4 deletions examples/simple/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@ import (
"time"

"github.com/desertbit/orbit/examples/simple/hello"
"github.com/desertbit/orbit/examples/simple/world"
"github.com/desertbit/orbit/pkg/client"
olog "github.com/desertbit/orbit/pkg/hook/log"
"github.com/desertbit/orbit/pkg/transport/mux"
"github.com/desertbit/orbit/pkg/transport/quic"
)

func main() {
tr, err := quic.NewTransport(&quic.Options{
qtr, err := quic.NewTransport(quic.Options{
DialAddr: "127.0.0.1:1122",
TLSConfig: &tls.Config{
InsecureSkipVerify: true,
NextProtos: []string{"orbit-simple-example"},
Expand All @@ -52,9 +55,14 @@ func main() {
log.Fatalln(err)
}

c, err := hello.NewClient(&client.Options{
Host: "127.0.0.1:1122",
Transport: tr,
// Multiplex transport to allow multiple services.
mtr, err := mux.New(qtr, mux.DefaultOptions())
if err != nil {
return
}

c, err := hello.NewClient(client.Options{
Transport: mtr.Transport("hello"),
Hooks: client.Hooks{
olog.ClientHook(),
},
Expand All @@ -64,6 +72,17 @@ func main() {
}
defer c.Close()

wc, err := world.NewClient(client.Options{
Transport: mtr.Transport("world"),
Hooks: client.Hooks{
olog.ClientHook(),
},
})
if err != nil {
log.Fatalln(err)
}
defer wc.Close()

var wg sync.WaitGroup
wg.Add(1)
go func() {
Expand Down Expand Up @@ -115,4 +134,9 @@ func main() {
}
bi.Close()
wg.Wait()

err = wc.YetAnotherCall(context.Background(), world.YetAnotherCallArg{S: "Finally done"})
if err != nil {
return
}
}
13 changes: 11 additions & 2 deletions examples/simple/hello/hello_orbit_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
errors "errors"
fmt "fmt"
closer "github.com/desertbit/closer/v3"
options "github.com/desertbit/options"
oclient "github.com/desertbit/orbit/pkg/client"
codec "github.com/desertbit/orbit/pkg/codec"
packet "github.com/desertbit/orbit/pkg/packet"
Expand Down Expand Up @@ -368,7 +369,11 @@ type client struct {
maxRetSize int
}

func NewClient(opts *oclient.Options) (c Client, err error) {
func NewClient(opts oclient.Options) (c Client, err error) {
err = options.SetDefaults(&opts, oclient.DefaultOptions())
if err != nil {
return
}
oc, err := oclient.New(opts)
if err != nil {
return
Expand Down Expand Up @@ -479,7 +484,11 @@ type service struct {
maxRetSize int
}

func NewService(h ServiceHandler, opts *oservice.Options) (s Service, err error) {
func NewService(h ServiceHandler, opts oservice.Options) (s Service, err error) {
err = options.SetDefaults(&opts, oservice.DefaultOptions())
if err != nil {
return
}
os, err := oservice.New(opts)
if err != nil {
return
Expand Down
49 changes: 43 additions & 6 deletions examples/simple/service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,33 @@ import (
"time"

"github.com/desertbit/orbit/examples/simple/hello"
"github.com/desertbit/orbit/examples/simple/world"
olog "github.com/desertbit/orbit/pkg/hook/log"
"github.com/desertbit/orbit/pkg/service"
"github.com/desertbit/orbit/pkg/transport"
"github.com/desertbit/orbit/pkg/transport/mux"
"github.com/desertbit/orbit/pkg/transport/quic"
)

func main() {
tr, err := quic.NewTransport(&quic.Options{
TLSConfig: generateTLSConfig(),
// Create quic transport.
qtr, err := quic.NewTransport(quic.Options{
ListenAddr: "127.0.0.1:1122",
TLSConfig: generateTLSConfig(),
})
if err != nil {
log.Fatalln(err)
}

// Multiplex transport to allow multiple services.
mtr, err := mux.New(qtr, mux.DefaultOptions())
if err != nil {
return
}

s, err := hello.NewService(&ServiceHandler{},
&service.Options{
ListenAddr: ":1122",
Transport: tr,
service.Options{
Transport: mtr.Transport("hello"),
Hooks: service.Hooks{
olog.ServiceHook(),
},
Expand All @@ -66,7 +75,26 @@ func main() {
}
defer s.Close()

err = s.Run()
go func() {
err = s.Run()
if err != nil {
log.Fatalln(err)
}
}()

ws, err := world.NewService(&ServiceHandler{},
service.Options{
Transport: mtr.Transport("world"),
Hooks: service.Hooks{
olog.ServiceHook(),
},
})
if err != nil {
log.Fatalln(err)
}
defer ws.Close()

err = ws.Run()
if err != nil {
log.Fatalln(err)
}
Expand Down Expand Up @@ -99,6 +127,8 @@ func generateTLSConfig() *tls.Config {

type ServiceHandler struct{}

// ### hello service

func (s *ServiceHandler) SayHi(ctx service.Context, arg hello.SayHiArg) (ret hello.SayHiRet, err error) {
fmt.Printf("handler: SayHi, %s, %s\n", arg.Name, arg.Ts.String())
ret = hello.SayHiRet{Res: []int{1, 2, 3}}
Expand Down Expand Up @@ -149,3 +179,10 @@ func (s *ServiceHandler) Bidirectional(ctx service.Context, stream *hello.Bidire
}
return nil
}

// ### world service

func (s *ServiceHandler) YetAnotherCall(ctx service.Context, arg world.YetAnotherCallArg) (err error) {
fmt.Printf("world: YAC -> %s\n", arg.S)
return
}
9 changes: 9 additions & 0 deletions examples/simple/world/world.orbit
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version 1

service {
call yetAnotherCall {
arg: {
s string
}
}
}
Loading