-logging conventions
[oweals/gnunet.git] / src / lockmanager / gnunet-service-lockmanager.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 2, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file lockmanager/gnunet-service-lockmanager.c
23  * @brief implementation of the LOCKMANAGER service
24  * @author Sree Harsha Totakura
25  */
26
27 #include "platform.h"
28 #include "gnunet_common.h"
29 #include "gnunet_protocols.h"
30 #include "gnunet_service_lib.h"
31 #include "gnunet_server_lib.h"
32
33 #include "lockmanager.h"
34
35 #define VERBOSE GNUNET_YES
36
37 #define LOG(kind,...) \
38   GNUNET_log (kind, __VA_ARGS__)
39
40 #define TIME_REL_MINS(min) \
41   GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, min)
42
43 #define TIMEOUT TIME_REL_MINS(3)
44
45 /**
46  * Transmit notify for sending message to client
47  *
48  * @param cls the message to send
49  * @param size number of bytes available in buf
50  * @param buf where the callee should write the message
51  * @return number of bytes written to buf
52  */
53 static size_t 
54 transmit_notify (void *cls, size_t size, void *buf)
55 {
56   struct GNUNET_LOCKMANAGER_Message *msg = cls;
57   uint16_t msg_size;
58
59   if ((0 == size) || (NULL == buf))
60     {
61       /* FIXME: Timed out -- requeue? */
62       return 0;
63     }
64   msg_size = ntohs (msg->header.size);
65   GNUNET_assert (size >= msg_size);
66   memcpy (buf, msg, msg_size);
67   GNUNET_free (msg);
68   LOG (GNUNET_ERROR_TYPE_DEBUG,
69        "Message of size %u sent\n", msg_size);
70   return msg_size;
71 }
72
73
74 /**
75  * Handler for GNUNET_MESSAGE_TYPE_LOCKMANAGER_ACQUIRE
76  *
77  * @param cls NULL
78  * @param client the client sending this message
79  * @param message GNUNET_MESSAGE_TYPE_LOCKMANAGER_ACQUIRE message
80  */
81 static void
82 handle_acquire (void *cls,
83                 struct GNUNET_SERVER_Client *client,
84                 const struct GNUNET_MessageHeader *message)
85 {
86   const struct GNUNET_LOCKMANAGER_Message *request;
87   struct GNUNET_LOCKMANAGER_Message *reply;
88   int16_t request_size;
89   
90
91   LOG (GNUNET_ERROR_TYPE_DEBUG,
92        "Received an ACQUIRE message\n");
93   
94   request = (struct GNUNET_LOCKMANAGER_Message *) message;
95
96   /* FIXME: Dummy implementation; just echos success for every lock */
97   request_size = ntohs (message->size);
98   reply = GNUNET_malloc (request_size);
99   memcpy (reply, request, request_size);
100   reply->header.type = htons (GNUNET_MESSAGE_TYPE_LOCKMANAGER_SUCCESS);
101   GNUNET_SERVER_notify_transmit_ready (client,
102                                        request_size,
103                                        TIMEOUT,
104                                        &transmit_notify,
105                                        reply);
106
107   GNUNET_SERVER_receive_done (client, GNUNET_OK);
108 }
109
110
111 /**
112  * Handle for GNUNET_MESSAGE_TYPE_LOCKMANAGER_RELEASE
113  *
114  * @param 
115  * @return 
116  */
117 static void
118 handle_release (void *cls,
119                 struct GNUNET_SERVER_Client *client,
120                 const struct GNUNET_MessageHeader *message)
121 {
122   LOG (GNUNET_ERROR_TYPE_DEBUG,
123        "Received a RELEASE message\n");
124
125   GNUNET_SERVER_receive_done (client, GNUNET_OK);
126 }
127
128
129 /**
130  * Lock manager setup
131  *
132  * @param cls closure
133  * @param server the initialized server
134  * @param cfg configuration to use
135  */
136 static void 
137 lockmanager_run (void *cls,
138                  struct GNUNET_SERVER_Handle * server,
139                  const struct GNUNET_CONFIGURATION_Handle *cfg)
140 {
141   static const struct GNUNET_SERVER_MessageHandler message_handlers[] =
142     {
143       {&handle_acquire, NULL, GNUNET_MESSAGE_TYPE_LOCKMANAGER_ACQUIRE, 0},
144       {&handle_release, NULL, GNUNET_MESSAGE_TYPE_LOCKMANAGER_RELEASE, 0},
145       {NULL}
146     };
147   
148   LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting lockmanager\n");
149   GNUNET_SERVER_add_handlers (server,
150                               message_handlers);
151   
152 }
153
154 /**
155  * The starting point of execution
156  */
157 int main (int argc, char *const *argv)
158 {
159   int ret;
160   
161   GNUNET_log_setup ("lockmanager",
162 #if VERBOSE
163                     "DEBUG",
164 #else
165                     "WARNING",
166 #endif
167                     NULL);
168
169   LOG (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
170   ret = 
171     (GNUNET_OK ==
172      GNUNET_SERVICE_run (argc,
173                          argv,
174                          "lockmanager",
175                          GNUNET_SERVICE_OPTION_NONE,
176                          &lockmanager_run,
177                          NULL)) ? 0 : 1;
178   LOG (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
179   return ret;
180 }