github.com/Elemental-core/elementalcore@v0.0.0-20191206075037-63891242267a/rpc/doc.go (about)

     1  // Copyright 2015 The elementalcore Authors
     2  // This file is part of the elementalcore library.
     3  //
     4  // The elementalcore library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser 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  // The elementalcore library 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 Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the elementalcore library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  /*
    18  Package rpc provides access to the exported methods of an object across a network
    19  or other I/O connection. After creating a server instance objects can be registered,
    20  making it visible from the outside. Exported methods that follow specific
    21  conventions can be called remotely. It also has support for the publish/subscribe
    22  pattern.
    23  
    24  Methods that satisfy the following criteria are made available for remote access:
    25   - object must be exported
    26   - method must be exported
    27   - method returns 0, 1 (response or error) or 2 (response and error) values
    28   - method argument(s) must be exported or builtin types
    29   - method returned value(s) must be exported or builtin types
    30  
    31  An example method:
    32   func (s *CalcService) Add(a, b int) (int, error)
    33  
    34  When the returned error isn't nil the returned integer is ignored and the error is
    35  send back to the client. Otherwise the returned integer is send back to the client.
    36  
    37  Optional arguments are supported by accepting pointer values as arguments. E.g.
    38  if we want to do the addition in an optional finite field we can accept a mod
    39  argument as pointer value.
    40  
    41   func (s *CalService) Add(a, b int, mod *int) (int, error)
    42  
    43  This RPC method can be called with 2 integers and a null value as third argument.
    44  In that case the mod argument will be nil. Or it can be called with 3 integers,
    45  in that case mod will be pointing to the given third argument. Since the optional
    46  argument is the last argument the RPC package will also accept 2 integers as
    47  arguments. It will pass the mod argument as nil to the RPC method.
    48  
    49  The server offers the ServeCodec method which accepts a ServerCodec instance. It will
    50  read requests from the codec, process the request and sends the response back to the
    51  client using the codec. The server can execute requests concurrently. Responses
    52  can be sent back to the client out of order.
    53  
    54  An example server which uses the JSON codec:
    55   type CalculatorService struct {}
    56  
    57   func (s *CalculatorService) Add(a, b int) int {
    58  	return a + b
    59   }
    60  
    61   func (s *CalculatorService Div(a, b int) (int, error) {
    62  	if b == 0 {
    63  		return 0, errors.New("divide by zero")
    64  	}
    65  	return a/b, nil
    66   }
    67  
    68   calculator := new(CalculatorService)
    69   server := NewServer()
    70   server.RegisterName("calculator", calculator")
    71  
    72   l, _ := net.ListenUnix("unix", &net.UnixAddr{Net: "unix", Name: "/tmp/calculator.sock"})
    73   for {
    74  	c, _ := l.AcceptUnix()
    75  	codec := v2.NewJSONCodec(c)
    76  	go server.ServeCodec(codec)
    77   }
    78  
    79  The package also supports the publish subscribe pattern through the use of subscriptions.
    80  A method that is considered eligible for notifications must satisfy the following criteria:
    81   - object must be exported
    82   - method must be exported
    83   - first method argument type must be context.Context
    84   - method argument(s) must be exported or builtin types
    85   - method must return the tuple Subscription, error
    86  
    87  An example method:
    88   func (s *BlockChainService) NewBlocks(ctx context.Context) (Subscription, error) {
    89   	...
    90   }
    91  
    92  Subscriptions are deleted when:
    93   - the user sends an unsubscribe request
    94   - the connection which was used to create the subscription is closed. This can be initiated
    95     by the client and server. The server will close the connection on an write error or when
    96     the queue of buffered notifications gets too big.
    97  */
    98  package rpc