github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/p2p/context.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 p2p
    19  
    20  import (
    21  	"github.com/mysteriumnetwork/node/identity"
    22  )
    23  
    24  // Context represents request context.
    25  type Context interface {
    26  	// Request returns message with data bytes.
    27  	Request() *Message
    28  
    29  	// Error allows to return error which will be seen for peer.
    30  	Error(err error) error
    31  
    32  	// OkWithReply indicates that request was handled successfully and returns reply with given message.
    33  	OkWithReply(msg *Message) error
    34  
    35  	// Ok indicates that request was handled successfully
    36  	OK() error
    37  
    38  	// PeerID returns peer identity used to authenticate P2P channel
    39  	PeerID() identity.Identity
    40  }
    41  
    42  type defaultContext struct {
    43  	req         *Message
    44  	res         *Message
    45  	publicError error
    46  	peerID      identity.Identity
    47  }
    48  
    49  func (d *defaultContext) Request() *Message {
    50  	return d.req
    51  }
    52  
    53  func (d *defaultContext) Error(err error) error {
    54  	d.publicError = err
    55  	return nil
    56  }
    57  
    58  func (d *defaultContext) OkWithReply(msg *Message) error {
    59  	d.res = msg
    60  	return nil
    61  }
    62  
    63  func (d *defaultContext) OK() error {
    64  	return nil
    65  }
    66  
    67  func (d *defaultContext) PeerID() identity.Identity {
    68  	return d.peerID
    69  }