Skip to content

Commit

Permalink
breaking(orchestrator): finally got something working without any bugs
Browse files Browse the repository at this point in the history
* some cleaning

* some more cleaning

* add operation name to error

* savings

* fix a test
  • Loading branch information
timtimjnvr committed Nov 12, 2023
1 parent 04cd77b commit dc20251
Show file tree
Hide file tree
Showing 14 changed files with 272 additions and 294 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tag-releases.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ jobs:
NEW_MINOR=0
NEW_PATCH=0
if [[ "${{ github.event.head_commit.message }}" == *"breaking(:"* ]]; then
if [[ "${{ github.event.head_commit.message }}" == *"breaking("* ]]; then
NEW_MAJOR=$((MAJOR+1))
fi
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Decentralized P2P chat built in golang.
- basic text message exchanges.
- list current users in discussion.
- close a chat discussion.
- order messages and handle new users with operation based CRDTs.
- order messages and handle new users with operation based CRDTs.

## Commands

Expand All @@ -21,6 +21,7 @@ Decentralized P2P chat built in golang.
/close : exit the current room.
/list : display user(s) in the room.
/list_chats : display enterred rooms.
/list_users : display all connected users.
/quit : kills the program
```

Expand Down
2 changes: 1 addition & 1 deletion conn/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (d *NodeHandler) Start(newConnections <-chan net.Conn, toSend <-chan *crdt.
d.nodes[s] = n

case s := <-done:
quitOperation := crdt.NewOperation(crdt.RemoveNode, "", nil)
quitOperation := crdt.NewOperation(crdt.KillNode, "", nil)
quitOperation.Slot = uint8(s)
toExecute <- quitOperation
d.nodes[s] = nil
Expand Down
2 changes: 1 addition & 1 deletion conn/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func TestNodeHandler_StartStopNodesAndSendQuit(t *testing.T) {
// killing conn1 by closing conn2
conn2.Close()

expectedQuitOperation := crdt.NewOperation(crdt.RemoveNode, "", nil)
expectedQuitOperation := crdt.NewOperation(crdt.KillNode, "", nil)
expectedQuitOperation.Slot = 1
expectedBytes := expectedQuitOperation.ToBytes()

Expand Down
34 changes: 4 additions & 30 deletions crdt/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package crdt

import (
"encoding/json"
"fmt"
"github.com/google/uuid"
"github.com/pkg/errors"
"time"
Expand Down Expand Up @@ -50,7 +51,7 @@ func (c *Chat) SaveNode(nodeSlot uint8) {
c.nodesSlots = append(c.nodesSlots, nodeSlot)
}

func (c *Chat) RemoveNodeSlot(slot uint8) error {
func (c *Chat) RemoveNode(slot uint8) error {
// get index
var (
index int
Expand Down Expand Up @@ -167,25 +168,6 @@ func (c *Chat) GetSlots() []uint8 {
return slots
}

/*
func (c *Chat) GetNodeBySlot(slot uint8) (*NodeInfos, error) {
for _, s := range c.nodesSlots {
if s == slot {
return s, nil
}
}
return &NodeInfos{}, NotFoundErr
}
*/
/*
func (c *Chat) DisplayUsers() {
log.Printf("chat name : %s\n", c.Name)
for _, n := range c.nodesSlots {
log.Printf("- %s (Address: %s, Port: %s, Slot: %d)\n", n.Name, n.Address, n.Port, n.Slot)
}
}
*/
func (c *Chat) ContainsMessage(message *Message) bool {
for _, m := range c.messages {
if m.Id == message.Id {
Expand All @@ -195,14 +177,6 @@ func (c *Chat) ContainsMessage(message *Message) bool {
return false
}

/*
func (c *Chat) containsNode(s uuid.UUID) bool {
for _, n := range c.nodesSlots {
if n.Id == id {
return true
}
}
return false
func (c *Chat) Display() {
fmt.Printf("- %s : %d users, %d messages\n", c.Name, len(c.nodesSlots)+1, len(c.messages))
}
*/
2 changes: 1 addition & 1 deletion crdt/chat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func TestChat_RemoveNodeBySlot(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.chat.RemoveNodeSlot(tt.slot)
tt.chat.RemoveNode(tt.slot)
assert.NotContains(t, idToDelete, tt.chat.nodesSlots)
assert.Equal(t, tt.expectedNumberOfNodes, len(tt.chat.nodesSlots))
})
Expand Down
4 changes: 4 additions & 0 deletions crdt/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package crdt

import (
"encoding/json"
"fmt"
"github.com/google/uuid"
)

Expand Down Expand Up @@ -39,3 +40,6 @@ func (i *NodeInfos) ToBytes() []byte {
bytesMessage, _ := json.Marshal(i)
return bytesMessage
}
func (i *NodeInfos) Display() {
fmt.Printf("- %s (Address: %s, Port: %s, Slot: %d)\n", i.Name, i.Address, i.Port, i.Slot)
}
26 changes: 24 additions & 2 deletions crdt/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,32 @@ const (
AddNode
RemoveNode
AddChat
LeaveChat
RemoveChat
SwitchChat
AddMessage
ListChatUsers
ListUsers
ListChats
Quit
)

var operationNames = map[OperationType]string{
CreateChat: "create chat",
JoinChatByName: "join chat by name",
SaveNode: "save node",
KillNode: "kill node",
AddNode: "add node",
RemoveNode: "remove node",
AddChat: "add chat",
RemoveChat: "leave chat",
SwitchChat: "switch chat",
AddMessage: "add message",
ListChatUsers: "list chat users",
ListUsers: "list users",
ListChats: "list chats",
Quit: "quit",
}

func NewOperation(typology OperationType, targetedChat string, data Data) *Operation {
return &Operation{
Slot: 0,
Expand Down Expand Up @@ -96,7 +114,7 @@ func DecodeOperation(bytes []byte) (*Operation, error) {

// decode data into concrete type when needed
switch typology {
case AddNode, SaveNode, LeaveChat, JoinChatByName:
case AddNode, SaveNode, RemoveChat, JoinChatByName:
var result NodeInfos
err := decodeData(dataBytes, &result)
if err != nil {
Expand Down Expand Up @@ -144,3 +162,7 @@ func getField(offset int, source []byte) (int, []byte) {
return offset + lenField + 1, source[offset+1 : offset+lenField+1]
}
}

func GetOperationName(typology OperationType) string {
return operationNames[typology]
}
Loading

0 comments on commit dc20251

Please sign in to comment.