Skip to content

Commit

Permalink
feat(core): initial node implementation (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Jul 27, 2018
1 parent 36ac15e commit 9dabc35
Show file tree
Hide file tree
Showing 11 changed files with 304 additions and 135 deletions.
2 changes: 1 addition & 1 deletion core/Makefile
Expand Up @@ -12,7 +12,7 @@ BUILD_ENV ?= CGO_LDFLAGS="$(CGO_LDFLAGS)" CGO_CPPFLAGS="$(CGO_CPPFLAGS)"
all: install

.PHONY: install
install:
install: generate
go install -v ./cmd/...

.PHONY: test
Expand Down
207 changes: 103 additions & 104 deletions core/api/node/service.pb.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion core/api/node/service.proto
Expand Up @@ -7,7 +7,7 @@ import "api/entity/contact.proto";

option go_package = "github.com/berty/berty/core/api/node";

service BertyNode {
service Service {

//
// Events
Expand Down
53 changes: 26 additions & 27 deletions core/api/p2p/service.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion core/api/p2p/service.proto
Expand Up @@ -6,7 +6,7 @@ option go_package = "github.com/berty/berty/core/api/p2p";

import "api/p2p/event.proto";

service BertyP2P {
service Service {
rpc Handle(Event) returns (Void) {};
}

Expand Down
10 changes: 10 additions & 0 deletions core/cmd/berty/banner.go
@@ -0,0 +1,10 @@
package main

const banner = `
██████╗ ███████╗██████╗ ████████╗██╗ ██╗
██╔══██╗██╔════╝██╔══██╗╚══██╔══╝╚██╗ ██╔╝
██████╔╝█████╗ ██████╔╝ ██║ ╚████╔╝
██╔══██╗██╔══╝ ██╔══██╗ ██║ ╚██╔╝
██████╔╝███████╗██║ ██║ ██║ ██║
╚═════╝ ╚══════╝╚═╝ ╚═╝ ╚═╝ ╚═╝
`
76 changes: 75 additions & 1 deletion core/cmd/berty/daemon.go
@@ -1,11 +1,85 @@
package main

import "github.com/spf13/cobra"
import (
"fmt"
"os"
"os/signal"
"syscall"

reuse "github.com/libp2p/go-reuseport"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"

"github.com/berty/berty/core/node"
)

func newDaemonCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "daemon",
RunE: func(cmd *cobra.Command, args []string) error {
// initialize dependencies
gs := grpc.NewServer()
reflection.Register(gs)
listener, err := reuse.Listen("tcp", "0.0.0.0:1337")
if err != nil {
return err
}

// initialize nodes
n := node.New(
node.WithP2PGrpcServer(gs),
node.WithNodeGrpcServer(gs),
)
_ = n

// start grpc server(s)
go gs.Serve(listener)
fmt.Println(banner)

// signal handling
signal_chan := make(chan os.Signal, 1)
signal.Notify(signal_chan,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT)

exit_chan := make(chan int)
go func() {
for {
s := <-signal_chan
switch s {
// kill -SIGHUP XXXX
case syscall.SIGHUP:
fmt.Println("hungup")

// kill -SIGINT XXXX or Ctrl+c
case syscall.SIGINT:
exit_chan <- 0

// kill -SIGTERM XXXX
case syscall.SIGTERM:
fmt.Println("force stop")
exit_chan <- 0

// kill -SIGQUIT XXXX
case syscall.SIGQUIT:
fmt.Println("stop and core dump")
exit_chan <- 0

default:
fmt.Println("Unknown signal.")
exit_chan <- 1
}
}
}()

code := <-exit_chan
if code != 0 {
os.Exit(code)
}

return nil
},
}
Expand Down
Empty file removed core/node/.gitkeep
Empty file.
14 changes: 14 additions & 0 deletions core/node/node.go
@@ -0,0 +1,14 @@
package node

type Node struct{}

func New(opts ...nodeOptions) *Node {
n := &Node{}

for _, opt := range opts {
opt(n)
}
return n
}

type nodeOptions func(n *Node)
51 changes: 51 additions & 0 deletions core/node/nodeapi.go
@@ -0,0 +1,51 @@
package node

import (
"context"
"fmt"

"google.golang.org/grpc"

"github.com/berty/berty/core/api/entity"
"github.com/berty/berty/core/api/node"
)

type nodeapi struct{}

func WithNodeGrpcServer(gs *grpc.Server) nodeOptions {
return func(n *Node) {
node.RegisterServiceServer(gs, n)
}
}

// events

func (n *Node) EventList(*node.Void, node.Service_EventListServer) error {
return fmt.Errorf("not implemented")
}

func (n *Node) EventStream(*node.Void, node.Service_EventStreamServer) error {
return fmt.Errorf("not implemented")
}

// contacts

func (n *Node) ContactAcceptRequest(context.Context, *entity.Contact) (*entity.Contact, error) {
return nil, fmt.Errorf("not implemented")
}

func (n *Node) ContactRequest(context.Context, *node.ContactRequestInput) (*entity.Contact, error) {
return nil, fmt.Errorf("not implemented")
}

func (n *Node) ContactUpdate(context.Context, *entity.Contact) (*entity.Contact, error) {
return nil, fmt.Errorf("not implemented")
}

func (n *Node) ContactRemove(context.Context, *entity.Contact) (*entity.Contact, error) {
return nil, fmt.Errorf("not implemented")
}

func (n *Node) ContactList(*node.Void, node.Service_ContactListServer) error {
return fmt.Errorf("not implemented")
}
22 changes: 22 additions & 0 deletions core/node/p2papi.go
@@ -0,0 +1,22 @@
package node

import (
"context"
"fmt"

"google.golang.org/grpc"

"github.com/berty/berty/core/api/p2p"
)

type p2papi struct{}

func WithP2PGrpcServer(gs *grpc.Server) nodeOptions {
return func(n *Node) {
p2p.RegisterServiceServer(gs, n)
}
}

func (n *Node) Handle(context.Context, *p2p.Event) (*p2p.Void, error) {
return nil, fmt.Errorf("not implemented")
}

0 comments on commit 9dabc35

Please sign in to comment.