aboutsummaryrefslogtreecommitdiff
path: root/doc
diff options
context:
space:
mode:
authorng0 <ng0@infotropique.org>2017-09-05 11:36:23 +0000
committerng0 <ng0@infotropique.org>2017-09-05 11:36:23 +0000
commit0c9ffbeba9f19c0bde8ee9c6d3bea04f8867885c (patch)
tree51ad32a91f2bf99a4ff444001efab014e900c0de /doc
parent3fd6fe49cd3801639fc26087838be589805f43d9 (diff)
downloadgnunet-0c9ffbeba9f19c0bde8ee9c6d3bea04f8867885c.tar.gz
gnunet-0c9ffbeba9f19c0bde8ee9c6d3bea04f8867885c.zip
doc: gnunet-c-tutorial: More @file, @code.
Diffstat (limited to 'doc')
-rw-r--r--doc/gnunet-c-tutorial.texi98
1 files changed, 52 insertions, 46 deletions
diff --git a/doc/gnunet-c-tutorial.texi b/doc/gnunet-c-tutorial.texi
index b4ed0e786..ba443e674 100644
--- a/doc/gnunet-c-tutorial.texi
+++ b/doc/gnunet-c-tutorial.texi
@@ -249,16 +249,24 @@ the programmer.
249 249
250@subsection Configure your peer 250@subsection Configure your peer
251 251
252First of all we need to configure your peer. Each peer is started with a configuration containing settings for GNUnet itself and its services. This configuration is based on the default configuration shipped with GNUnet and can be modified. The default configuration is located in the $PREFIX/share/gnunet/config.d directory. When starting a peer, you can specify a customized configuration using the the $-c$ command line switch when starting the ARM service and all other services. When using a modified configuration the default values are loaded and only values specified in the configuration file will replace the default values. 252First of all we need to configure your peer. Each peer is started with a configuration
253 253containing settings for GNUnet itself and its services. This configuration is based on the
254Since we want to start additional peers later, we need 254default configuration shipped with GNUnet and can be modified. The default configuration
255some modifications from the default configuration. We need to create a separate service home and a file containing our modifications for this peer: 255is located in the @file{$PREFIX/share/gnunet/config.d} directory. When starting a peer, you
256can specify a customized configuration using the the @command{-c} command line switch when
257starting the ARM service and all other services. When using a modified configuration the
258default values are loaded and only values specified in the configuration file will replace
259the default values.
260
261Since we want to start additional peers later, we need some modifications from the default
262configuration. We need to create a separate service home and a file containing our
263modifications for this peer:
256@example 264@example
257$ mkdir ~/gnunet1/ 265$ mkdir ~/gnunet1/
258$ touch peer1.conf 266$ touch peer1.conf
259@end example 267@end example
260 268
261Now add the following lines to peer1.conf to use this directory. For 269Now add the following lines to @file{peer1.conf} to use this directory. For
262simplified usage we want to prevent the peer to connect to the GNUnet 270simplified usage we want to prevent the peer to connect to the GNUnet
263network since this could lead to confusing output. This modifications 271network since this could lead to confusing output. This modifications
264will replace the default settings: 272will replace the default settings:
@@ -274,11 +282,11 @@ Each GNUnet instance (called peer) has an identity (peer ID) based on a
274cryptographic public private key pair. The peer ID is the printable hash of the 282cryptographic public private key pair. The peer ID is the printable hash of the
275public key. 283public key.
276 284
277GNUnet services are controlled by a master service the so called Automatic Restart Manager (ARM). 285GNUnet services are controlled by a master service, the so called @dfn{Automatic Restart Manager} (ARM).
278ARM starts, stops and even restarts services automatically or on demand when a client connects. 286ARM starts, stops and even restarts services automatically or on demand when a client connects.
279You interact with the ARM service using the gnunet-arm tool. 287You interact with the ARM service using the gnunet-arm tool.
280GNUnet can then be started with gnunet-arm -s and stopped with 288GNUnet can then be started with @command{gnunet-arm -s} and stopped with
281gnunet-arm -e. An additional service not automatically started 289@command{gnunet-arm -e}. An additional service not automatically started
282can be started using @command{gnunet-arm -i <service name>} and stopped 290can be started using @command{gnunet-arm -i <service name>} and stopped
283using @command{gnunet-arm -k <servicename>}. 291using @command{gnunet-arm -k <servicename>}.
284 292
@@ -298,9 +306,9 @@ I am peer `0PA02UVRKQTS2C .. JL5Q78F6H0B1ACPV1CJI59MEQUMQCC5G'.
298 306
299In this section, we will monitor the behaviour of our peer's DHT service with respect to a 307In this section, we will monitor the behaviour of our peer's DHT service with respect to a
300specific key. First we will start GNUnet and then start the DHT service and use the DHT monitor tool 308specific key. First we will start GNUnet and then start the DHT service and use the DHT monitor tool
301to monitor the PUT and GET commands we issue ussing the gnunet-dht-put and 309to monitor the PUT and GET commands we issue ussing the @command{gnunet-dht-put} and
302gnunet-dht-get commands. Using the ``monitor'' line given below, you can observe the behavior of 310@command{gnunet-dht-get} commands. Using the ``monitor'' line given below, you can observe
303your own peer's DHT with respect to the specified KEY: 311the behavior of your own peer's DHT with respect to the specified KEY:
304 312
305@example 313@example
306$ gnunet-arm -c ~/peer1.conf -s # start gnunet with all default services 314$ gnunet-arm -c ~/peer1.conf -s # start gnunet with all default services
@@ -308,7 +316,7 @@ $ gnunet-arm -c ~/peer1.conf -i dht # start DHT service
308$ cd ~/gnunet/src/dht; 316$ cd ~/gnunet/src/dht;
309$ ./gnunet-dht-monitor -c ~/peer1.conf -k KEY 317$ ./gnunet-dht-monitor -c ~/peer1.conf -k KEY
310@end example 318@end example
311Now open a separate terminal and change again to the gnunet/src/dht directory: 319Now open a separate terminal and change again to the @file{gnunet/src/dht} directory:
312@example 320@example
313$ cd ~/gnunet/src/dht 321$ cd ~/gnunet/src/dht
314$ ./gnunet-dht-put -c ~/peer1.conf -k KEY -d VALUE # put VALUE under KEY in the DHT 322$ ./gnunet-dht-put -c ~/peer1.conf -k KEY -d VALUE # put VALUE under KEY in the DHT
@@ -390,7 +398,7 @@ the first one to be a hostlist server by adding the following lines to
390OPTIONS = -p 398OPTIONS = -p
391@end example 399@end example
392 400
393Then change {\tt peer2.conf} and replace the ``@code{SERVERS}'' line in the ``@code{[hostlist]}'' section with 401Then change @file{peer2.conf} and replace the ``@code{SERVERS}'' line in the ``@code{[hostlist]}'' section with
394``@code{http://localhost:8080/}''. Restart both peers using: 402``@code{http://localhost:8080/}''. Restart both peers using:
395@example 403@example
396$ gnunet-arm -c peer1.conf -e # stop first peer 404$ gnunet-arm -c peer1.conf -e # stop first peer
@@ -408,13 +416,12 @@ by you.
408@subsubsection How to connect manually 416@subsubsection How to connect manually
409 417
410If you want to use the @code{peerinfo} tool to connect your peers, you should: 418If you want to use the @code{peerinfo} tool to connect your peers, you should:
411\begin{itemize} 419@itemize
412\itemsep0em 420@item Set @code{FORCESTART = NO} in section @code{hostlist} (to not connect to the global GNUnet)
413 \item{Set {\tt FORCESTART = NO} in section {\tt hostlist} (to not connect to the global GNUnet)} 421@item Start both peers running @command{gnunet-arm -c peer1.conf -s} and @command{gnunet-arm -c peer2.conf -s}
414 \item{Start both peers running {\tt gnunet-arm -c peer1.conf -s} and {\tt gnunet-arm -c peer2.conf -s}} 422@item Get @code{HELLO} message of the first peer running @command{gnunet-peerinfo -c peer1.conf -g}
415 \item{Get @code{HELLO} message of the first peer running {\tt gnunet-peerinfo -c peer1.conf -g}} 423@item Give the output to the second peer by running @command{gnunet-peerinfo -c peer2.conf -p '<output>'}
416 \item{Give the output to the second peer by running {\tt gnunet-peerinfo -c peer2.conf -p '<output>'}} 424@end itemize
417\end{itemize}
418 425
419Check that they are connected using @command{gnunet-core -c peer1.conf}, 426Check that they are connected using @command{gnunet-core -c peer1.conf},
420which should give you the other peer's peer identity: 427which should give you the other peer's peer identity:
@@ -440,7 +447,7 @@ environment on a single host.
440 447
441This function takes a configuration file which will be used as a template 448This function takes a configuration file which will be used as a template
442configuration for the peers. The testbed takes care of modifying relevant 449configuration for the peers. The testbed takes care of modifying relevant
443options in the peers' configuration such as SERVICEHOME, PORT, UNIXPATH to 450options in the peers' configuration such as @code{SERVICEHOME}, @code{PORT}, @code{UNIXPATH} to
444unique values so that peers run without running into conflicts. It also checks 451unique values so that peers run without running into conflicts. It also checks
445and assigns the ports in configurations only if they are free. 452and assigns the ports in configurations only if they are free.
446 453
@@ -449,7 +456,7 @@ configuration file. Various available options and details about them can be
449found in the testbed default configuration file @file{src/testbed/testbed.conf}. 456found in the testbed default configuration file @file{src/testbed/testbed.conf}.
450 457
451With the testbed API, a sample test case can be structured as follows: 458With the testbed API, a sample test case can be structured as follows:
452% <lynX> Is there a way to pick a more readable font for this include? 459@c* <lynX> Is there a way to pick a more readable font for this include?
453\lstset{language=C} 460\lstset{language=C}
454\lstinputlisting{testbed_test.c} 461\lstinputlisting{testbed_test.c}
455The source code for the above listing can be found at 462The source code for the above listing can be found at
@@ -475,7 +482,7 @@ waiting operations can be executed. Operations will be canceled if they are
475marked as ``done'' before their completion. 482marked as ``done'' before their completion.
476 483
477An operation is treated as completed when it succeeds or fails. Completion of 484An operation is treated as completed when it succeeds or fails. Completion of
478an operation is either conveyed as events through \textit{controller event 485an operation is either conveyed as events through @i{controller event
479 callback} or through respective operation completion callbacks. In functions 486 callback} or through respective operation completion callbacks. In functions
480which support completion notification through both controller event callback and 487which support completion notification through both controller event callback and
481operation completion callback, first the controller event callback will be 488operation completion callback, first the controller event callback will be
@@ -493,13 +500,13 @@ system is discouraged as their locations are dynamically created and will be
493different among various runs of testbed. To make access to these configurations 500different among various runs of testbed. To make access to these configurations
494easy, testbed API provides the function 501easy, testbed API provides the function
495@code{GNUNET\_TESTBED\_service\_connect()}. This function fetches the 502@code{GNUNET\_TESTBED\_service\_connect()}. This function fetches the
496configuration of a given peer and calls the \textit{Connect Adapter}. 503configuration of a given peer and calls the @i{Connect Adapter}.
497In the example code, it is the @code{dht\_ca}. A connect adapter is expected 504In the example code, it is the @code{dht\_ca}. A connect adapter is expected
498to open the connection to the needed service by using the provided configuration 505to open the connection to the needed service by using the provided configuration
499and return the created service connection handle. Successful connection to the 506and return the created service connection handle. Successful connection to the
500needed service is signaled through @code{service\_connect\_comp\_cb}. 507needed service is signaled through @code{service\_connect\_comp\_cb}.
501 508
502A dual to connect adapter is the \textit{Disconnect Adapter}. This callback is 509A dual to connect adapter is the @i{Disconnect Adapter}. This callback is
503called after the connect adapter has been called when the operation from 510called after the connect adapter has been called when the operation from
504@code{GNUNET\_TESTBED\_service\_connect()} is marked as ``done''. It has to 511@code{GNUNET\_TESTBED\_service\_connect()} is marked as ``done''. It has to
505disconnect from the service with the provided service handle (@code{op\_result}). 512disconnect from the service with the provided service handle (@code{op\_result}).
@@ -567,11 +574,10 @@ in the @file{gnunet-ext} root.
567@section Writing a Client Application 574@section Writing a Client Application
568 575
569When writing any client application (for example, a command-line 576When writing any client application (for example, a command-line
570tool), the basic structure is to start with the {\tt 577tool), the basic structure is to start with the @code{GNUNET\_PROGRAM\_run}
571 GNUNET\_PROGRAM\_run} function. This function will parse 578function. This function will parse command-line options, setup the scheduler
572command-line options, setup the scheduler and then invoke the {\tt 579and then invoke the @code{run} function (with the remaining non-option arguments)
573 run} function (with the remaining non-option arguments) and a handle 580and a handle to the parsed configuration (and the configuration file name that was
574to the parsed configuration (and the configuration file name that was
575used, which is typically not needed): 581used, which is typically not needed):
576 582
577\lstset{language=C} 583\lstset{language=C}
@@ -610,8 +616,8 @@ main (int argc, char *const *argv)
610 616
611Options can then be added easily by adding global variables and 617Options can then be added easily by adding global variables and
612expanding the {\tt options} array. For example, the following would 618expanding the {\tt options} array. For example, the following would
613add a string-option and a binary flag (defaulting to {\tt NULL} and 619add a string-option and a binary flag (defaulting to @code{NULL} and
614{\tt GNUNET\_NO} respectively): 620@code{GNUNET\_NO} respectively):
615 621
616\lstset{language=C} 622\lstset{language=C}
617\begin{lstlisting} 623\begin{lstlisting}
@@ -634,8 +640,8 @@ static int a_flag;
634@end example 640@end example
635 641
636Issues such as displaying some helpful text describing options using 642Issues such as displaying some helpful text describing options using
637the {\tt --help} argument and error handling are taken care of when 643the @code{--help} argument and error handling are taken care of when
638using this approach. Other {\tt GNUNET\_GETOPT\_}-functions can be used 644using this approach. Other @code{GNUNET\_GETOPT\_}-functions can be used
639to obtain integer value options, increment counters, etc. You can 645to obtain integer value options, increment counters, etc. You can
640even write custom option parsers for special circumstances not covered 646even write custom option parsers for special circumstances not covered
641by the available handlers. To check if an argument was specified by the 647by the available handlers. To check if an argument was specified by the
@@ -643,14 +649,14 @@ user you initialize the variable with a specific value (e.g. NULL for
643a string and GNUNET\_SYSERR for a integer) and check after parsing 649a string and GNUNET\_SYSERR for a integer) and check after parsing
644happened if the values were modified. 650happened if the values were modified.
645 651
646Inside the {\tt run} method, the program would perform the 652Inside the @code{run} method, the program would perform the
647application-specific logic, which typically involves initializing and 653application-specific logic, which typically involves initializing and
648using some client library to interact with the service. The client 654using some client library to interact with the service. The client
649library is supposed to implement the IPC whereas the service provides 655library is supposed to implement the IPC whereas the service provides
650more persistent P2P functions. 656more persistent P2P functions.
651 657
652Exercise: Add a few command-line options and print them inside 658Exercise: Add a few command-line options and print them inside
653of {\tt run}. What happens if the user gives invalid arguments?} 659of @code{run}. What happens if the user gives invalid arguments?}
654 660
655@subsection Writing a Client Library} 661@subsection Writing a Client Library}
656 662
@@ -664,7 +670,7 @@ Then, a client-service protocol needs to be designed. This typically
664involves defining various message formats in a header that will be 670involves defining various message formats in a header that will be
665included by both the service and the client library (but is otherwise 671included by both the service and the client library (but is otherwise
666not shared and hence located within the service's directory and not 672not shared and hence located within the service's directory and not
667installed by {\tt make install}). Each message must start with a {\tt 673installed by @command{make install}). Each message must start with a {\tt
668 struct GNUNET\_MessageHeader} and must be shorter than 64k. By 674 struct GNUNET\_MessageHeader} and must be shorter than 64k. By
669convention, all fields in IPC (and P2P) messages must be in big-endian 675convention, all fields in IPC (and P2P) messages must be in big-endian
670format (and thus should be read using {\tt ntohl} and similar 676format (and thus should be read using {\tt ntohl} and similar
@@ -692,7 +698,7 @@ with the service, a connection must be created:
692As a result a {\tt GNUNET\_MQ\_Handle} is returned 698As a result a {\tt GNUNET\_MQ\_Handle} is returned
693which can to used henceforth to transmit messages to 699which can to used henceforth to transmit messages to
694the service. 700the service.
695The complete MQ API can be found in {\tt gnunet\_mq\_lib.h}. 701The complete MQ API can be found in @file{gnunet\_mq\_lib.h}.
696The {\tt hanlders} array in the example above is incomplete. 702The {\tt hanlders} array in the example above is incomplete.
697Here is where you will define which messages you expect to 703Here is where you will define which messages you expect to
698receive from the service, and which functions handle them. 704receive from the service, and which functions handle them.
@@ -880,13 +886,13 @@ client_disconnect_cb (void *cls,
880Exercise: Write a stub service that processes no messages at all 886Exercise: Write a stub service that processes no messages at all
881 in your code. Create a default configuration for it, integrate it 887 in your code. Create a default configuration for it, integrate it
882 with the build system and start the service from {\tt 888 with the build system and start the service from {\tt
883 gnunet-service-arm} using {\tt gnunet-arm -i NAME}.} 889 gnunet-service-arm} using @command{gnunet-arm -i NAME}.
884 890
885Exercise: Figure out how to set the closure ({\tt cls}) for handlers 891Exercise: Figure out how to set the closure ({\tt cls}) for handlers
886 of a service.} 892 of a service.
887 893
888Exercise: Figure out how to send messages from the service back to the 894Exercise: Figure out how to send messages from the service back to the
889 client.} 895 client.
890 896
891Each handler function in the service {\bf must} eventually (possibly in some 897Each handler function in the service {\bf must} eventually (possibly in some
892asynchronous continuation) call {\tt GNUNET\_SERVICE\_client\_continue()}. 898asynchronous continuation) call {\tt GNUNET\_SERVICE\_client\_continue()}.
@@ -961,7 +967,7 @@ to not keep a very long queue in memory.
961Exercise: Start one peer with a new service that has a message 967Exercise: Start one peer with a new service that has a message
962handler and start a second peer that only has your ``old'' service 968handler and start a second peer that only has your ``old'' service
963without message handlers. Which ``connect'' handlers are invoked when 969without message handlers. Which ``connect'' handlers are invoked when
964the two peers are connected? Why?} 970the two peers are connected? Why?
965 971
966 972
967@subsection Sending P2P Messages 973@subsection Sending P2P Messages
@@ -1451,13 +1457,13 @@ restarted service with the peer.
1451 1457
1452GNUnet provides a powerful logging mechanism providing log levels @code{ERROR}, 1458GNUnet provides a powerful logging mechanism providing log levels @code{ERROR},
1453@code{WARNING}, @code{INFO} and @code{DEBUG}. The current log level is 1459@code{WARNING}, @code{INFO} and @code{DEBUG}. The current log level is
1454configured using the \lstinline|$GNUNET_FORCE_LOG| environmental variable. 1460configured using the @code{$GNUNET_FORCE_LOG} environmental variable.
1455The @code{DEBUG} level is only available if \lstinline|--enable-logging=verbose| was used when 1461The @code{DEBUG} level is only available if @command{--enable-logging=verbose} was used when
1456running @code{configure}. More details about logging can be found under 1462running @command{configure}. More details about logging can be found under
1457@uref{https://gnunet.org/logging}. 1463@uref{https://gnunet.org/logging}.
1458 1464
1459You should also probably enable the creation of core files, by setting 1465You should also probably enable the creation of core files, by setting
1460{\tt ulimit}, and echo'ing 1 into @file{/proc/sys/kernel/core\_uses\_pid}. 1466@code{ulimit}, and echo'ing 1 into @file{/proc/sys/kernel/core\_uses\_pid}.
1461Then you can investigate the core dumps with @command{gdb}, which is often 1467Then you can investigate the core dumps with @command{gdb}, which is often
1462the fastest method to find simple errors. 1468the fastest method to find simple errors.
1463 1469