summaryrefslogtreecommitdiff
path: root/src/messenger/gnunet-service-messenger_operation_store.c
blob: 05985ef8463964099f9853c08d7ec014b64ee9c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
   This file is part of GNUnet.
   Copyright (C) 2021 GNUnet e.V.

   GNUnet is free software: you can redistribute it and/or modify it
   under the terms of the GNU Affero General Public License as published
   by the Free Software Foundation, either version 3 of the License,
   or (at your option) any later version.

   GNUnet is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

   SPDX-License-Identifier: AGPL3.0-or-later
 */
/**
 * @author Tobias Frisch
 * @file src/messenger/gnunet-service-messenger_operation_store.c
 * @brief GNUnet MESSENGER service
 */

#include "gnunet-service-messenger_operation_store.h"

#include "gnunet-service-messenger_operation.h"
#include "gnunet-service-messenger_room.h"

void
init_operation_store (struct GNUNET_MESSENGER_OperationStore *store, struct GNUNET_MESSENGER_SrvRoom *room)
{
  GNUNET_assert((store) && (room));

  store->room = room;
  store->operations = GNUNET_CONTAINER_multihashmap_create(8, GNUNET_NO);
}

static int
iterate_destroy_operations (void *cls, const struct GNUNET_HashCode *key, void *value)
{
  struct GNUNET_MESSENGER_Operation *op = value;

  destroy_operation(op);

  return GNUNET_YES;
}

void
clear_operation_store (struct GNUNET_MESSENGER_OperationStore *store)
{
  GNUNET_assert(store);

  GNUNET_CONTAINER_multihashmap_iterate (store->operations, iterate_destroy_operations, NULL);
  GNUNET_CONTAINER_multihashmap_destroy(store->operations);
}

static int
callback_scan_for_operations (void *cls, const char *filename)
{
  struct GNUNET_MESSENGER_OperationStore *store = cls;

  if (GNUNET_YES == GNUNET_DISK_file_test (filename))
  {
    char *path;

    GNUNET_asprintf (&path, "%s%c", filename, DIR_SEPARATOR);

    struct GNUNET_MESSENGER_Operation *op = load_operation(store, path);

    if ((op) && (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put(
        store->operations,
        &(op->hash), op,
        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)))
    {
      destroy_operation(op);
    }

    GNUNET_free(path);
  }

  return GNUNET_OK;
}

void
load_operation_store (struct GNUNET_MESSENGER_OperationStore *store,
                      const char *directory)
{
  GNUNET_assert ((store) && (directory));

  if (GNUNET_OK == GNUNET_DISK_directory_test (directory, GNUNET_YES))
    GNUNET_DISK_directory_scan (directory, callback_scan_for_operations, store);
}

static int
iterate_save_operations (void *cls, const struct GNUNET_HashCode *key, void *value)
{
  const char *save_dir = cls;

  struct GNUNET_MESSENGER_Operation *op = value;

  if (!op)
    return GNUNET_YES;

  char *op_dir;
  GNUNET_asprintf (&op_dir, "%s%s.cfg", save_dir, GNUNET_h2s(key));
  save_operation(op, op_dir);

  GNUNET_free(op_dir);
  return GNUNET_YES;
}

void
save_operation_store (const struct GNUNET_MESSENGER_OperationStore *store,
                      const char *directory)
{
  GNUNET_assert ((store) && (directory));

  char* save_dir;
  GNUNET_asprintf (&save_dir, "%s%s%c", directory, "operations", DIR_SEPARATOR);

  if ((GNUNET_YES == GNUNET_DISK_directory_test (save_dir, GNUNET_NO)) ||
      (GNUNET_OK == GNUNET_DISK_directory_create (save_dir)))
    GNUNET_CONTAINER_multihashmap_iterate (store->operations, iterate_save_operations, save_dir);

  GNUNET_free(save_dir);
}

enum GNUNET_MESSENGER_OperationType
get_store_operation_type (const struct GNUNET_MESSENGER_OperationStore *store,
                          const struct GNUNET_HashCode *hash)
{
  GNUNET_assert((store) && (hash));

  struct GNUNET_MESSENGER_Operation *op = GNUNET_CONTAINER_multihashmap_get(store->operations, hash);

  if (!op)
    return GNUNET_MESSENGER_OP_UNKNOWN;

  return op->type;
}

int
use_store_operation (struct GNUNET_MESSENGER_OperationStore *store,
                     const struct GNUNET_HashCode *hash,
                     enum GNUNET_MESSENGER_OperationType type,
                     struct GNUNET_TIME_Relative delay)
{
  GNUNET_assert((store) && (hash));

  struct GNUNET_MESSENGER_Operation *op = GNUNET_CONTAINER_multihashmap_get(store->operations, hash);

  if (op)
    goto use_op;

  op = create_operation(hash);

  if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put(store->operations, hash, op, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
  {
    destroy_operation(op);

    return GNUNET_SYSERR;
  }

use_op:
  if ((op->type != GNUNET_MESSENGER_OP_UNKNOWN) &&
      (type == GNUNET_MESSENGER_OP_DELETE))
    stop_operation (op);

  return start_operation(op, type, store, delay);
}

void
cancel_store_operation (struct GNUNET_MESSENGER_OperationStore *store,
                        const struct GNUNET_HashCode *hash)
{
  GNUNET_assert((store) && (hash));

  struct GNUNET_MESSENGER_Operation *op = GNUNET_CONTAINER_multihashmap_get(store->operations, hash);

  if (!op)
    return;

  stop_operation(op);

  GNUNET_CONTAINER_multihashmap_remove(store->operations, hash, op);

  destroy_operation(op);
}

extern void
callback_room_deletion (struct GNUNET_MESSENGER_SrvRoom *room, const struct GNUNET_HashCode *hash);

extern void
callback_room_merge (struct GNUNET_MESSENGER_SrvRoom *room, const struct GNUNET_HashCode *hash);

void
callback_store_operation (struct GNUNET_MESSENGER_OperationStore *store,
                          enum GNUNET_MESSENGER_OperationType type,
                          const struct GNUNET_HashCode *hash)
{
  GNUNET_assert((store) && (hash));

  struct GNUNET_HashCode op_hash;
  GNUNET_memcpy(&op_hash, hash, sizeof(op_hash));
  cancel_store_operation (store, &op_hash);

  struct GNUNET_MESSENGER_SrvRoom *room = store->room;

  switch (type)
  {
  case GNUNET_MESSENGER_OP_REQUEST:
    break;
  case GNUNET_MESSENGER_OP_DELETE:
    callback_room_deletion (room, &op_hash);
    break;
  case GNUNET_MESSENGER_OP_MERGE:
    callback_room_merge (room, &op_hash);
    break;
  default:
    break;
  }
}