diff options
author | ng0 <ng0@infotropique.org> | 2017-09-06 08:10:50 +0000 |
---|---|---|
committer | ng0 <ng0@infotropique.org> | 2017-09-06 08:10:50 +0000 |
commit | c62c08a196a8018d2a70d6072d45a52290e66776 (patch) | |
tree | 03ecf7b4081b0bb72830b5cdc76c02889a046dce /doc | |
parent | 4d4c96237c19efbada15fa2152537ef8250f658f (diff) | |
download | gnunet-c62c08a196a8018d2a70d6072d45a52290e66776.tar.gz gnunet-c62c08a196a8018d2a70d6072d45a52290e66776.zip |
doc: gnunet-c-tutorial: some unrelated edits.
Diffstat (limited to 'doc')
-rw-r--r-- | doc/gnunet-c-tutorial.texi | 244 |
1 files changed, 117 insertions, 127 deletions
diff --git a/doc/gnunet-c-tutorial.texi b/doc/gnunet-c-tutorial.texi index 78d48a386..46ad98480 100644 --- a/doc/gnunet-c-tutorial.texi +++ b/doc/gnunet-c-tutorial.texi | |||
@@ -150,10 +150,10 @@ $ cd .. | |||
150 | Assuming all dependencies are installed, the following commands will | 150 | Assuming all dependencies are installed, the following commands will |
151 | compile and install GNUnet in your home directory. You can specify the | 151 | compile and install GNUnet in your home directory. You can specify the |
152 | directory where GNUnet will be installed by changing the | 152 | directory where GNUnet will be installed by changing the |
153 | --prefix value when calling ./configure. If | 153 | @code{--prefix} value when calling @command{./configure}. If |
154 | you do not specifiy a prefix, GNUnet is installed in the directory | 154 | you do not specifiy a prefix, GNUnet is installed in the directory |
155 | /usr/local. When developing new applications you may want | 155 | @file{/usr/local}. When developing new applications you may want |
156 | to enable verbose logging by adding --enable-logging=verbose: | 156 | to enable verbose logging by adding @code{--enable-logging=verbose}: |
157 | 157 | ||
158 | @example | 158 | @example |
159 | $ ./configure --prefix=$PREFIX --enable-logging | 159 | $ ./configure --prefix=$PREFIX --enable-logging |
@@ -330,8 +330,8 @@ $ gnunet-statistics -c ~/peer1.conf -s dht # print statistics about DHT service | |||
330 | 330 | ||
331 | This section describes how to start two peers on the same machine by hand. | 331 | This section describes how to start two peers on the same machine by hand. |
332 | The process is rather painful, but the description is somewhat instructive. | 332 | The process is rather painful, but the description is somewhat instructive. |
333 | In practice, you might prefer the automated method described in | 333 | In practice, you might prefer the automated method |
334 | Section sec:testbed. | 334 | (@pxref{Starting Peers Using the Testbed Service}). |
335 | 335 | ||
336 | @subsubsection Setup a second peer | 336 | @subsubsection Setup a second peer |
337 | We will now start a second peer on your machine. | 337 | We will now start a second peer on your machine. |
@@ -348,20 +348,18 @@ configuration file: | |||
348 | @example | 348 | @example |
349 | $ cat $PREFIX/share/gnunet/config.d/*.conf > peer2.conf | 349 | $ cat $PREFIX/share/gnunet/config.d/*.conf > peer2.conf |
350 | @end example | 350 | @end example |
351 | Now you have to edit peer2.conf and change: | 351 | Now you have to edit @file{peer2.conf} and change: |
352 | \begin{itemize} | 352 | @itemize |
353 | \itemsep0em | 353 | @item @code{GNUNET\_TEST\_HOME} under @code{PATHS} |
354 | \item{@code{GNUNET\_TEST\_HOME} under @code{PATHS}} | 354 | @tem Every (uncommented) value for ``@code{PORT}'' (add 10000) in any |
355 | \item{Every (uncommented) value for ``@code{PORT}'' (add 10000) in any | 355 | section (the option may be commented out if @code{PORT} is |
356 | section (the option may be commented out if @code{PORT} is | 356 | prefixed by "\#", in this case, UNIX domain sockets are used |
357 | prefixed by "\#", in this case, UNIX domain sockets are used | 357 | and the PORT option does not need to be touched) |
358 | and the PORT option does not need to be touched) } | 358 | @item Every value for ``@code{UNIXPATH}'' in any section (e.g. by adding a "-p2" suffix) |
359 | \item{Every value for ``@code{UNIXPATH}'' in any section (e.g. by adding a "-p2" suffix)} | 359 | @end itemize |
360 | \end{itemize} | 360 | to a fresh, unique value. Make sure that the PORT numbers stay below 65536. |
361 | to a fresh, unique value. Make sure that the PORT numbers stay | 361 | From now on, whenever you interact with the second peer, you need to specify |
362 | below 65536. From now on, whenever you interact with the second | 362 | @command{-c peer2.conf} as an additional command line argument. |
363 | peer, you need to specify @command{-c peer2.conf} as an additional | ||
364 | command line argument. | ||
365 | 363 | ||
366 | Now, generate the 2nd peer's private key: | 364 | Now, generate the 2nd peer's private key: |
367 | 365 | ||
@@ -384,16 +382,15 @@ $ ~/gnunet/src/dht/gnunet-dht-put -c peer2.conf -k KEY -d VALUE | |||
384 | $ ~/gnunet/src/dht/gnunet-dht-get -c peer2.conf -k KEY | 382 | $ ~/gnunet/src/dht/gnunet-dht-get -c peer2.conf -k KEY |
385 | @end example | 383 | @end example |
386 | If you want the two peers to connect, you have multiple options: | 384 | If you want the two peers to connect, you have multiple options: |
387 | \begin{itemize} | 385 | @itemize |
388 | \itemsep0em | 386 | @item UDP neighbour discovery (automatic) |
389 | \item UDP neighbour discovery (automatic) | 387 | @item Setup a bootstrap server |
390 | \item Setup a bootstrap server | 388 | @item Connect manually |
391 | \item Connect manually | 389 | @end itemize |
392 | \end{itemize} | ||
393 | To setup peer 1 as bootstrapping server change the configuration of | 390 | To setup peer 1 as bootstrapping server change the configuration of |
394 | the first one to be a hostlist server by adding the following lines to | 391 | the first one to be a hostlist server by adding the following lines to |
395 | @code{peer1.conf} to enable bootstrapping server: | 392 | @file{peer1.conf} to enable bootstrapping server: |
396 | @example | 393 | @example |
397 | [hostlist] | 394 | [hostlist] |
398 | OPTIONS = -p | 395 | OPTIONS = -p |
399 | @end example | 396 | @end example |
@@ -456,7 +453,6 @@ configuration file. Various available options and details about them can be | |||
456 | found in the testbed default configuration file @file{src/testbed/testbed.conf}. | 453 | found in the testbed default configuration file @file{src/testbed/testbed.conf}. |
457 | 454 | ||
458 | With the testbed API, a sample test case can be structured as follows: | 455 | With the testbed API, a sample test case can be structured as follows: |
459 | @c* <lynX> Is there a way to pick a more readable font for this include? | ||
460 | @example | 456 | @example |
461 | @verbatiminclude testbed_test.c | 457 | @verbatiminclude testbed_test.c |
462 | @end example | 458 | @end example |
@@ -483,8 +479,8 @@ waiting operations can be executed. Operations will be canceled if they are | |||
483 | marked as ``done'' before their completion. | 479 | marked as ``done'' before their completion. |
484 | 480 | ||
485 | An operation is treated as completed when it succeeds or fails. Completion of | 481 | An operation is treated as completed when it succeeds or fails. Completion of |
486 | an operation is either conveyed as events through @i{controller event | 482 | an operation is either conveyed as events through @i{controller event callback} |
487 | callback} or through respective operation completion callbacks. In functions | 483 | or through respective operation completion callbacks. In functions |
488 | which support completion notification through both controller event callback and | 484 | which support completion notification through both controller event callback and |
489 | operation completion callback, first the controller event callback will be | 485 | operation completion callback, first the controller event callback will be |
490 | called. If the operation is not marked as done in that callback or if the | 486 | called. If the operation is not marked as done in that callback or if the |
@@ -611,7 +607,7 @@ library is supposed to implement the IPC whereas the service provides | |||
611 | more persistent P2P functions. | 607 | more persistent P2P functions. |
612 | 608 | ||
613 | Exercise: Add a few command-line options and print them inside | 609 | Exercise: Add a few command-line options and print them inside |
614 | of @code{run}. What happens if the user gives invalid arguments?} | 610 | of @code{run}. What happens if the user gives invalid arguments? |
615 | 611 | ||
616 | @subsection Writing a Client Library} | 612 | @subsection Writing a Client Library} |
617 | 613 | ||
@@ -625,11 +621,11 @@ Then, a client-service protocol needs to be designed. This typically | |||
625 | involves defining various message formats in a header that will be | 621 | involves defining various message formats in a header that will be |
626 | included by both the service and the client library (but is otherwise | 622 | included by both the service and the client library (but is otherwise |
627 | not shared and hence located within the service's directory and not | 623 | not shared and hence located within the service's directory and not |
628 | installed by @command{make install}). Each message must start with a {\tt | 624 | installed by @command{make install}). Each message must start with a |
629 | struct GNUNET\_MessageHeader} and must be shorter than 64k. By | 625 | @code{struct GNUNET\_MessageHeader} and must be shorter than 64k. By |
630 | convention, all fields in IPC (and P2P) messages must be in big-endian | 626 | convention, all fields in IPC (and P2P) messages must be in big-endian |
631 | format (and thus should be read using {\tt ntohl} and similar | 627 | format (and thus should be read using @code{ntohl} and similar |
632 | functions and written using {\tt htonl} and similar functions). | 628 | functions and written using @code{htonl} and similar functions). |
633 | Unique message types must be defined for each message struct in the | 629 | Unique message types must be defined for each message struct in the |
634 | @file{gnunet\_protocols.h} header (or an extension-specific include | 630 | @file{gnunet\_protocols.h} header (or an extension-specific include |
635 | file). | 631 | file). |
@@ -642,26 +638,25 @@ with the service, a connection must be created: | |||
642 | @verbatiminclude tutorial-examples/003.c | 638 | @verbatiminclude tutorial-examples/003.c |
643 | @end example | 639 | @end example |
644 | 640 | ||
645 | As a result a {\tt GNUNET\_MQ\_Handle} is returned | 641 | As a result a @code{GNUNET\_MQ\_Handle} is returned |
646 | which can to used henceforth to transmit messages to | 642 | which can to used henceforth to transmit messages to the service. |
647 | the service. | ||
648 | The complete MQ API can be found in @file{gnunet\_mq\_lib.h}. | 643 | The complete MQ API can be found in @file{gnunet\_mq\_lib.h}. |
649 | The {\tt hanlders} array in the example above is incomplete. | 644 | The @code{hanlders} array in the example above is incomplete. |
650 | Here is where you will define which messages you expect to | 645 | Here is where you will define which messages you expect to |
651 | receive from the service, and which functions handle them. | 646 | receive from the service, and which functions handle them. |
652 | The {\tt error\_cb} is a function that is to be called whenever | 647 | The @code{error\_cb} is a function that is to be called whenever |
653 | there are errors communicating with the service. | 648 | there are errors communicating with the service. |
654 | 649 | ||
655 | @subsubsection Sending messages} | 650 | @subsubsection Sending messages} |
656 | 651 | ||
657 | In GNUnet, messages are always sent beginning with a {\tt struct GNUNET\_MessageHeader} | 652 | In GNUnet, messages are always sent beginning with a @code{struct GNUNET\_MessageHeader} |
658 | in big endian format. This header defines the size and the type of the | 653 | in big endian format. This header defines the size and the type of the |
659 | message, the payload follows after this header. | 654 | message, the payload follows after this header. |
660 | @example | 655 | @example |
661 | @verbatiminclude tutorial-examples/004.c | 656 | @verbatiminclude tutorial-examples/004.c |
662 | @end example | 657 | @end example |
663 | 658 | ||
664 | Existing message types are defined in @file{gnunet\_protocols.h}\\ | 659 | Existing message types are defined in @file{gnunet\_protocols.h}. |
665 | A common way to create a message is with an envelope: | 660 | A common way to create a message is with an envelope: |
666 | @example | 661 | @example |
667 | @verbatiminclude tutorial-examples/005.c | 662 | @verbatiminclude tutorial-examples/005.c |
@@ -672,37 +667,37 @@ unsigned integer in addition to the standard GNUnet MessageHeader. | |||
672 | Add a C struct and define a fresh protocol number for your message. | 667 | Add a C struct and define a fresh protocol number for your message. |
673 | Protocol numbers in gnunet-ext are defined in @file{gnunet-ext/src/include/gnunet_protocols_ext.h} | 668 | Protocol numbers in gnunet-ext are defined in @file{gnunet-ext/src/include/gnunet_protocols_ext.h} |
674 | 669 | ||
675 | Exercise: Find out how you can determine the number of messages in a message queue.} | 670 | Exercise: Find out how you can determine the number of messages in a message queue. |
676 | 671 | ||
677 | Exercise: Find out how you can determine when a message you have queued was actually transmitted.} | 672 | Exercise: Find out how you can determine when a message you have queued was actually transmitted. |
678 | 673 | ||
679 | Exercise: Define a helper function to transmit a 32-bit | 674 | Exercise: Define a helper function to transmit a 32-bit |
680 | unsigned integer (as payload) to a service using some given client | 675 | unsigned integer (as payload) to a service using some given client |
681 | handle.} | 676 | handle. |
682 | 677 | ||
683 | 678 | ||
684 | @subsubsection Receiving Replies from the Service} | 679 | @subsubsection Receiving Replies from the Service} |
685 | 680 | ||
686 | Clients can receive messages from the service using the handlers | 681 | Clients can receive messages from the service using the handlers |
687 | specified in the {\tt handlers} array we specified when connecting | 682 | specified in the @code{handlers} array we specified when connecting |
688 | to the service. Entries in the the array are usually created using | 683 | to the service. Entries in the the array are usually created using |
689 | one of two macros, depending on whether the message is fixed size | 684 | one of two macros, depending on whether the message is fixed size |
690 | or variable size. Variable size messages are managed using two | 685 | or variable size. Variable size messages are managed using two |
691 | callbacks, one to check that the message is well-formed, the other | 686 | callbacks, one to check that the message is well-formed, the other |
692 | to actually process the message. Fixed size messages are fully | 687 | to actually process the message. Fixed size messages are fully |
693 | checked by the MQ-logic, and thus only need to provide the handler | 688 | checked by the MQ-logic, and thus only need to provide the handler |
694 | to process the message. Note that the prefixes {\tt check\_} | 689 | to process the message. Note that the prefixes @code{check\_} |
695 | and {\tt handle\_} are mandatory. | 690 | and @code{handle\_} are mandatory. |
696 | @example | 691 | @example |
697 | @verbatiminclude tutorial-examples/006.c | 692 | @verbatiminclude tutorial-examples/006.c |
698 | @end example | 693 | @end example |
699 | 694 | ||
700 | Exercise: Expand your helper function to receive a response message | 695 | Exercise: Expand your helper function to receive a response message |
701 | (for example, containing just the {\tt struct GNUnet MessageHeader} | 696 | (for example, containing just the @code{struct GNUnet MessageHeader} |
702 | without any payload). Upon receiving the service's response, you | 697 | without any payload). Upon receiving the service's response, you |
703 | should call a callback provided to your helper function's API.} | 698 | should call a callback provided to your helper function's API. |
704 | 699 | ||
705 | Exercise: Figure out where you can pass values to the closures ({\tt cls}).} | 700 | Exercise: Figure out where you can pass values to the closures (@code{cls}). |
706 | 701 | ||
707 | 702 | ||
708 | @subsection Writing a user interface} | 703 | @subsection Writing a user interface} |
@@ -711,20 +706,17 @@ Given a client library, all it takes to access a service now is to | |||
711 | combine calls to the client library with parsing command-line | 706 | combine calls to the client library with parsing command-line |
712 | options. | 707 | options. |
713 | 708 | ||
714 | Exercise: Call your client API from your {\tt run()} method in your | 709 | Exercise: Call your client API from your @code{run()} method in your |
715 | client application to send a request to the service. For example, | 710 | client application to send a request to the service. For example, |
716 | send a 32-bit integer value based on a number given at the | 711 | send a 32-bit integer value based on a number given at the |
717 | command-line to the service.} | 712 | command-line to the service. |
718 | |||
719 | |||
720 | 713 | ||
721 | @section Writing a Service | 714 | @section Writing a Service |
722 | 715 | ||
723 | Before you can test the client you've written so far, you'll need to also | 716 | Before you can test the client you've written so far, you'll need to also |
724 | implement the corresponding service. | 717 | implement the corresponding service. |
725 | 718 | ||
726 | 719 | @subsection Code Placement | |
727 | @subsection Code Placement} | ||
728 | 720 | ||
729 | New services are placed in their own subdirectory under @file{gnunet/src}. | 721 | New services are placed in their own subdirectory under @file{gnunet/src}. |
730 | This subdirectory should contain the API implementation file @file{SERVICE\_api.c}, | 722 | This subdirectory should contain the API implementation file @file{SERVICE\_api.c}, |
@@ -735,14 +727,14 @@ and configuration files. | |||
735 | 727 | ||
736 | @subsection Starting a Service} | 728 | @subsection Starting a Service} |
737 | 729 | ||
738 | The key API definition for creating a service is the {\tt GNUNET\_SERVICE\_MAIN} macro: | 730 | The key API definition for creating a service is the @code{GNUNET\_SERVICE\_MAIN} macro: |
739 | @example | 731 | @example |
740 | @verbatiminclude tutorial-examples/007.c | 732 | @verbatiminclude tutorial-examples/007.c |
741 | @end example | 733 | @end example |
742 | 734 | ||
743 | In addition to the service name and flags, the macro takes three | 735 | In addition to the service name and flags, the macro takes three |
744 | functions, typically called {\tt run}, {\tt client\_connect\_cb} and | 736 | functions, typically called @code{run}, @code{client\_connect\_cb} and |
745 | {\tt client\_disconnect\_cb} as well as an array of message handlers | 737 | @code{client\_disconnect\_cb} as well as an array of message handlers |
746 | that will be called for incoming messages from clients. | 738 | that will be called for incoming messages from clients. |
747 | 739 | ||
748 | A minimal version of the three central service funtions would look | 740 | A minimal version of the three central service funtions would look |
@@ -752,25 +744,25 @@ like this: | |||
752 | @end example | 744 | @end example |
753 | 745 | ||
754 | Exercise: Write a stub service that processes no messages at all | 746 | Exercise: Write a stub service that processes no messages at all |
755 | in your code. Create a default configuration for it, integrate it | 747 | in your code. Create a default configuration for it, integrate it |
756 | with the build system and start the service from {\tt | 748 | with the build system and start the service from |
757 | gnunet-service-arm} using @command{gnunet-arm -i NAME}. | 749 | @command{gnunet-service-arm} using @command{gnunet-arm -i NAME}. |
758 | 750 | ||
759 | Exercise: Figure out how to set the closure ({\tt cls}) for handlers | 751 | Exercise: Figure out how to set the closure (@code{cls}) for handlers |
760 | of a service. | 752 | of a service. |
761 | 753 | ||
762 | Exercise: Figure out how to send messages from the service back to the | 754 | Exercise: Figure out how to send messages from the service back to the |
763 | client. | 755 | client. |
764 | 756 | ||
765 | Each handler function in the service {\bf must} eventually (possibly in some | 757 | Each handler function in the service @b{must} eventually (possibly in some |
766 | asynchronous continuation) call {\tt GNUNET\_SERVICE\_client\_continue()}. | 758 | asynchronous continuation) call @code{GNUNET\_SERVICE\_client\_continue()}. |
767 | Only after this call additional messages from the same client may | 759 | Only after this call additional messages from the same client may |
768 | be processed. This way, the service can throttle processing messages | 760 | be processed. This way, the service can throttle processing messages |
769 | from the same client. | 761 | from the same client. |
770 | 762 | ||
771 | Exercise: Change the service to ``handle'' the message from your | 763 | Exercise: Change the service to ``handle'' the message from your |
772 | client (for now, by printing a message). What happens if you | 764 | client (for now, by printing a message). What happens if you |
773 | forget to call {\tt GNUNET\_SERVICE\_client\_continue()}?} | 765 | forget to call @code{GNUNET\_SERVICE\_client\_continue()}? |
774 | 766 | ||
775 | 767 | ||
776 | @section Interacting directly with other Peers using the CORE Service | 768 | @section Interacting directly with other Peers using the CORE Service |
@@ -789,24 +781,24 @@ is connect to the @code{CORE} service using: | |||
789 | @subsection New P2P connections} | 781 | @subsection New P2P connections} |
790 | 782 | ||
791 | Before any traffic with a different peer can be exchanged, the peer must be | 783 | Before any traffic with a different peer can be exchanged, the peer must be |
792 | known to the service. This is notified by the @code{CORE} {\tt connects} callback, | 784 | known to the service. This is notified by the @code{CORE} @code{connects} callback, |
793 | which communicates the identity of the new peer to the service: | 785 | which communicates the identity of the new peer to the service: |
794 | @example | 786 | @example |
795 | @verbatiminclude tutorial-examples/010.c | 787 | @verbatiminclude tutorial-examples/010.c |
796 | @end example | 788 | @end example |
797 | 789 | ||
798 | Note that whatever you return from {\tt connects} is given as the | 790 | Note that whatever you return from @code{connects} is given as the |
799 | {\it cls} argument to the message handlers for messages from | 791 | @i{cls} argument to the message handlers for messages from |
800 | the respective peer. | 792 | the respective peer. |
801 | 793 | ||
802 | Exercise: Create a service that connects to the @code{CORE}. Then | 794 | Exercise: Create a service that connects to the @code{CORE}. Then |
803 | start (and connect) two peers and print a message once your connect | 795 | start (and connect) two peers and print a message once your connect |
804 | callback is invoked.} | 796 | callback is invoked. |
805 | 797 | ||
806 | @subsection Receiving P2P Messages | 798 | @subsection Receiving P2P Messages |
807 | 799 | ||
808 | To receive messages from @code{CORE}, you pass the desired | 800 | To receive messages from @code{CORE}, you pass the desired |
809 | {\em handlers} to the {\tt GNUNET\_CORE\_connect()} function, | 801 | @i{handlers} to the @code{GNUNET\_CORE\_connect()} function, |
810 | just as we showed for services. | 802 | just as we showed for services. |
811 | 803 | ||
812 | It is your responsibility to process messages fast enough or | 804 | It is your responsibility to process messages fast enough or |
@@ -822,9 +814,9 @@ the two peers are connected? Why? | |||
822 | 814 | ||
823 | @subsection Sending P2P Messages | 815 | @subsection Sending P2P Messages |
824 | 816 | ||
825 | You can transmit messages to other peers using the {\it mq} you were | 817 | You can transmit messages to other peers using the @i{mq} you were |
826 | given during the {\tt connect} callback. Note that the {\it mq} | 818 | given during the @code{connect} callback. Note that the @i{mq} |
827 | automatically is released upon {\tt disconnect} and that you must | 819 | automatically is released upon @code{disconnect} and that you must |
828 | not use it afterwards. | 820 | not use it afterwards. |
829 | 821 | ||
830 | It is your responsibility to not over-fill the message queue, GNUnet | 822 | It is your responsibility to not over-fill the message queue, GNUnet |
@@ -835,12 +827,12 @@ fast as possible to the other peer (the other peer should run a | |||
835 | service that ``processes'' those messages). How fast is the | 827 | service that ``processes'' those messages). How fast is the |
836 | transmission? Count using the STATISTICS service on both ends. Are | 828 | transmission? Count using the STATISTICS service on both ends. Are |
837 | messages lost? How can you transmit messages faster? What happens if | 829 | messages lost? How can you transmit messages faster? What happens if |
838 | you stop the peer that is receiving your messages?} | 830 | you stop the peer that is receiving your messages? |
839 | 831 | ||
840 | 832 | ||
841 | @subsection End of P2P connections} | 833 | @subsection End of P2P connections} |
842 | 834 | ||
843 | If a message handler returns {\tt GNUNET\_SYSERR}, the remote peer shuts down or | 835 | If a message handler returns @code{GNUNET\_SYSERR}, the remote peer shuts down or |
844 | there is an unrecoverable network disconnection, CORE notifies the service that | 836 | there is an unrecoverable network disconnection, CORE notifies the service that |
845 | the peer disconnected. After this notification no more messages will be received | 837 | the peer disconnected. After this notification no more messages will be received |
846 | from the peer and the service is no longer allowed to send messages to the peer. | 838 | from the peer and the service is no longer allowed to send messages to the peer. |
@@ -849,7 +841,7 @@ The disconnect callback looks like the following: | |||
849 | @verbatiminclude tutorial-examples/011.c | 841 | @verbatiminclude tutorial-examples/011.c |
850 | @end example | 842 | @end example |
851 | 843 | ||
852 | Exercise: Fix your service to handle peer disconnects.} | 844 | Exercise: Fix your service to handle peer disconnects. |
853 | 845 | ||
854 | @section Storing peer-specific data using the PEERSTORE service | 846 | @section Storing peer-specific data using the PEERSTORE service |
855 | 847 | ||
@@ -857,21 +849,20 @@ GNUnet's PEERSTORE service offers a persistorage for arbitrary peer-specific dat | |||
857 | Other GNUnet services can use the PEERSTORE to store, retrieve and monitor data records. | 849 | Other GNUnet services can use the PEERSTORE to store, retrieve and monitor data records. |
858 | Each data record stored with PEERSTORE contains the following fields: | 850 | Each data record stored with PEERSTORE contains the following fields: |
859 | 851 | ||
860 | \begin{itemize} | 852 | @itemize |
861 | \itemsep0em | 853 | @item subsystem: Name of the subsystem responsible for the record. |
862 | \item subsystem: Name of the subsystem responsible for the record. | 854 | @item peerid: Identity of the peer this record is related to. |
863 | \item peerid: Identity of the peer this record is related to. | 855 | @item key: a key string identifying the record. |
864 | \item key: a key string identifying the record. | 856 | @item value: binary record value. |
865 | \item value: binary record value. | 857 | @item expiry: record expiry date. |
866 | \item expiry: record expiry date. | 858 | @end itemize |
867 | \end{itemize} | ||
868 | 859 | ||
869 | The first step is to start a connection to the PEERSTORE service: | 860 | The first step is to start a connection to the PEERSTORE service: |
870 | @example | 861 | @example |
871 | @verbatiminclude tutorial-examples/012.c | 862 | @verbatiminclude tutorial-examples/012.c |
872 | @end example | 863 | @end example |
873 | 864 | ||
874 | The service handle \lstinline|peerstore_handle| will be needed for all subsequent | 865 | The service handle @code{peerstore_handle} will be needed for all subsequent |
875 | PEERSTORE operations. | 866 | PEERSTORE operations. |
876 | 867 | ||
877 | @subsection Storing records} | 868 | @subsection Storing records} |
@@ -881,16 +872,16 @@ To store a new record, use the following function: | |||
881 | @verbatiminclude tutorial-examples/013.c | 872 | @verbatiminclude tutorial-examples/013.c |
882 | @end example | 873 | @end example |
883 | 874 | ||
884 | The \lstinline|options| parameter can either be \lstinline|GNUNET_PEERSTORE_STOREOPTION_MULTIPLE| | 875 | The @code{options} parameter can either be @code{GNUNET_PEERSTORE_STOREOPTION_MULTIPLE} |
885 | which means that multiple values can be stored under the same key combination (subsystem, peerid, key), | 876 | which means that multiple values can be stored under the same key combination (subsystem, peerid, key), |
886 | or \lstinline|GNUNET_PEERSTORE_STOREOPTION_REPLACE| which means that PEERSTORE will replace any | 877 | or @code{GNUNET_PEERSTORE_STOREOPTION_REPLACE} which means that PEERSTORE will replace any |
887 | existing values under the given key combination (subsystem, peerid, key) with the new given value. | 878 | existing values under the given key combination (subsystem, peerid, key) with the new given value. |
888 | 879 | ||
889 | The continuation function \lstinline|cont| will be called after the store request is successfully | 880 | The continuation function @code{cont} will be called after the store request is successfully |
890 | sent to the PEERSTORE service. This does not guarantee that the record is successfully stored, only | 881 | sent to the PEERSTORE service. This does not guarantee that the record is successfully stored, only |
891 | that it was received by the service. | 882 | that it was received by the service. |
892 | 883 | ||
893 | The \lstinline|GNUNET_PEERSTORE_store| function returns a handle to the store operation. This handle | 884 | The @code{GNUNET_PEERSTORE_store} function returns a handle to the store operation. This handle |
894 | can be used to cancel the store operation only before the continuation function is called: | 885 | can be used to cancel the store operation only before the continuation function is called: |
895 | @example | 886 | @example |
896 | void | 887 | void |
@@ -904,22 +895,21 @@ To retrieve stored records, use the following function: | |||
904 | @verbatiminclude tutorial-examples/014.c | 895 | @verbatiminclude tutorial-examples/014.c |
905 | @end example | 896 | @end example |
906 | 897 | ||
907 | The values of \lstinline|peer| and \lstinline|key| can be \lstinline|NULL|. This allows the | 898 | The values of @code{peer} and @code{key} can be @code{NULL}. This allows the |
908 | iteration over values stored under any of the following key combinations: | 899 | iteration over values stored under any of the following key combinations: |
909 | \begin{itemize} | 900 | @itemize |
910 | \itemsep0em | 901 | @item (subsystem) |
911 | \item (subsystem) | 902 | @item (subsystem, peerid) |
912 | \item (subsystem, peerid) | 903 | @item (subsystem, key) |
913 | \item (subsystem, key) | 904 | @item (subsystem, peerid, key) |
914 | \item (subsystem, peerid, key) | 905 | @end itemize |
915 | \end{itemize} | 906 | |
916 | 907 | The @code{callback} function will be called once with each retrieved record and once | |
917 | The \lstinline|callback| function will be called once with each retrieved record and once | 908 | more with a @code{NULL} record to signal the end of results. |
918 | more with a \lstinline|NULL| record to signal the end of results. | 909 | |
919 | 910 | The @code{GNUNET_PEERSTORE_iterate} function returns a handle to the iterate operation. This | |
920 | The \lstinline|GNUNET_PEERSTORE_iterate| function returns a handle to the iterate operation. This | ||
921 | handle can be used to cancel the iterate operation only before the callback function is called with | 911 | handle can be used to cancel the iterate operation only before the callback function is called with |
922 | a \lstinline|NULL| record. | 912 | a @code{NULL} record. |
923 | 913 | ||
924 | @subsection Monitoring records | 914 | @subsection Monitoring records |
925 | 915 | ||
@@ -929,7 +919,7 @@ combination (subsystem, peerid, key). To start the monitoring, use the following | |||
929 | @verbatiminclude tutorial-examples/015.c | 919 | @verbatiminclude tutorial-examples/015.c |
930 | @end example | 920 | @end example |
931 | 921 | ||
932 | Whenever a new record is stored under the given key combination, the \lstinline|callback| function | 922 | Whenever a new record is stored under the given key combination, the @code{callback} function |
933 | will be called with this new record. This will continue until the connection to the PEERSTORE service | 923 | will be called with this new record. This will continue until the connection to the PEERSTORE service |
934 | is broken or the watch operation is canceled: | 924 | is broken or the watch operation is canceled: |
935 | @example | 925 | @example |
@@ -944,7 +934,7 @@ function: | |||
944 | @verbatiminclude tutorial-examples/017.c | 934 | @verbatiminclude tutorial-examples/017.c |
945 | @end example | 935 | @end example |
946 | 936 | ||
947 | If the \lstinline|sync_first| flag is set to \lstinline|GNUNET_YES|, the API will delay the | 937 | If the @code{sync_first} flag is set to @code{GNUNET_YES}, the API will delay the |
948 | disconnection until all store requests are received by the PEERSTORE service. Otherwise, | 938 | disconnection until all store requests are received by the PEERSTORE service. Otherwise, |
949 | it will disconnect immediately. | 939 | it will disconnect immediately. |
950 | 940 | ||
@@ -982,8 +972,8 @@ and other unfavorable events, just make several PUT requests! | |||
982 | @end example | 972 | @end example |
983 | 973 | ||
984 | Exercise: Store a value in the DHT periodically to make sure it is available | 974 | Exercise: Store a value in the DHT periodically to make sure it is available |
985 | over time. You might consider using the function GNUNET\_SCHEDULER\_add\_delayed and | 975 | over time. You might consider using the function @code{GNUNET\_SCHEDULER\_add\_delayed} |
986 | call GNUNET\_DHT\_put from inside a helper function.} | 976 | and call @code{GNUNET\_DHT\_put} from inside a helper function. |
987 | 977 | ||
988 | 978 | ||
989 | @subsection Obtaining data from the DHT | 979 | @subsection Obtaining data from the DHT |
@@ -994,9 +984,9 @@ API provides a callback. Once started, the request runs in the service, | |||
994 | the service will try to get as many results as possible (filtering out | 984 | the service will try to get as many results as possible (filtering out |
995 | duplicates) until the timeout expires or we explicitly stop the request. | 985 | duplicates) until the timeout expires or we explicitly stop the request. |
996 | It is possible to give a ``forever'' timeout with | 986 | It is possible to give a ``forever'' timeout with |
997 | {\tt GNUNET\_TIME\_UNIT\_FOREVER\_REL}. | 987 | @code{GNUNET\_TIME\_UNIT\_FOREVER\_REL}. |
998 | 988 | ||
999 | If we give a route option {\tt GNUNET\_DHT\_RO\_RECORD\_ROUTE} the callback | 989 | If we give a route option @code{GNUNET\_DHT\_RO\_RECORD\_ROUTE} the callback |
1000 | will get a list of all the peers the data has travelled, both on the PUT | 990 | will get a list of all the peers the data has travelled, both on the PUT |
1001 | path and on the GET path. | 991 | path and on the GET path. |
1002 | @example | 992 | @example |
@@ -1005,7 +995,7 @@ path and on the GET path. | |||
1005 | 995 | ||
1006 | Exercise: Store a value in the DHT and after a while retrieve it. Show the IDs of all | 996 | Exercise: Store a value in the DHT and after a while retrieve it. Show the IDs of all |
1007 | the peers the requests have gone through. In order to convert a peer ID to a string, use | 997 | the peers the requests have gone through. In order to convert a peer ID to a string, use |
1008 | the function GNUNET\_i2s. Pay attention to the route option parameters in both calls!} | 998 | the function @code{GNUNET\_i2s}. Pay attention to the route option parameters in both calls! |
1009 | 999 | ||
1010 | @subsection Implementing a block plugin | 1000 | @subsection Implementing a block plugin |
1011 | 1001 | ||
@@ -1021,13 +1011,13 @@ described in the following sections. | |||
1021 | @subsubsection Validating requests and replies | 1011 | @subsubsection Validating requests and replies |
1022 | 1012 | ||
1023 | The evaluate function should validate a reply or a request. It returns | 1013 | The evaluate function should validate a reply or a request. It returns |
1024 | a {\tt GNUNET\_BLOCK\_EvaluationResult}, which is an enumeration. All | 1014 | a @code{GNUNET\_BLOCK\_EvaluationResult}, which is an enumeration. All |
1025 | possible answers are in @file{gnunet\_block\_lib.h}. The function will | 1015 | possible answers are in @file{gnunet\_block\_lib.h}. The function will |
1026 | be called with a {\tt reply\_block} argument of {\tt NULL} for | 1016 | be called with a @code{reply\_block} argument of @code{NULL} for |
1027 | requests. Note that depending on how {\tt evaluate} is called, only | 1017 | requests. Note that depending on how @code{evaluate} is called, only |
1028 | some of the possible return values are valid. The specific meaning of | 1018 | some of the possible return values are valid. The specific meaning of |
1029 | the {\tt xquery} argument is application-specific. Applications that | 1019 | the @code{xquery} argument is application-specific. Applications that |
1030 | do not use an extended query should check that the {\tt xquery\_size} | 1020 | do not use an extended query should check that the @code{xquery\_size} |
1031 | is zero. The block group is typically used to filter duplicate | 1021 | is zero. The block group is typically used to filter duplicate |
1032 | replies. | 1022 | replies. |
1033 | @example | 1023 | @example |
@@ -1043,10 +1033,10 @@ circle in the network. | |||
1043 | @subsubsection Deriving a key from a reply | 1033 | @subsubsection Deriving a key from a reply |
1044 | 1034 | ||
1045 | The DHT can operate more efficiently if it is possible to derive a key | 1035 | The DHT can operate more efficiently if it is possible to derive a key |
1046 | from the value of the corresponding block. The {\tt get\_key} | 1036 | from the value of the corresponding block. The @code{get\_key} |
1047 | function is used to obtain the key of a block --- for example, by | 1037 | function is used to obtain the key of a block --- for example, by |
1048 | means of hashing. If deriving the key is not possible, the function | 1038 | means of hashing. If deriving the key is not possible, the function |
1049 | should simply return {\tt GNUNET\_SYSERR} (the DHT will still work | 1039 | should simply return @code{GNUNET\_SYSERR} (the DHT will still work |
1050 | just fine with such blocks). | 1040 | just fine with such blocks). |
1051 | @example | 1041 | @example |
1052 | @verbatiminclude tutorial-examples/022.c | 1042 | @verbatiminclude tutorial-examples/022.c |
@@ -1074,9 +1064,9 @@ little. | |||
1074 | 1064 | ||
1075 | @subsubsection Integration of the plugin with the build system | 1065 | @subsubsection Integration of the plugin with the build system |
1076 | 1066 | ||
1077 | In order to compile the plugin, the Makefile.am file for the | 1067 | In order to compile the plugin, the @file{Makefile.am} file for the |
1078 | service SERVICE should contain a rule similar to this: | 1068 | service SERVICE should contain a rule similar to this: |
1079 | @c* Actually this is a Makefile not c. But the whole structure of examples | 1069 | @c* Actually this is a Makefile not C. But the whole structure of examples |
1080 | @c* must be improved. | 1070 | @c* must be improved. |
1081 | @example | 1071 | @example |
1082 | @verbatiminclude tutorial-examples/025.c | 1072 | @verbatiminclude tutorial-examples/025.c |