github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/core/discovery/brokerdiscovery/messaging_ping.go (about)

     1  /*
     2   * Copyright (C) 2020 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package brokerdiscovery
    19  
    20  import (
    21  	"github.com/mysteriumnetwork/node/communication"
    22  	"github.com/mysteriumnetwork/node/communication/nats"
    23  	"github.com/mysteriumnetwork/node/identity"
    24  	"github.com/mysteriumnetwork/node/market"
    25  )
    26  
    27  // pingMessage structure represents message that the Provider sends about healthy Proposal
    28  type pingMessage struct {
    29  	Proposal market.ServiceProposal `json:"proposal"`
    30  }
    31  
    32  const pingEndpoint = communication.MessageEndpoint("*.proposal-ping.v3")
    33  
    34  // pingProducer
    35  type pingProducer struct {
    36  	message *pingMessage
    37  	signer  identity.Signer
    38  }
    39  
    40  // GetMessageEndpoint returns endpoint where to send messages
    41  func (p *pingProducer) GetMessageEndpoint() (communication.MessageEndpoint, error) {
    42  	subj, err := nats.SignedSubject(p.signer, string(pingEndpoint))
    43  	return communication.MessageEndpoint(subj), err
    44  }
    45  
    46  // Produce creates message which will be serialized to endpoint
    47  func (p *pingProducer) Produce() (requestPtr interface{}) {
    48  	return p.message
    49  }
    50  
    51  // pingConsumer
    52  type pingConsumer struct {
    53  	Callback func(pingMessage) error
    54  }
    55  
    56  // GetMessageEndpoint returns endpoint where to receive messages
    57  func (c *pingConsumer) GetMessageEndpoint() (communication.MessageEndpoint, error) {
    58  	return pingEndpoint, nil
    59  }
    60  
    61  // NewMessage creates struct where message from endpoint will be serialized
    62  func (c *pingConsumer) NewMessage() (messagePtr interface{}) {
    63  	return &pingMessage{}
    64  }
    65  
    66  // Consume handles messages from endpoint
    67  func (c *pingConsumer) Consume(messagePtr interface{}) error {
    68  	return c.Callback(*messagePtr.(*pingMessage))
    69  }