Forráskód Böngészése

:recycle: refactor code

Ettore Di Giacinto 3 éve
szülő
commit
2a22f27ab3
2 módosított fájl, 14 hozzáadás és 27 törlés
  1. 7 4
      pkg/hub/hub.go
  2. 7 23
      pkg/hub/room.go

+ 7 - 4
pkg/hub/hub.go

@@ -33,7 +33,7 @@ import (
 type MessageHub struct {
 	sync.Mutex
 
-	r         *Room
+	r         *room
 	otpKey    string
 	maxsize   int
 	keyLength int
@@ -43,9 +43,12 @@ type MessageHub struct {
 	Messages  chan *Message
 }
 
+// roomBufSize is the number of incoming messages to buffer for each topic.
+const roomBufSize = 128
+
 func NewHub(otp string, maxsize, keyLength, interval int) *MessageHub {
 	return &MessageHub{otpKey: otp, maxsize: maxsize, keyLength: keyLength, interval: interval,
-		Messages: make(chan *Message, RoomBufSize)}
+		Messages: make(chan *Message, roomBufSize)}
 }
 
 func (m *MessageHub) topicKey() string {
@@ -71,7 +74,7 @@ func (m *MessageHub) joinRoom(host host.Host) error {
 	}
 
 	// join the "chat" room
-	cr, err := JoinRoom(ctx, ps, host.ID(), m.topicKey(), m.Messages)
+	cr, err := connect(ctx, ps, host.ID(), m.topicKey(), m.Messages)
 	if err != nil {
 		return err
 	}
@@ -115,7 +118,7 @@ func (m *MessageHub) PublishMessage(mess *Message) error {
 	m.Lock()
 	defer m.Unlock()
 	if m.r != nil {
-		return m.r.PublishMessage(mess)
+		return m.r.publishMessage(mess)
 	}
 	return errors.New("no message room available")
 }

+ 7 - 23
pkg/hub/room.go

@@ -24,13 +24,10 @@ import (
 	pubsub "github.com/libp2p/go-libp2p-pubsub"
 )
 
-// RoomBufSize is the number of incoming messages to buffer for each topic.
-const RoomBufSize = 128
-
 // Room represents a subscription to a single PubSub topic. Messages
 // can be published to the topic with Room.Publish, and received
 // messages are pushed to the Messages channel.
-type Room struct {
+type room struct {
 	ctx   context.Context
 	ps    *pubsub.PubSub
 	Topic *pubsub.Topic
@@ -40,9 +37,9 @@ type Room struct {
 	self     peer.ID
 }
 
-// JoinRoom tries to subscribe to the PubSub topic for the room name, returning
+// connect tries to subscribe to the PubSub topic for the room name, returning
 // a Room on success.
-func JoinRoom(ctx context.Context, ps *pubsub.PubSub, selfID peer.ID, roomName string, messageChan chan *Message) (*Room, error) {
+func connect(ctx context.Context, ps *pubsub.PubSub, selfID peer.ID, roomName string, messageChan chan *Message) (*room, error) {
 	// join the pubsub topic
 	topic, err := ps.Join(roomName)
 	if err != nil {
@@ -55,7 +52,7 @@ func JoinRoom(ctx context.Context, ps *pubsub.PubSub, selfID peer.ID, roomName s
 		return nil, err
 	}
 
-	cr := &Room{
+	cr := &room{
 		ctx:      ctx,
 		ps:       ps,
 		Topic:    topic,
@@ -69,21 +66,8 @@ func JoinRoom(ctx context.Context, ps *pubsub.PubSub, selfID peer.ID, roomName s
 	return cr, nil
 }
 
-// Publish sends a message to the pubsub topic.
-func (cr *Room) Publish(message string, o ...func(*Message)) error {
-	m := &Message{
-		Message: message,
-	}
-
-	for _, f := range o {
-		f(m)
-	}
-
-	return cr.PublishMessage(m)
-}
-
-// Publish sends a message to the pubsub topic.
-func (cr *Room) PublishMessage(m *Message) error {
+// publishMessage sends a message to the pubsub topic.
+func (cr *room) publishMessage(m *Message) error {
 	m.SenderID = cr.self.Pretty()
 
 	msgBytes, err := json.Marshal(m)
@@ -94,7 +78,7 @@ func (cr *Room) PublishMessage(m *Message) error {
 }
 
 // readLoop pulls messages from the pubsub topic and pushes them onto the Messages channel.
-func (cr *Room) readLoop(messageChan chan *Message) {
+func (cr *room) readLoop(messageChan chan *Message) {
 	for {
 		msg, err := cr.sub.Next(cr.ctx)
 		if err != nil {