github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/rpc/doc.go (about)

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