diff options
author | ng0 <ng0@infotropique.org> | 2017-09-05 12:22:34 +0000 |
---|---|---|
committer | ng0 <ng0@infotropique.org> | 2017-09-05 12:22:34 +0000 |
commit | 298c53e73003a7e024713dcf777a6e1c16f5e144 (patch) | |
tree | 75d10e3cf0fb0b589aa85070b6c3f977e4e6d419 /doc | |
parent | 0406fa6f2e3b6014f465c4d987759324065375df (diff) | |
download | gnunet-298c53e73003a7e024713dcf777a6e1c16f5e144.tar.gz gnunet-298c53e73003a7e024713dcf777a6e1c16f5e144.zip |
doc: gnunet-c-tutorial: more includes of examples.
Diffstat (limited to 'doc')
-rw-r--r-- | doc/gnunet-c-tutorial.texi | 314 | ||||
-rw-r--r-- | doc/tutorial-examples/007.c | 10 | ||||
-rw-r--r-- | doc/tutorial-examples/008.c | 22 | ||||
-rw-r--r-- | doc/tutorial-examples/009.c | 9 | ||||
-rw-r--r-- | doc/tutorial-examples/010.c | 8 | ||||
-rw-r--r-- | doc/tutorial-examples/011.c | 8 | ||||
-rw-r--r-- | doc/tutorial-examples/012.c | 4 | ||||
-rw-r--r-- | doc/tutorial-examples/013.c | 12 | ||||
-rw-r--r-- | doc/tutorial-examples/014.c | 9 | ||||
-rw-r--r-- | doc/tutorial-examples/015.c | 8 | ||||
-rw-r--r-- | doc/tutorial-examples/016.c | 3 | ||||
-rw-r--r-- | doc/tutorial-examples/017.c | 3 | ||||
-rw-r--r-- | doc/tutorial-examples/018.c | 2 | ||||
-rw-r--r-- | doc/tutorial-examples/019.c | 15 | ||||
-rw-r--r-- | doc/tutorial-examples/020.c | 24 | ||||
-rw-r--r-- | doc/tutorial-examples/021.c | 13 | ||||
-rw-r--r-- | doc/tutorial-examples/022.c | 8 | ||||
-rw-r--r-- | doc/tutorial-examples/023.c | 17 | ||||
-rw-r--r-- | doc/tutorial-examples/024.c | 9 | ||||
-rw-r--r-- | doc/tutorial-examples/025.c | 15 | ||||
-rw-r--r-- | doc/tutorial-examples/026.c | 52 |
21 files changed, 292 insertions, 273 deletions
diff --git a/doc/gnunet-c-tutorial.texi b/doc/gnunet-c-tutorial.texi index 6b2c770f3..c62c9c076 100644 --- a/doc/gnunet-c-tutorial.texi +++ b/doc/gnunet-c-tutorial.texi | |||
@@ -736,18 +736,8 @@ and configuration files. | |||
736 | @subsection Starting a Service} | 736 | @subsection Starting a Service} |
737 | 737 | ||
738 | The key API definition for creating a service is the {\tt GNUNET\_SERVICE\_MAIN} macro: | 738 | The key API definition for creating a service is the {\tt GNUNET\_SERVICE\_MAIN} macro: |
739 | \lstset{language=C} | 739 | @example |
740 | \begin{lstlisting} | 740 | @verbatiminclude tutorial-examples/007.c |
741 | GNUNET_SERVICE_MAIN | ||
742 | ("service-name", | ||
743 | GNUNET_SERVICE_OPTION_NONE, | ||
744 | &run, | ||
745 | &client_connect_cb, | ||
746 | &client_disconnect_cb, | ||
747 | NULL, | ||
748 | GNUNET_MQ_hd_fixed_size (...), | ||
749 | GNUNET_MQ_hd_var_size (...), | ||
750 | GNUNET_MQ_handler_end ()); | ||
751 | @end example | 741 | @end example |
752 | 742 | ||
753 | In addition to the service name and flags, the macro takes three | 743 | In addition to the service name and flags, the macro takes three |
@@ -757,31 +747,8 @@ that will be called for incoming messages from clients. | |||
757 | 747 | ||
758 | A minimal version of the three central service funtions would look | 748 | A minimal version of the three central service funtions would look |
759 | like this: | 749 | like this: |
760 | 750 | @example | |
761 | \lstset{language=c} | 751 | @verbatiminclude tutorial-examples/008.c |
762 | \begin{lstlisting} | ||
763 | static void | ||
764 | run (void *cls, | ||
765 | const struct GNUNET_CONFIGURATION_Handle *c, | ||
766 | struct GNUNET_SERVICE_Handle *service) | ||
767 | { | ||
768 | } | ||
769 | |||
770 | static void * | ||
771 | client_connect_cb (void *cls, | ||
772 | struct GNUNET_SERVICE_Client *c, | ||
773 | struct GNUNET_MQ_Handle *mq) | ||
774 | { | ||
775 | return c; | ||
776 | } | ||
777 | |||
778 | static void | ||
779 | client_disconnect_cb (void *cls, | ||
780 | struct GNUNET_SERVICE_Client *c, | ||
781 | void *internal_cls) | ||
782 | { | ||
783 | GNUNET_assert (c == internal_cls); | ||
784 | } | ||
785 | @end example | 752 | @end example |
786 | 753 | ||
787 | Exercise: Write a stub service that processes no messages at all | 754 | Exercise: Write a stub service that processes no messages at all |
@@ -815,18 +782,8 @@ managing connections between peers and handling encryption between peers. | |||
815 | 782 | ||
816 | One of the first things any service that extends the P2P protocol typically does | 783 | One of the first things any service that extends the P2P protocol typically does |
817 | is connect to the @code{CORE} service using: | 784 | is connect to the @code{CORE} service using: |
818 | 785 | @example | |
819 | \lstset{language=C} | 786 | @verbatiminclude tutorial-examples/009.c |
820 | \begin{lstlisting} | ||
821 | #include <gnunet/gnunet_core_service.h> | ||
822 | |||
823 | struct GNUNET_CORE_Handle * | ||
824 | GNUNET_CORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
825 | void *cls, | ||
826 | GNUNET_CORE_StartupCallback init, | ||
827 | GNUNET_CORE_ConnectEventHandler connects, | ||
828 | GNUNET_CORE_DisconnectEventHandler disconnects, | ||
829 | const struct GNUNET_MQ_MessageHandler *handlers); | ||
830 | @end example | 787 | @end example |
831 | 788 | ||
832 | @subsection New P2P connections} | 789 | @subsection New P2P connections} |
@@ -834,16 +791,8 @@ GNUNET_CORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
834 | Before any traffic with a different peer can be exchanged, the peer must be | 791 | Before any traffic with a different peer can be exchanged, the peer must be |
835 | known to the service. This is notified by the @code{CORE} {\tt connects} callback, | 792 | known to the service. This is notified by the @code{CORE} {\tt connects} callback, |
836 | which communicates the identity of the new peer to the service: | 793 | which communicates the identity of the new peer to the service: |
837 | 794 | @example | |
838 | \lstset{language=C} | 795 | @verbatiminclude tutorial-examples/010.c |
839 | \begin{lstlisting} | ||
840 | void * | ||
841 | connects (void *cls, | ||
842 | const struct GNUNET_PeerIdentity *peer, | ||
843 | struct GNUNET_MQ_Handle *mq) | ||
844 | { | ||
845 | return mq; | ||
846 | } | ||
847 | @end example | 796 | @end example |
848 | 797 | ||
849 | Note that whatever you return from {\tt connects} is given as the | 798 | Note that whatever you return from {\tt connects} is given as the |
@@ -896,16 +845,8 @@ there is an unrecoverable network disconnection, CORE notifies the service that | |||
896 | the peer disconnected. After this notification no more messages will be received | 845 | the peer disconnected. After this notification no more messages will be received |
897 | from the peer and the service is no longer allowed to send messages to the peer. | 846 | from the peer and the service is no longer allowed to send messages to the peer. |
898 | The disconnect callback looks like the following: | 847 | The disconnect callback looks like the following: |
899 | 848 | @example | |
900 | \lstset{language=C} | 849 | @verbatiminclude tutorial-examples/011.c |
901 | \begin{lstlisting} | ||
902 | void | ||
903 | disconnects (void *cls, | ||
904 | const struct GNUNET_PeerIdentity * peer) | ||
905 | { | ||
906 | /* Remove peer's identity from known peers */ | ||
907 | /* Make sure no messages are sent to peer from now on */ | ||
908 | } | ||
909 | @end example | 850 | @end example |
910 | 851 | ||
911 | Exercise: Fix your service to handle peer disconnects.} | 852 | Exercise: Fix your service to handle peer disconnects.} |
@@ -926,29 +867,18 @@ Each data record stored with PEERSTORE contains the following fields: | |||
926 | \end{itemize} | 867 | \end{itemize} |
927 | 868 | ||
928 | The first step is to start a connection to the PEERSTORE service: | 869 | The first step is to start a connection to the PEERSTORE service: |
929 | \begin{lstlisting} | 870 | @example |
930 | #include "gnunet_peerstore_service.h" | 871 | @verbatiminclude tutorial-examples/012.c |
931 | |||
932 | peerstore_handle = GNUNET_PEERSTORE_connect (cfg); | ||
933 | @end example | 872 | @end example |
873 | |||
934 | The service handle \lstinline|peerstore_handle| will be needed for all subsequent | 874 | The service handle \lstinline|peerstore_handle| will be needed for all subsequent |
935 | PEERSTORE operations. | 875 | PEERSTORE operations. |
936 | 876 | ||
937 | @subsection Storing records} | 877 | @subsection Storing records} |
938 | 878 | ||
939 | To store a new record, use the following function: | 879 | To store a new record, use the following function: |
940 | \begin{lstlisting} | 880 | @example |
941 | struct GNUNET_PEERSTORE_StoreContext * | 881 | @verbatiminclude tutorial-examples/013.c |
942 | GNUNET_PEERSTORE_store (struct GNUNET_PEERSTORE_Handle *h, | ||
943 | const char *sub_system, | ||
944 | const struct GNUNET_PeerIdentity *peer, | ||
945 | const char *key, | ||
946 | const void *value, | ||
947 | size_t size, | ||
948 | struct GNUNET_TIME_Absolute expiry, | ||
949 | enum GNUNET_PEERSTORE_StoreOption options, | ||
950 | GNUNET_PEERSTORE_Continuation cont, | ||
951 | void *cont_cls); | ||
952 | @end example | 882 | @end example |
953 | 883 | ||
954 | The \lstinline|options| parameter can either be \lstinline|GNUNET_PEERSTORE_STOREOPTION_MULTIPLE| | 884 | The \lstinline|options| parameter can either be \lstinline|GNUNET_PEERSTORE_STOREOPTION_MULTIPLE| |
@@ -962,7 +892,7 @@ that it was received by the service. | |||
962 | 892 | ||
963 | The \lstinline|GNUNET_PEERSTORE_store| function returns a handle to the store operation. This handle | 893 | The \lstinline|GNUNET_PEERSTORE_store| function returns a handle to the store operation. This handle |
964 | can be used to cancel the store operation only before the continuation function is called: | 894 | can be used to cancel the store operation only before the continuation function is called: |
965 | \begin{lstlisting} | 895 | @example |
966 | void | 896 | void |
967 | GNUNET_PEERSTORE_store_cancel (struct GNUNET_PEERSTORE_StoreContext *sc); | 897 | GNUNET_PEERSTORE_store_cancel (struct GNUNET_PEERSTORE_StoreContext *sc); |
968 | @end example | 898 | @end example |
@@ -970,16 +900,10 @@ GNUNET_PEERSTORE_store_cancel (struct GNUNET_PEERSTORE_StoreContext *sc); | |||
970 | @subsection Retrieving records | 900 | @subsection Retrieving records |
971 | 901 | ||
972 | To retrieve stored records, use the following function: | 902 | To retrieve stored records, use the following function: |
973 | \begin{lstlisting} | 903 | @example |
974 | struct GNUNET_PEERSTORE_IterateContext * | 904 | @verbatiminclude tutorial-examples/014.c |
975 | GNUNET_PEERSTORE_iterate (struct GNUNET_PEERSTORE_Handle *h, | ||
976 | const char *sub_system, | ||
977 | const struct GNUNET_PeerIdentity *peer, | ||
978 | const char *key, | ||
979 | struct GNUNET_TIME_Relative timeout, | ||
980 | GNUNET_PEERSTORE_Processor callback, | ||
981 | void *callback_cls); | ||
982 | @end example | 905 | @end example |
906 | |||
983 | The values of \lstinline|peer| and \lstinline|key| can be \lstinline|NULL|. This allows the | 907 | The values of \lstinline|peer| and \lstinline|key| can be \lstinline|NULL|. This allows the |
984 | iteration over values stored under any of the following key combinations: | 908 | iteration over values stored under any of the following key combinations: |
985 | \begin{itemize} | 909 | \begin{itemize} |
@@ -1001,31 +925,23 @@ a \lstinline|NULL| record. | |||
1001 | 925 | ||
1002 | PEERSTORE offers the functionality of monitoring for new records stored under a specific key | 926 | PEERSTORE offers the functionality of monitoring for new records stored under a specific key |
1003 | combination (subsystem, peerid, key). To start the monitoring, use the following function: | 927 | combination (subsystem, peerid, key). To start the monitoring, use the following function: |
1004 | \begin{lstlisting} | 928 | @example |
1005 | struct GNUNET_PEERSTORE_WatchContext * | 929 | @verbatiminclude tutorial-examples/015.c |
1006 | GNUNET_PEERSTORE_watch (struct GNUNET_PEERSTORE_Handle *h, | ||
1007 | const char *sub_system, | ||
1008 | const struct GNUNET_PeerIdentity *peer, | ||
1009 | const char *key, | ||
1010 | GNUNET_PEERSTORE_Processor callback, | ||
1011 | void *callback_cls); | ||
1012 | @end example | 930 | @end example |
1013 | 931 | ||
1014 | Whenever a new record is stored under the given key combination, the \lstinline|callback| function | 932 | Whenever a new record is stored under the given key combination, the \lstinline|callback| function |
1015 | will be called with this new record. This will continue until the connection to the PEERSTORE service | 933 | will be called with this new record. This will continue until the connection to the PEERSTORE service |
1016 | is broken or the watch operation is canceled: | 934 | is broken or the watch operation is canceled: |
1017 | \begin{lstlisting} | 935 | @example |
1018 | void | 936 | @verbatiminclude tutorial-examples/016.c |
1019 | GNUNET_PEERSTORE_watch_cancel (struct GNUNET_PEERSTORE_WatchContext *wc); | ||
1020 | @end example | 937 | @end example |
1021 | 938 | ||
1022 | @subsection Disconnecting from PEERSTORE | 939 | @subsection Disconnecting from PEERSTORE |
1023 | 940 | ||
1024 | When the connection to the PEERSTORE service is no longer needed, disconnect using the following | 941 | When the connection to the PEERSTORE service is no longer needed, disconnect using the following |
1025 | function: | 942 | function: |
1026 | \begin{lstlisting} | 943 | @example |
1027 | void | 944 | @verbatiminclude tutorial-examples/017.c |
1028 | GNUNET_PEERSTORE_disconnect (struct GNUNET_PEERSTORE_Handle *h, int sync_first); | ||
1029 | @end example | 945 | @end example |
1030 | 946 | ||
1031 | If the \lstinline|sync_first| flag is set to \lstinline|GNUNET_YES|, the API will delay the | 947 | If the \lstinline|sync_first| flag is set to \lstinline|GNUNET_YES|, the API will delay the |
@@ -1039,10 +955,10 @@ The DHT allows to store data so other peers in the P2P network can | |||
1039 | access it and retrieve data stored by any peers in the network. | 955 | access it and retrieve data stored by any peers in the network. |
1040 | This section will explain how to use the DHT. Of course, the first | 956 | This section will explain how to use the DHT. Of course, the first |
1041 | thing to do is to connect to the DHT service: | 957 | thing to do is to connect to the DHT service: |
1042 | \lstset{language=C} | 958 | @example |
1043 | \begin{lstlisting} | 959 | @verbatiminclude tutorial-examples/018.c |
1044 | dht_handle = GNUNET_DHT_connect (cfg, parallel_requests); | ||
1045 | @end example | 960 | @end example |
961 | |||
1046 | The second parameter indicates how many requests in parallel to expect. | 962 | The second parameter indicates how many requests in parallel to expect. |
1047 | It is not a hard limit, but a good approximation will make the DHT more | 963 | It is not a hard limit, but a good approximation will make the DHT more |
1048 | efficient. | 964 | efficient. |
@@ -1061,24 +977,8 @@ destination. Currently there is no feedback about whether or not the data | |||
1061 | has been sucessfully stored or where it has been stored. In order to improve | 977 | has been sucessfully stored or where it has been stored. In order to improve |
1062 | the availablilty of the data and to compensate for possible errors, peers leaving | 978 | the availablilty of the data and to compensate for possible errors, peers leaving |
1063 | and other unfavorable events, just make several PUT requests! | 979 | and other unfavorable events, just make several PUT requests! |
1064 | 980 | @example | |
1065 | \lstset{language=C} | 981 | @verbatiminclude tutorial-examples/019.c |
1066 | \begin{lstlisting} | ||
1067 | static void | ||
1068 | message_sent_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
1069 | { | ||
1070 | // Request has left local node | ||
1071 | } | ||
1072 | |||
1073 | struct GNUNET_DHT_PutHandle * | ||
1074 | GNUNET_DHT_put (struct GNUNET_DHT_Handle *handle, | ||
1075 | const struct GNUNET_HashCode *key, | ||
1076 | uint32_t desired_replication_level, | ||
1077 | enum GNUNET_DHT_RouteOption options, | ||
1078 | enum GNUNET_BLOCK_Type type, size_t size, const void *data, | ||
1079 | struct GNUNET_TIME_Absolute exp, | ||
1080 | struct GNUNET_TIME_Relative timeout, | ||
1081 | GNUNET_DHT_PutContinuation cont, void *cont_cls) | ||
1082 | @end example | 982 | @end example |
1083 | 983 | ||
1084 | Exercise: Store a value in the DHT periodically to make sure it is available | 984 | Exercise: Store a value in the DHT periodically to make sure it is available |
@@ -1099,31 +999,8 @@ It is possible to give a ``forever'' timeout with | |||
1099 | If we give a route option {\tt GNUNET\_DHT\_RO\_RECORD\_ROUTE} the callback | 999 | If we give a route option {\tt GNUNET\_DHT\_RO\_RECORD\_ROUTE} the callback |
1100 | will get a list of all the peers the data has travelled, both on the PUT | 1000 | will get a list of all the peers the data has travelled, both on the PUT |
1101 | path and on the GET path. | 1001 | path and on the GET path. |
1102 | \lstset{language=C} | 1002 | @example |
1103 | \begin{lstlisting} | 1003 | @verbatiminclude tutorial-examples/020.c |
1104 | static void | ||
1105 | get_result_iterator (void *cls, struct GNUNET_TIME_Absolute expiration, | ||
1106 | const struct GNUNET_HashCode *key, | ||
1107 | const struct GNUNET_PeerIdentity *get_path, | ||
1108 | unsigned int get_path_length, | ||
1109 | const struct GNUNET_PeerIdentity *put_path, | ||
1110 | unsigned int put_path_length, | ||
1111 | enum GNUNET_BLOCK_Type type, size_t size, const void *data) | ||
1112 | { | ||
1113 | // Optionally: | ||
1114 | GNUNET_DHT_get_stop (get_handle); | ||
1115 | } | ||
1116 | |||
1117 | get_handle = | ||
1118 | GNUNET_DHT_get_start (dht_handle, | ||
1119 | block_type, | ||
1120 | &key, | ||
1121 | replication, | ||
1122 | GNUNET_DHT_RO_NONE, | ||
1123 | NULL, | ||
1124 | 0, | ||
1125 | &get_result_iterator, | ||
1126 | cls) | ||
1127 | @end example | 1004 | @end example |
1128 | 1005 | ||
1129 | Exercise: Store a value in the DHT and after a while retrieve it. Show the IDs of all | 1006 | Exercise: Store a value in the DHT and after a while retrieve it. Show the IDs of all |
@@ -1153,21 +1030,8 @@ the {\tt xquery} argument is application-specific. Applications that | |||
1153 | do not use an extended query should check that the {\tt xquery\_size} | 1030 | do not use an extended query should check that the {\tt xquery\_size} |
1154 | is zero. The block group is typically used to filter duplicate | 1031 | is zero. The block group is typically used to filter duplicate |
1155 | replies. | 1032 | replies. |
1156 | 1033 | @example | |
1157 | \lstset{language=C} | 1034 | @verbatiminclude tutorial-examples/021.c |
1158 | \begin{lstlisting} | ||
1159 | static enum GNUNET_BLOCK_EvaluationResult | ||
1160 | block_plugin_SERVICE_evaluate (void *cls, | ||
1161 | enum GNUNET_BLOCK_Type type, | ||
1162 | struct GNUNET_BlockGroup *bg, | ||
1163 | const GNUNET_HashCode *query, | ||
1164 | const void *xquery, | ||
1165 | size_t xquery_size, | ||
1166 | const void *reply_block, | ||
1167 | size_t reply_block_size) | ||
1168 | { | ||
1169 | // Verify type, block and bg | ||
1170 | } | ||
1171 | @end example | 1035 | @end example |
1172 | 1036 | ||
1173 | Note that it is mandatory to detect duplicate replies in this function | 1037 | Note that it is mandatory to detect duplicate replies in this function |
@@ -1184,15 +1048,8 @@ function is used to obtain the key of a block --- for example, by | |||
1184 | means of hashing. If deriving the key is not possible, the function | 1048 | means of hashing. If deriving the key is not possible, the function |
1185 | should simply return {\tt GNUNET\_SYSERR} (the DHT will still work | 1049 | should simply return {\tt GNUNET\_SYSERR} (the DHT will still work |
1186 | just fine with such blocks). | 1050 | just fine with such blocks). |
1187 | |||
1188 | @example | 1051 | @example |
1189 | static int | 1052 | @verbatiminclude tutorial-examples/022.c |
1190 | block_plugin_SERVICE_get_key (void *cls, enum GNUNET_BLOCK_Type type, | ||
1191 | const void *block, size_t block_size, | ||
1192 | struct GNUNET_HashCode *key) | ||
1193 | { | ||
1194 | // Store the key in the key argument, return GNUNET_OK on success. | ||
1195 | } | ||
1196 | @end example | 1053 | @end example |
1197 | 1054 | ||
1198 | @subsubsection Initialization of the plugin | 1055 | @subsubsection Initialization of the plugin |
@@ -1202,24 +1059,8 @@ an initialization function which should initialize the plugin. The | |||
1202 | initialization function specifies what block types the plugin cares | 1059 | initialization function specifies what block types the plugin cares |
1203 | about and returns a struct with the functions that are to be used for | 1060 | about and returns a struct with the functions that are to be used for |
1204 | validation and obtaining keys (the ones just defined above). | 1061 | validation and obtaining keys (the ones just defined above). |
1205 | |||
1206 | @example | 1062 | @example |
1207 | void * | 1063 | @verbatiminclude tutorial-examples/023.c |
1208 | libgnunet_plugin_block_SERVICE_init (void *cls) | ||
1209 | { | ||
1210 | static enum GNUNET_BLOCK_Type types[] = | ||
1211 | { | ||
1212 | GNUNET_BLOCK_TYPE_SERVICE_BLOCKYPE, | ||
1213 | GNUNET_BLOCK_TYPE_ANY | ||
1214 | }; | ||
1215 | struct GNUNET_BLOCK_PluginFunctions *api; | ||
1216 | |||
1217 | api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions); | ||
1218 | api->evaluate = &block_plugin_SERICE_evaluate; | ||
1219 | api->get_key = &block_plugin_SERVICE_get_key; | ||
1220 | api->types = types; | ||
1221 | return api; | ||
1222 | } | ||
1223 | @end example | 1064 | @end example |
1224 | 1065 | ||
1225 | @subsubsection Shutdown of the plugin | 1066 | @subsubsection Shutdown of the plugin |
@@ -1227,42 +1068,20 @@ libgnunet_plugin_block_SERVICE_init (void *cls) | |||
1227 | Following GNUnet's general plugin API concept, the plugin must | 1068 | Following GNUnet's general plugin API concept, the plugin must |
1228 | export a second function for cleaning up. It usually does very | 1069 | export a second function for cleaning up. It usually does very |
1229 | little. | 1070 | little. |
1230 | |||
1231 | @example | 1071 | @example |
1232 | void * | 1072 | @verbatiminclude tutorial-examples/024.c |
1233 | libgnunet_plugin_block_SERVICE_done (void *cls) | ||
1234 | { | ||
1235 | struct GNUNET_TRANSPORT_PluginFunctions *api = cls; | ||
1236 | |||
1237 | GNUNET_free (api); | ||
1238 | return NULL; | ||
1239 | } | ||
1240 | @end example | 1073 | @end example |
1241 | 1074 | ||
1242 | |||
1243 | @subsubsection Integration of the plugin with the build system | 1075 | @subsubsection Integration of the plugin with the build system |
1244 | 1076 | ||
1245 | In order to compile the plugin, the Makefile.am file for the | 1077 | In order to compile the plugin, the Makefile.am file for the |
1246 | service SERVICE should contain a rule similar to this: | 1078 | service SERVICE should contain a rule similar to this: |
1247 | 1079 | @c* Actually this is a Makefile not c. But the whole structure of examples | |
1080 | @c* must be improved. | ||
1248 | @example | 1081 | @example |
1249 | plugindir = $(libdir)/gnunet | 1082 | @verbatiminclude tutorial-examples/025.c |
1250 | |||
1251 | plugin_LTLIBRARIES = \ | ||
1252 | libgnunet_plugin_block_ext.la | ||
1253 | libgnunet_plugin_block_ext_la_SOURCES = \ | ||
1254 | plugin_block_ext.c | ||
1255 | libgnunet_plugin_block_ext_la_LIBADD = \ | ||
1256 | $(prefix)/lib/libgnunethello.la \ | ||
1257 | $(prefix)/lib/libgnunetblock.la \ | ||
1258 | $(prefix)/lib/libgnunetutil.la | ||
1259 | libgnunet_plugin_block_ext_la_LDFLAGS = \ | ||
1260 | $(GN_PLUGIN_LDFLAGS) | ||
1261 | libgnunet_plugin_block_ext_la_DEPENDENCIES = \ | ||
1262 | $(prefix)/lib/libgnunetblock.la | ||
1263 | @end example | 1083 | @end example |
1264 | 1084 | ||
1265 | |||
1266 | Exercise: Write a block plugin that accepts all queries | 1085 | Exercise: Write a block plugin that accepts all queries |
1267 | and all replies but prints information about queries and replies | 1086 | and all replies but prints information about queries and replies |
1268 | when the respective validation hooks are called. | 1087 | when the respective validation hooks are called. |
@@ -1279,60 +1098,9 @@ different callbacks (one for each message type) and optional type and key parame | |||
1279 | to allow for filtering of messages. When an event happens, the appropiate callback | 1098 | to allow for filtering of messages. When an event happens, the appropiate callback |
1280 | is called with all the information about the event. | 1099 | is called with all the information about the event. |
1281 | @example | 1100 | @example |
1282 | static void | 1101 | @verbatiminclude tutorial-examples/026.c |
1283 | get_callback (void *cls, | ||
1284 | enum GNUNET_DHT_RouteOption options, | ||
1285 | enum GNUNET_BLOCK_Type type, | ||
1286 | uint32_t hop_count, | ||
1287 | uint32_t desired_replication_level, | ||
1288 | unsigned int path_length, | ||
1289 | const struct GNUNET_PeerIdentity *path, | ||
1290 | const struct GNUNET_HashCode * key) | ||
1291 | { | ||
1292 | } | ||
1293 | |||
1294 | |||
1295 | static void | ||
1296 | get_resp_callback (void *cls, | ||
1297 | enum GNUNET_BLOCK_Type type, | ||
1298 | const struct GNUNET_PeerIdentity *get_path, | ||
1299 | unsigned int get_path_length, | ||
1300 | const struct GNUNET_PeerIdentity *put_path, | ||
1301 | unsigned int put_path_length, | ||
1302 | struct GNUNET_TIME_Absolute exp, | ||
1303 | const struct GNUNET_HashCode * key, | ||
1304 | const void *data, | ||
1305 | size_t size) | ||
1306 | { | ||
1307 | } | ||
1308 | |||
1309 | |||
1310 | static void | ||
1311 | put_callback (void *cls, | ||
1312 | enum GNUNET_DHT_RouteOption options, | ||
1313 | enum GNUNET_BLOCK_Type type, | ||
1314 | uint32_t hop_count, | ||
1315 | uint32_t desired_replication_level, | ||
1316 | unsigned int path_length, | ||
1317 | const struct GNUNET_PeerIdentity *path, | ||
1318 | struct GNUNET_TIME_Absolute exp, | ||
1319 | const struct GNUNET_HashCode * key, | ||
1320 | const void *data, | ||
1321 | size_t size) | ||
1322 | { | ||
1323 | } | ||
1324 | |||
1325 | |||
1326 | monitor_handle = GNUNET_DHT_monitor_start (dht_handle, | ||
1327 | block_type, | ||
1328 | key, | ||
1329 | &get_callback, | ||
1330 | &get_resp_callback, | ||
1331 | &put_callback, | ||
1332 | cls); | ||
1333 | @end example | 1102 | @end example |
1334 | 1103 | ||
1335 | |||
1336 | @section Debugging with gnunet-arm | 1104 | @section Debugging with gnunet-arm |
1337 | 1105 | ||
1338 | Even if services are managed by @command{gnunet-arm}, you can start them with | 1106 | Even if services are managed by @command{gnunet-arm}, you can start them with |
diff --git a/doc/tutorial-examples/007.c b/doc/tutorial-examples/007.c new file mode 100644 index 000000000..096539e43 --- /dev/null +++ b/doc/tutorial-examples/007.c | |||
@@ -0,0 +1,10 @@ | |||
1 | GNUNET_SERVICE_MAIN | ||
2 | ("service-name", | ||
3 | GNUNET_SERVICE_OPTION_NONE, | ||
4 | &run, | ||
5 | &client_connect_cb, | ||
6 | &client_disconnect_cb, | ||
7 | NULL, | ||
8 | GNUNET_MQ_hd_fixed_size (...), | ||
9 | GNUNET_MQ_hd_var_size (...), | ||
10 | GNUNET_MQ_handler_end ()); | ||
diff --git a/doc/tutorial-examples/008.c b/doc/tutorial-examples/008.c new file mode 100644 index 000000000..2dffe2cf9 --- /dev/null +++ b/doc/tutorial-examples/008.c | |||
@@ -0,0 +1,22 @@ | |||
1 | static void | ||
2 | run (void *cls, | ||
3 | const struct GNUNET_CONFIGURATION_Handle *c, | ||
4 | struct GNUNET_SERVICE_Handle *service) | ||
5 | { | ||
6 | } | ||
7 | |||
8 | static void * | ||
9 | client_connect_cb (void *cls, | ||
10 | struct GNUNET_SERVICE_Client *c, | ||
11 | struct GNUNET_MQ_Handle *mq) | ||
12 | { | ||
13 | return c; | ||
14 | } | ||
15 | |||
16 | static void | ||
17 | client_disconnect_cb (void *cls, | ||
18 | struct GNUNET_SERVICE_Client *c, | ||
19 | void *internal_cls) | ||
20 | { | ||
21 | GNUNET_assert (c == internal_cls); | ||
22 | } | ||
diff --git a/doc/tutorial-examples/009.c b/doc/tutorial-examples/009.c new file mode 100644 index 000000000..26d918fb0 --- /dev/null +++ b/doc/tutorial-examples/009.c | |||
@@ -0,0 +1,9 @@ | |||
1 | #include <gnunet/gnunet_core_service.h> | ||
2 | |||
3 | struct GNUNET_CORE_Handle * | ||
4 | GNUNET_CORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
5 | void *cls, | ||
6 | GNUNET_CORE_StartupCallback init, | ||
7 | GNUNET_CORE_ConnectEventHandler connects, | ||
8 | GNUNET_CORE_DisconnectEventHandler disconnects, | ||
9 | const struct GNUNET_MQ_MessageHandler *handlers); | ||
diff --git a/doc/tutorial-examples/010.c b/doc/tutorial-examples/010.c new file mode 100644 index 000000000..33494490d --- /dev/null +++ b/doc/tutorial-examples/010.c | |||
@@ -0,0 +1,8 @@ | |||
1 | void * | ||
2 | connects (void *cls, | ||
3 | const struct GNUNET_PeerIdentity *peer, | ||
4 | struct GNUNET_MQ_Handle *mq) | ||
5 | { | ||
6 | return mq; | ||
7 | } | ||
8 | |||
diff --git a/doc/tutorial-examples/011.c b/doc/tutorial-examples/011.c new file mode 100644 index 000000000..23bc051de --- /dev/null +++ b/doc/tutorial-examples/011.c | |||
@@ -0,0 +1,8 @@ | |||
1 | void | ||
2 | disconnects (void *cls, | ||
3 | const struct GNUNET_PeerIdentity * peer) | ||
4 | { | ||
5 | /* Remove peer's identity from known peers */ | ||
6 | /* Make sure no messages are sent to peer from now on */ | ||
7 | } | ||
8 | |||
diff --git a/doc/tutorial-examples/012.c b/doc/tutorial-examples/012.c new file mode 100644 index 000000000..cb21d78ab --- /dev/null +++ b/doc/tutorial-examples/012.c | |||
@@ -0,0 +1,4 @@ | |||
1 | #include "gnunet_peerstore_service.h" | ||
2 | |||
3 | peerstore_handle = GNUNET_PEERSTORE_connect (cfg); | ||
4 | |||
diff --git a/doc/tutorial-examples/013.c b/doc/tutorial-examples/013.c new file mode 100644 index 000000000..6792417e1 --- /dev/null +++ b/doc/tutorial-examples/013.c | |||
@@ -0,0 +1,12 @@ | |||
1 | struct GNUNET_PEERSTORE_StoreContext * | ||
2 | GNUNET_PEERSTORE_store (struct GNUNET_PEERSTORE_Handle *h, | ||
3 | const char *sub_system, | ||
4 | const struct GNUNET_PeerIdentity *peer, | ||
5 | const char *key, | ||
6 | const void *value, | ||
7 | size_t size, | ||
8 | struct GNUNET_TIME_Absolute expiry, | ||
9 | enum GNUNET_PEERSTORE_StoreOption options, | ||
10 | GNUNET_PEERSTORE_Continuation cont, | ||
11 | void *cont_cls); | ||
12 | |||
diff --git a/doc/tutorial-examples/014.c b/doc/tutorial-examples/014.c new file mode 100644 index 000000000..ce204f795 --- /dev/null +++ b/doc/tutorial-examples/014.c | |||
@@ -0,0 +1,9 @@ | |||
1 | struct GNUNET_PEERSTORE_IterateContext * | ||
2 | GNUNET_PEERSTORE_iterate (struct GNUNET_PEERSTORE_Handle *h, | ||
3 | const char *sub_system, | ||
4 | const struct GNUNET_PeerIdentity *peer, | ||
5 | const char *key, | ||
6 | struct GNUNET_TIME_Relative timeout, | ||
7 | GNUNET_PEERSTORE_Processor callback, | ||
8 | void *callback_cls); | ||
9 | |||
diff --git a/doc/tutorial-examples/015.c b/doc/tutorial-examples/015.c new file mode 100644 index 000000000..0dd267e8e --- /dev/null +++ b/doc/tutorial-examples/015.c | |||
@@ -0,0 +1,8 @@ | |||
1 | struct GNUNET_PEERSTORE_WatchContext * | ||
2 | GNUNET_PEERSTORE_watch (struct GNUNET_PEERSTORE_Handle *h, | ||
3 | const char *sub_system, | ||
4 | const struct GNUNET_PeerIdentity *peer, | ||
5 | const char *key, | ||
6 | GNUNET_PEERSTORE_Processor callback, | ||
7 | void *callback_cls); | ||
8 | |||
diff --git a/doc/tutorial-examples/016.c b/doc/tutorial-examples/016.c new file mode 100644 index 000000000..d8db4b3b8 --- /dev/null +++ b/doc/tutorial-examples/016.c | |||
@@ -0,0 +1,3 @@ | |||
1 | void | ||
2 | GNUNET_PEERSTORE_watch_cancel (struct GNUNET_PEERSTORE_WatchContext *wc); | ||
3 | |||
diff --git a/doc/tutorial-examples/017.c b/doc/tutorial-examples/017.c new file mode 100644 index 000000000..c4acbc088 --- /dev/null +++ b/doc/tutorial-examples/017.c | |||
@@ -0,0 +1,3 @@ | |||
1 | void | ||
2 | GNUNET_PEERSTORE_disconnect (struct GNUNET_PEERSTORE_Handle *h, int sync_first); | ||
3 | |||
diff --git a/doc/tutorial-examples/018.c b/doc/tutorial-examples/018.c new file mode 100644 index 000000000..3fc22584c --- /dev/null +++ b/doc/tutorial-examples/018.c | |||
@@ -0,0 +1,2 @@ | |||
1 | dht_handle = GNUNET_DHT_connect (cfg, parallel_requests); | ||
2 | |||
diff --git a/doc/tutorial-examples/019.c b/doc/tutorial-examples/019.c new file mode 100644 index 000000000..d016d381b --- /dev/null +++ b/doc/tutorial-examples/019.c | |||
@@ -0,0 +1,15 @@ | |||
1 | message_sent_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
2 | { | ||
3 | // Request has left local node | ||
4 | } | ||
5 | |||
6 | struct GNUNET_DHT_PutHandle * | ||
7 | GNUNET_DHT_put (struct GNUNET_DHT_Handle *handle, | ||
8 | const struct GNUNET_HashCode *key, | ||
9 | uint32_t desired_replication_level, | ||
10 | enum GNUNET_DHT_RouteOption options, | ||
11 | enum GNUNET_BLOCK_Type type, size_t size, const void *data, | ||
12 | struct GNUNET_TIME_Absolute exp, | ||
13 | struct GNUNET_TIME_Relative timeout, | ||
14 | GNUNET_DHT_PutContinuation cont, void *cont_cls) | ||
15 | |||
diff --git a/doc/tutorial-examples/020.c b/doc/tutorial-examples/020.c new file mode 100644 index 000000000..5ecba1c16 --- /dev/null +++ b/doc/tutorial-examples/020.c | |||
@@ -0,0 +1,24 @@ | |||
1 | static void | ||
2 | get_result_iterator (void *cls, struct GNUNET_TIME_Absolute expiration, | ||
3 | const struct GNUNET_HashCode *key, | ||
4 | const struct GNUNET_PeerIdentity *get_path, | ||
5 | unsigned int get_path_length, | ||
6 | const struct GNUNET_PeerIdentity *put_path, | ||
7 | unsigned int put_path_length, | ||
8 | enum GNUNET_BLOCK_Type type, size_t size, const void *data) | ||
9 | { | ||
10 | // Optionally: | ||
11 | GNUNET_DHT_get_stop (get_handle); | ||
12 | } | ||
13 | |||
14 | get_handle = | ||
15 | GNUNET_DHT_get_start (dht_handle, | ||
16 | block_type, | ||
17 | &key, | ||
18 | replication, | ||
19 | GNUNET_DHT_RO_NONE, | ||
20 | NULL, | ||
21 | 0, | ||
22 | &get_result_iterator, | ||
23 | cls) | ||
24 | |||
diff --git a/doc/tutorial-examples/021.c b/doc/tutorial-examples/021.c new file mode 100644 index 000000000..688a31fe0 --- /dev/null +++ b/doc/tutorial-examples/021.c | |||
@@ -0,0 +1,13 @@ | |||
1 | static enum GNUNET_BLOCK_EvaluationResult | ||
2 | block_plugin_SERVICE_evaluate (void *cls, | ||
3 | enum GNUNET_BLOCK_Type type, | ||
4 | struct GNUNET_BlockGroup *bg, | ||
5 | const GNUNET_HashCode *query, | ||
6 | const void *xquery, | ||
7 | size_t xquery_size, | ||
8 | const void *reply_block, | ||
9 | size_t reply_block_size) | ||
10 | { | ||
11 | // Verify type, block and bg | ||
12 | } | ||
13 | |||
diff --git a/doc/tutorial-examples/022.c b/doc/tutorial-examples/022.c new file mode 100644 index 000000000..a373619bd --- /dev/null +++ b/doc/tutorial-examples/022.c | |||
@@ -0,0 +1,8 @@ | |||
1 | static int | ||
2 | block_plugin_SERVICE_get_key (void *cls, enum GNUNET_BLOCK_Type type, | ||
3 | const void *block, size_t block_size, | ||
4 | struct GNUNET_HashCode *key) | ||
5 | { | ||
6 | // Store the key in the key argument, return GNUNET_OK on success. | ||
7 | } | ||
8 | |||
diff --git a/doc/tutorial-examples/023.c b/doc/tutorial-examples/023.c new file mode 100644 index 000000000..820c38b10 --- /dev/null +++ b/doc/tutorial-examples/023.c | |||
@@ -0,0 +1,17 @@ | |||
1 | void * | ||
2 | libgnunet_plugin_block_SERVICE_init (void *cls) | ||
3 | { | ||
4 | static enum GNUNET_BLOCK_Type types[] = | ||
5 | { | ||
6 | GNUNET_BLOCK_TYPE_SERVICE_BLOCKYPE, | ||
7 | GNUNET_BLOCK_TYPE_ANY | ||
8 | }; | ||
9 | struct GNUNET_BLOCK_PluginFunctions *api; | ||
10 | |||
11 | api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions); | ||
12 | api->evaluate = &block_plugin_SERICE_evaluate; | ||
13 | api->get_key = &block_plugin_SERVICE_get_key; | ||
14 | api->types = types; | ||
15 | return api; | ||
16 | } | ||
17 | |||
diff --git a/doc/tutorial-examples/024.c b/doc/tutorial-examples/024.c new file mode 100644 index 000000000..2e84b5905 --- /dev/null +++ b/doc/tutorial-examples/024.c | |||
@@ -0,0 +1,9 @@ | |||
1 | void * | ||
2 | libgnunet_plugin_block_SERVICE_done (void *cls) | ||
3 | { | ||
4 | struct GNUNET_TRANSPORT_PluginFunctions *api = cls; | ||
5 | |||
6 | GNUNET_free (api); | ||
7 | return NULL; | ||
8 | } | ||
9 | |||
diff --git a/doc/tutorial-examples/025.c b/doc/tutorial-examples/025.c new file mode 100644 index 000000000..66d4f80ec --- /dev/null +++ b/doc/tutorial-examples/025.c | |||
@@ -0,0 +1,15 @@ | |||
1 | plugindir = $(libdir)/gnunet | ||
2 | |||
3 | plugin_LTLIBRARIES = \ | ||
4 | libgnunet_plugin_block_ext.la | ||
5 | libgnunet_plugin_block_ext_la_SOURCES = \ | ||
6 | plugin_block_ext.c | ||
7 | libgnunet_plugin_block_ext_la_LIBADD = \ | ||
8 | $(prefix)/lib/libgnunethello.la \ | ||
9 | $(prefix)/lib/libgnunetblock.la \ | ||
10 | $(prefix)/lib/libgnunetutil.la | ||
11 | libgnunet_plugin_block_ext_la_LDFLAGS = \ | ||
12 | $(GN_PLUGIN_LDFLAGS) | ||
13 | libgnunet_plugin_block_ext_la_DEPENDENCIES = \ | ||
14 | $(prefix)/lib/libgnunetblock.la | ||
15 | |||
diff --git a/doc/tutorial-examples/026.c b/doc/tutorial-examples/026.c new file mode 100644 index 000000000..264e0b6b9 --- /dev/null +++ b/doc/tutorial-examples/026.c | |||
@@ -0,0 +1,52 @@ | |||
1 | static void | ||
2 | get_callback (void *cls, | ||
3 | enum GNUNET_DHT_RouteOption options, | ||
4 | enum GNUNET_BLOCK_Type type, | ||
5 | uint32_t hop_count, | ||
6 | uint32_t desired_replication_level, | ||
7 | unsigned int path_length, | ||
8 | const struct GNUNET_PeerIdentity *path, | ||
9 | const struct GNUNET_HashCode * key) | ||
10 | { | ||
11 | } | ||
12 | |||
13 | |||
14 | static void | ||
15 | get_resp_callback (void *cls, | ||
16 | enum GNUNET_BLOCK_Type type, | ||
17 | const struct GNUNET_PeerIdentity *get_path, | ||
18 | unsigned int get_path_length, | ||
19 | const struct GNUNET_PeerIdentity *put_path, | ||
20 | unsigned int put_path_length, | ||
21 | struct GNUNET_TIME_Absolute exp, | ||
22 | const struct GNUNET_HashCode * key, | ||
23 | const void *data, | ||
24 | size_t size) | ||
25 | { | ||
26 | } | ||
27 | |||
28 | |||
29 | static void | ||
30 | put_callback (void *cls, | ||
31 | enum GNUNET_DHT_RouteOption options, | ||
32 | enum GNUNET_BLOCK_Type type, | ||
33 | uint32_t hop_count, | ||
34 | uint32_t desired_replication_level, | ||
35 | unsigned int path_length, | ||
36 | const struct GNUNET_PeerIdentity *path, | ||
37 | struct GNUNET_TIME_Absolute exp, | ||
38 | const struct GNUNET_HashCode * key, | ||
39 | const void *data, | ||
40 | size_t size) | ||
41 | { | ||
42 | } | ||
43 | |||
44 | |||
45 | monitor_handle = GNUNET_DHT_monitor_start (dht_handle, | ||
46 | block_type, | ||
47 | key, | ||
48 | &get_callback, | ||
49 | &get_resp_callback, | ||
50 | &put_callback, | ||
51 | cls); | ||
52 | |||