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

[be1] reorganize files #1901

Merged
merged 19 commits into from
Jun 2, 2024
Merged
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
4 changes: 2 additions & 2 deletions be1-go/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ vet: protocol
check: test test-cov lint vet

protocol:
cp -r ../protocol ./validation
cp -r ../protocol ./internal/validation

clean:
rm -rf validation/protocol
rm -rf ./internal/validation/protocol

fmt:
gofmt -s -w ./
Expand Down
30 changes: 15 additions & 15 deletions be1-go/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ import (
"fmt"
"net/url"
"os"
popstellar "popstellar"
"popstellar/crypto"
"popstellar/hub"
"popstellar/internal/popserver"
"popstellar/internal/popserver/config"
"popstellar/internal/popserver/database"
"popstellar/internal/popserver/database/sqlite"
"popstellar/internal/popserver/state"
"popstellar/internal/popserver/utils"
"popstellar/network"
"popstellar/network/socket"
"popstellar/validation"
"popstellar/internal/crypto"
hub2 "popstellar/internal/hub"
"popstellar/internal/logger"
"popstellar/internal/network"
"popstellar/internal/network/socket"
"popstellar/internal/old/hub"
"popstellar/internal/singleton/config"
"popstellar/internal/singleton/database"
"popstellar/internal/singleton/state"
"popstellar/internal/singleton/utils"
"popstellar/internal/sqlite"
"popstellar/internal/validation"
"sync"
"time"

Expand Down Expand Up @@ -131,13 +131,13 @@ func (s *ServerConfig) newHub(l *zerolog.Logger) (hub.Hub, error) {
}
}

return popserver.NewHub(), nil
return hub2.NewHub(), nil
}

// Serve parses the CLI arguments and spawns a hub and a websocket server for
// the server
func Serve(cliCtx *cli.Context) error {
poplog := popstellar.Logger
poplog := logger.Logger

configFilePath := cliCtx.String("config-file")
var serverConfig ServerConfig
Expand Down Expand Up @@ -303,7 +303,7 @@ func connectToServers(h hub.Hub, wg *sync.WaitGroup, done chan struct{}, servers
func connectToSocket(address string, h hub.Hub,
wg *sync.WaitGroup, done chan struct{}) error {

poplog := popstellar.Logger
poplog := logger.Logger

urlString := fmt.Sprintf("ws://%s/server", address)
u, err := url.Parse(urlString)
Expand Down
10 changes: 5 additions & 5 deletions be1-go/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"golang.org/x/xerrors"
"io"
"os"
"popstellar/channel/lao"
"popstellar/crypto"
"popstellar/hub/standard_hub"
"popstellar/network"
"popstellar/network/socket"
"popstellar/internal/crypto"
"popstellar/internal/network"
"popstellar/internal/network/socket"
"popstellar/internal/old/channel/lao"
"popstellar/internal/old/hub/standard_hub"
"sync"
"testing"
"time"
Expand Down
47 changes: 26 additions & 21 deletions be1-go/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,33 @@ for their use.
The project is organized into different modules as follows

```
.
├── channel # contains the abstract definition of a channel NEED TO BE DELETED
├── cli # command line interface
├── crypto # defines the cryptographic suite
├── cli # command line interface
├── docs
├── hub # contains the abstract definition of a hub NEED TO BE DELETED
├── inbox # helper to store messages used by channels NEED TO BE DELETED
├── internal
│   ├── depgraph # tool to generate the dependencies graph
│   └── popserver # entry point of the messages received by the sockets
│   ├── config # singleton with the server config informations and server keys
│   ├── database # singleton with the database + implementations of the database
│   ├── generatortest # query and message generators only use for the tests
│   ├── handler # handlers for each query, answer and channel type (entry point is func HandleIncomingMessage)
│   ├── state # singleton with the temporary states of the server (peers, queries, and subscriptions)
│   ├── type # every types use in the implementation
│   └── utils # singleton with the log instance and the schema validator
├── message # message types and marshaling/unmarshaling logic
├── network # module to set up Websocket connections
│   └── socket # module to send/receive data over the wire
├── popcha # HTTP server and back-end logic for PoPCHA NEED TO BE REFACTOR
└── validation # module to validate incoming/outgoing messages
└── internal
├── crypto # defines the cryptographic suite
├── docsutils # utils use for the documentation
├── handler # handle the incoming messages
│   ├── answer # handler for the answers
│   ├── channel # handler for the channel
│   └── query # handler for the queries
├── hub # entry point of the messages received by the sockets
├── logger # logger use inside the implementation
├── message # message types and marshaling/unmarshaling logic
├── mocks # mocks and utils use inside tests
│   └── generator # query and message generators only use for the tests
├── network # module to set up Websocket connections
│   └── socket # module to send/receive data over the wire
├── old # old implementation NEED TO BE DELETE
├── popcha # HTTP server and back-end logic for PoPCHA NEED TO BE REFACTOR
├── repository # repository for the database
├── singleton # NEED TO BE REMOVE AND REPLACE BY REF INJECTION
│   ├── config # server config informations and server keys
│   ├── database # database
│   ├── state # temporary states of the server (peers, queries, and subscriptions)
│   └── utils # singleton with the log instance and the schema validator
├── sqlite # sqlite implementation of the repository
├── types # types use inside the implementation
└── validation # module to validate incoming/outgoing messages
```

The entry point is the `cli` with bulk of the implementation logic in the `popserver` package.
Expand Down
63 changes: 47 additions & 16 deletions be1-go/docs/images/dependencies/dependencies.dot
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
strict digraph {
labelloc="t";
label = <Modules dependencies of popstellar <font point-size='10'><br/>(generated 10 May 24 - 08:56:20)</font>>;
label = <Modules dependencies of popstellar <font point-size='10'><br/>(generated 2 Jun 24 - 18:53:37)</font>>;
graph [fontname = "helvetica"];
graph [fontname = "helvetica"];
node [fontname = "helvetica"];
Expand All @@ -9,19 +9,50 @@ node [shape=box,style=rounded];
start=0;
ratio = fill;
rankdir="LR";
"internal/popserver" -> "internal/popserver/config" [minlen=1];
"internal/popserver" -> "internal/popserver/handler" [minlen=1];
"internal/popserver" -> "internal/popserver/state" [minlen=1];
"internal/popserver" -> "internal/popserver/utils" [minlen=1];
"internal/popserver" -> "network/socket" [minlen=1];
"internal/popserver/database" -> "internal/popserver/types" [minlen=1];
"internal/popserver/handler" -> "internal/popserver/config" [minlen=1];
"internal/popserver/handler" -> "internal/popserver/database" [minlen=1];
"internal/popserver/handler" -> "internal/popserver/state" [minlen=1];
"internal/popserver/handler" -> "internal/popserver/types" [minlen=1];
"internal/popserver/handler" -> "internal/popserver/utils" [minlen=1];
"internal/popserver/handler" -> "network/socket" [minlen=1];
"internal/popserver/state" -> "internal/popserver/types" [minlen=1];
"internal/popserver/state" -> "network/socket" [minlen=1];
"internal/popserver/types" -> "network/socket" [minlen=1];
"cli" -> "internal/crypto" [minlen=1];
"cli" -> "internal/hub" [minlen=1];
"cli" -> "internal/network" [minlen=1];
"cli" -> "internal/network/socket" [minlen=1];
"cli" -> "internal/singleton/config" [minlen=1];
"cli" -> "internal/singleton/database" [minlen=1];
"cli" -> "internal/singleton/state" [minlen=1];
"cli" -> "internal/singleton/utils" [minlen=1];
"cli" -> "internal/validation" [minlen=1];
"internal/handler" -> "internal/handler/answer" [minlen=1];
"internal/handler" -> "internal/handler/query" [minlen=1];
"internal/handler" -> "internal/network/socket" [minlen=1];
"internal/handler" -> "internal/singleton/utils" [minlen=1];
"internal/handler" -> "internal/validation" [minlen=1];
"internal/handler/answer" -> "internal/handler/channel" [minlen=1];
"internal/handler/answer" -> "internal/handler/query" [minlen=1];
"internal/handler/answer" -> "internal/singleton/state" [minlen=1];
"internal/handler/channel" -> "internal/crypto" [minlen=1];
"internal/handler/channel" -> "internal/network/socket" [minlen=1];
"internal/handler/channel" -> "internal/singleton/config" [minlen=1];
"internal/handler/channel" -> "internal/singleton/database" [minlen=1];
"internal/handler/channel" -> "internal/singleton/state" [minlen=1];
"internal/handler/channel" -> "internal/singleton/utils" [minlen=1];
"internal/handler/channel" -> "internal/types" [minlen=1];
"internal/handler/channel" -> "internal/validation" [minlen=1];
"internal/handler/query" -> "internal/handler/channel" [minlen=1];
"internal/handler/query" -> "internal/network/socket" [minlen=1];
"internal/handler/query" -> "internal/singleton/config" [minlen=1];
"internal/handler/query" -> "internal/singleton/database" [minlen=1];
"internal/handler/query" -> "internal/singleton/state" [minlen=1];
"internal/handler/query" -> "internal/singleton/utils" [minlen=1];
"internal/hub" -> "internal/handler" [minlen=1];
"internal/hub" -> "internal/handler/query" [minlen=1];
"internal/hub" -> "internal/network/socket" [minlen=1];
"internal/hub" -> "internal/singleton/config" [minlen=1];
"internal/hub" -> "internal/singleton/database" [minlen=1];
"internal/hub" -> "internal/singleton/state" [minlen=1];
"internal/hub" -> "internal/singleton/utils" [minlen=1];
"internal/network" -> "internal/network/socket" [minlen=1];
"internal/network" -> "internal/popcha" [minlen=1];
"internal/repository" -> "internal/types" [minlen=1];
"internal/singleton/database" -> "internal/repository" [minlen=1];
"internal/singleton/state" -> "internal/network/socket" [minlen=1];
"internal/singleton/state" -> "internal/types" [minlen=1];
"internal/singleton/utils" -> "internal/validation" [minlen=1];
"internal/types" -> "internal/network/socket" [minlen=1];
}
Binary file modified be1-go/docs/images/dependencies/dependencies.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 0 additions & 8 deletions be1-go/internal/depgraph/dep.yml

This file was deleted.

14 changes: 14 additions & 0 deletions be1-go/internal/docsutils/depgraph/dep.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
modname: popstellar
overwrite: true
outfile: graph.dot
includes:
- popstellar/cli/*
- popstellar/internal/*
excludes:
- popstellar/internal/message/*
- popstellar/internal/docsutils/*
- popstellar/internal/old/*
- popstellar/internal/mocks/*
- popstellar/internal/sqlite/*
- popstellar/internal/logger
interfaces:
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package handler
package answer

import (
"encoding/json"
"math/rand"
"popstellar"
"popstellar/internal/popserver/state"
"popstellar/message/answer"
"popstellar/message/query/method/message"
"popstellar/internal/handler/channel"
"popstellar/internal/handler/query"
"popstellar/internal/logger"
"popstellar/internal/message/answer"
"popstellar/internal/message/query/method/message"
"popstellar/internal/singleton/state"
"sort"
)

Expand All @@ -15,7 +17,7 @@ const (
continueMongering = 0.5
)

func handleAnswer(msg []byte) *answer.Error {
func HandleAnswer(msg []byte) *answer.Error {
var answerMsg answer.Answer

err := json.Unmarshal(msg, &answerMsg)
Expand All @@ -33,14 +35,14 @@ func handleAnswer(msg []byte) *answer.Error {
}

if answerMsg.Result == nil {
popstellar.Logger.Info().Msg("received an error, nothing to handle")
logger.Logger.Info().Msg("received an error, nothing to handle")
// don't send any error to avoid infinite error loop as a server will
// send an error to another server that will create another error
return nil
}

if answerMsg.Result.IsEmpty() {
popstellar.Logger.Info().Msg("expected isn't an answer to a popquery, nothing to handle")
logger.Logger.Info().Msg("expected isn't an answer to a popquery, nothing to handle")
return nil
}

Expand All @@ -63,26 +65,26 @@ func handleRumorAnswer(msg answer.Answer) *answer.Error {
return errAnswer
}

popstellar.Logger.Debug().Msgf("received an answer to rumor query %d", *msg.ID)
logger.Logger.Debug().Msgf("received an answer to rumor query %d", *msg.ID)

if msg.Error != nil {
popstellar.Logger.Debug().Msgf("received an answer error to rumor query %d", *msg.ID)
logger.Logger.Debug().Msgf("received an answer error to rumor query %d", *msg.ID)
if msg.Error.Code != answer.DuplicateResourceErrorCode {
popstellar.Logger.Debug().Msgf("invalid error code to rumor query %d", *msg.ID)
logger.Logger.Debug().Msgf("invalid error code to rumor query %d", *msg.ID)
return nil
}

stop := rand.Float64() < continueMongering

if stop {
popstellar.Logger.Debug().Msgf("stop mongering rumor query %d", *msg.ID)
logger.Logger.Debug().Msgf("stop mongering rumor query %d", *msg.ID)
return nil
}

popstellar.Logger.Debug().Msgf("continue mongering rumor query %d", *msg.ID)
logger.Logger.Debug().Msgf("continue mongering rumor query %d", *msg.ID)
}

popstellar.Logger.Debug().Msgf("sender rumor need to continue sending query %d", *msg.ID)
logger.Logger.Debug().Msgf("sender rumor need to continue sending query %d", *msg.ID)
rumor, ok, errAnswer := state.GetRumorFromPastQuery(*msg.ID)
if errAnswer != nil {
return errAnswer
Expand All @@ -91,7 +93,7 @@ func handleRumorAnswer(msg answer.Answer) *answer.Error {
return answer.NewInternalServerError("rumor query %d doesn't exist", *msg.ID)
}

SendRumor(nil, rumor)
query.SendRumor(nil, rumor)

return nil
}
Expand All @@ -112,7 +114,7 @@ func handleGetMessagesByIDAnswer(msg answer.Answer) *answer.Error {
}

errAnswer := answer.NewInvalidMessageFieldError("failed to unmarshal: %v", err)
popstellar.Logger.Error().Err(errAnswer)
logger.Logger.Error().Err(errAnswer)
}

if len(msgsByChan[channelID]) == 0 {
Expand Down Expand Up @@ -144,7 +146,7 @@ func tryToHandleMessages(msgsByChannel map[string]map[string]message.Message, so
for _, channelID := range sortedChannelIDs {
msgs := msgsByChannel[channelID]
for msgID, msg := range msgs {
errAnswer := handleChannel(channelID, msg, false)
errAnswer := channel.HandleChannel(channelID, msg, false)
if errAnswer == nil {
delete(msgsByChannel[channelID], msgID)
continue
Expand All @@ -155,7 +157,7 @@ func tryToHandleMessages(msgsByChannel map[string]map[string]message.Message, so
}

errAnswer = errAnswer.Wrap(msgID).Wrap("tryToHandleMessages")
popstellar.Logger.Error().Err(errAnswer)
logger.Logger.Error().Err(errAnswer)
}

if len(msgsByChannel[channelID]) == 0 {
Expand Down
Loading
Loading