File: db.c

package info (click to toggle)
ion 3.2.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 23,700 kB
  • ctags: 10,983
  • sloc: ansic: 141,798; sh: 22,848; makefile: 7,818; python: 1,638; sql: 311; perl: 197; awk: 178; xml: 50; java: 19
file content (161 lines) | stat: -rw-r--r-- 4,200 bytes parent folder | download | duplicates (2)
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
/******************************************************************************
 **                           COPYRIGHT NOTICE
 **      (c) 2012 The Johns Hopkins University Applied Physics Laboratory
 **                         All rights reserved.
 **
 **     This material may only be used, modified, or reproduced by or for the
 **       U.S. Government pursuant to the license rights granted under
 **          FAR clause 52.227-14 or DFARS clauses 252.227-7013/7014
 **
 **     For any other permissions, please contact the Legal Office at JHU/APL.
 ******************************************************************************/

/*****************************************************************************
 **
 ** File Name: db.c
 **
 ** Description: This file contains the definitions, prototypes, constants, and
 **              other information necessary for DTNMP actors to interact with
 **              SDRs to persistently store information.
 **
 ** Notes:
 **
 ** Assumptions:
 **
 **
 ** Modification History:
 **  MM/DD/YY  AUTHOR         DESCRIPTION
 **  --------  ------------   ---------------------------------------------
 **  06/29/13  E. Birrane Initial Implementation
 *****************************************************************************/

#include "db.h"


int  db_forget(Object *primitiveObj, Object *descObj, Object list)
{
	Sdr sdr = getIonsdr();
	Object elt;

	if((primitiveObj == NULL) || (descObj == NULL) || (list == 0))
	{
		DTNMP_DEBUG_ERR("db_forget","Bad Params.",NULL);
		return -1;
	}

	sdr_begin_xn(sdr);

	if(*primitiveObj != 0)
	{
	  sdr_free(sdr, *primitiveObj);
	}

	if(*descObj != 0)
	{
	   elt = sdr_list_first(sdr, list);
	   while(elt)
	   {
		   if(sdr_list_data(sdr, elt) == *descObj)
		   {
			   sdr_list_delete(sdr, elt, NULL, NULL);
			   sdr_free(sdr, *descObj);
			   elt = 0;
		   }
		   else
		   {
			   elt = sdr_list_next(sdr, elt);
		   }
	   }
	}

	sdr_end_xn(sdr);

	/* Forget now invalid SDR pointers. */
	*descObj = 0;
	*primitiveObj = 0;

	return 1;
}


/*
 * This function writes an item and its associated descriptor into the SDR,
 * allocating space for each, and adding the SDR descriptor pointer to a
 * given SDR list.
 *
 * item    : The serialized item to store in the SDR.
 * item_len: The size of the serialized item.
 * *itemObj: The SDR pointer to the serialized item in the SDR.
 * desc    : The item descriptor being written to the SDR.
 * desc_len: The size of the item descriptor.
 * *descObj: The SDR pointer to the item's descriptor object in the SDR.
 * list    : The SDR list holding the item descriptor (at *descrObj).
 */
int  db_persist(uint8_t  *item,
					  uint32_t  item_len,
					  Object   *itemObj,
					  void     *desc,
					  uint32_t  desc_len,
					  Object   *descObj,
					  Object    list)
{

   Sdr sdr = getIonsdr();

   sdr_begin_xn(sdr);


   /* Step 1: Allocate a descriptor object for this item in the SDR. */
   if((*descObj = sdr_malloc(sdr, desc_len)) == 0)
   {
	   sdr_cancel_xn(sdr);

	   DTNMP_DEBUG_ERR("db_persist",
			   	       "Can't allocate descriptor of size %d.",
			   	       desc_len);
	   return -1;
   }


   /* Step 2: Allocate space for the serialized rule in the SDR. */
   if((*itemObj = sdr_malloc(sdr, item_len)) == 0)
   {
	   sdr_free(sdr, *descObj);

	   sdr_cancel_xn(sdr);
	   *descObj = 0;
	   DTNMP_DEBUG_ERR("db_persist",
			   	   	   "Unable to allocate Item in SDR. Size %d.",
			           item_len);
	   return -1;
   }

   /* Step 3: Write the item to the SDR. */
   sdr_write(sdr, *itemObj, (char *) item, item_len);

   /* Step 4: Write the item descriptor to the SDR. */
   sdr_write(sdr, *descObj, (char *) desc, desc_len);

   /* Step 5: Save the descriptor in the AgentDB active rules list. */
   if (sdr_list_insert_last(sdr, list, *descObj) == 0)
   {
      sdr_free(sdr, *itemObj);
      sdr_free(sdr, *descObj);

      sdr_cancel_xn(sdr);

      *itemObj = 0;
      *descObj = 0;
      DTNMP_DEBUG_ERR("db_persist",
				        "Unable to insert item Descr. in SDR.", NULL);
      return -1;
   }

	if(sdr_end_xn(sdr))
	{
		DTNMP_DEBUG_ERR("db_persist", "Can't create Agent database.", NULL);
		return -1;
	}

   return 1;
}