Skip to content

Commit

Permalink
Adapt new go-ma structure
Browse files Browse the repository at this point in the history
  • Loading branch information
bahner committed Apr 12, 2024
1 parent d756e8b commit ced73ef
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 8 deletions.
3 changes: 1 addition & 2 deletions cmd/pong/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"

"github.com/bahner/go-ma"
"github.com/bahner/go-ma-actor/entity"
"github.com/bahner/go-ma-actor/entity/actor"
"github.com/bahner/go-ma/msg"
Expand Down Expand Up @@ -51,7 +50,7 @@ func handleMessageEvents(ctx context.Context, a *actor.Actor) {
continue
}

if to == me && _type == ma.MESSAGE_TYPE {
if to == me && _type == msg.PLAIN {
messageReply(ctx, a, m)
}
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/robot/robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"

"github.com/bahner/go-ma"
"github.com/bahner/go-ma-actor/config"
"github.com/bahner/go-ma-actor/entity"
"github.com/bahner/go-ma-actor/entity/actor"
Expand Down Expand Up @@ -98,7 +97,7 @@ func (i *RobotStruct) handleEntityMessageEvents() {
continue
}

if m.Message.Type == ma.MESSAGE_TYPE {
if m.Message.Type == msg.PLAIN {
i.handleMessage(ctx, m)
}
}
Expand Down
5 changes: 5 additions & 0 deletions entity/actor/actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@ const ENVELOPES_BUFFERSIZE = 100

// An actor is just an entity with a keyset.
type Actor struct {
// The entity is the core of the actor.
Entity *entity.Entity

// The keyset is used to sign messages.
Keyset set.Keyset

// Envelopes are here. Messages comes to the entity.
// So Entity.Messages
Envelopes chan *msg.Envelope

// A handler for the dot messages.
MessageHandler func(*msg.Message)
}

// Create a new entity from a DID and a Keyset. We need both.
Expand Down
28 changes: 28 additions & 0 deletions entity/actor/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package actor

import (
"github.com/bahner/go-ma/msg"
)

func (a *Actor) DefaultMessageHandler(m *msg.Message) error {

switch m.Type {
case msg.DOT:
return handleDotMessage(m)
case msg.BROADCAST:
return handleBroadcastMessage(m)

default:
return msg.ErrInvalidType
}
}

func handleDotMessage(m *msg.Message) error {
// Do something with the message
return nil
}

func handleBroadcastMessage(m *msg.Message) error {
// Do something with the message
return nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.22
require (
github.com/adrg/xdg v0.4.0
github.com/ayush6624/go-chatgpt v0.3.0
github.com/bahner/go-ma v0.7.6
github.com/bahner/go-ma v0.7.7-0.20240412215842-0b5e204f34fd
github.com/ergo-services/ergo v1.999.224
github.com/fsnotify/fsnotify v1.7.0
github.com/fxamacker/cbor/v2 v2.6.0
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ github.com/ayush6624/go-chatgpt v0.3.0 h1:tQUfwSvSL9KA2XmBqj3L8aVdVPRb0Hcs3XtMmZ
github.com/ayush6624/go-chatgpt v0.3.0/go.mod h1:bn550cv7EHT7sHJG5yR60IGqjKlZ0S0Ll+IZp3z7nOc=
github.com/bahner/go-ma v0.7.6 h1:VJXVtT8iVDDwpQwpSvBNFinQ/nm0pI6D/qryWoE6yIk=
github.com/bahner/go-ma v0.7.6/go.mod h1:IgMPcdPzH1GkP5mMz2PhJh8831hGi4TzhYbOV8UFzZ0=
github.com/bahner/go-ma v0.7.7-0.20240412212004-e42bd9e959bf h1:ZbQ+u6JktyXdyqgSHjthaQZ9AK9QdWrOiNlXTCxaumI=
github.com/bahner/go-ma v0.7.7-0.20240412212004-e42bd9e959bf/go.mod h1:IgMPcdPzH1GkP5mMz2PhJh8831hGi4TzhYbOV8UFzZ0=
github.com/bahner/go-ma v0.7.7-0.20240412212950-76f81aec0c9b h1:/rxbOIFspuRI6nc7Eg4MwhfKlSRKExHgN+HxgzvCalM=
github.com/bahner/go-ma v0.7.7-0.20240412212950-76f81aec0c9b/go.mod h1:IgMPcdPzH1GkP5mMz2PhJh8831hGi4TzhYbOV8UFzZ0=
github.com/bahner/go-ma v0.7.7-0.20240412214208-1a0d577b3c95 h1:G1gZAHuMwUpkVx9xBJ16MF8xSx+2TihbJ3/xYSjbLOs=
github.com/bahner/go-ma v0.7.7-0.20240412214208-1a0d577b3c95/go.mod h1:IgMPcdPzH1GkP5mMz2PhJh8831hGi4TzhYbOV8UFzZ0=
github.com/bahner/go-ma v0.7.7-0.20240412215842-0b5e204f34fd h1:YI685TvEET9oXaouCTQ50duvvMz/4hu4oxwzu9MD0L8=
github.com/bahner/go-ma v0.7.7-0.20240412215842-0b5e204f34fd/go.mod h1:IgMPcdPzH1GkP5mMz2PhJh8831hGi4TzhYbOV8UFzZ0=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/benbjohnson/clock v1.3.5 h1:VvXlSJBzZpA/zum6Sj74hxwYI2DIxRWuNIoXAzHZz5o=
Expand Down
2 changes: 1 addition & 1 deletion ui/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (ui *ChatUI) subscribeBroadcasts() {

m, err := msg.UnmarshalAndVerifyMessageFromCBOR(message.Data)
if err == nil {
if m.Type == ma.BROADCAST_MESSAGE_TYPE {
if m.Type == msg.BROADCAST {
log.Debugf("handleBroadcastMessages: Received broadcast message: %v\n", m)
ui.displayBroadcastMessage(m)
continue
Expand Down
4 changes: 2 additions & 2 deletions ui/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"
"time"

"github.com/bahner/go-ma"
"github.com/bahner/go-ma/msg"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -62,7 +62,7 @@ func (ui *ChatUI) handleEvents() {
ui.handleChatMessage(input)

case m := <-ui.chMessages:
if m.Message.Type == ma.BROADCAST_MESSAGE_TYPE {
if m.Message.Type == msg.BROADCAST {
ui.displayBroadcastMessage(m.Message)
continue
}
Expand Down

0 comments on commit ced73ef

Please sign in to comment.