NTT: add a debug facility to dump all currently locked peers
[citadel.git] / citadel / nttlist.c
1 /*
2  * This module handles shared rooms, inter-Citadel mail, and outbound
3  * mailing list processing.
4  *
5  * Copyright (c) 2000-2012 by the citadel.org team
6  *
7  *  This program is open source software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License, version 3.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  */
16 #include "sysdep.h"
17 #include <stdio.h>
18 #include <libcitadel.h>
19 #include "ctdl_module.h"
20 #include "serv_extensions.h"
21
22 /*-----------------------------------------------------------------------------*
23  *                 Network maps: evaluate other nodes                          *
24  *-----------------------------------------------------------------------------*/
25 int NTTDebugEnabled = 0;
26 int NTTDumpEnabled = 0;
27
28 /*
29  * network_talking_to()  --  concurrency checker
30  */
31 static HashList *nttlist = NULL;
32 int CtdlNetworkTalkingTo(const char *nodename, long len, int operation)
33 {
34
35         int retval = 0;
36         HashPos *Pos = NULL;
37         void *vdata;
38
39         begin_critical_section(S_NTTLIST);
40
41         switch(operation) {
42
43                 case NTT_ADD:
44                         if (nttlist == NULL) 
45                                 nttlist = NewHash(1, NULL);
46                         Put(nttlist, nodename, len, NewStrBufPlain(nodename, len), HFreeStrBuf);
47                         if (NTTDebugEnabled) syslog(LOG_DEBUG, "nttlist: added <%s>\n", nodename);
48                         break;
49                 case NTT_REMOVE:
50                         if ((nttlist == NULL) ||
51                             (GetCount(nttlist) == 0))
52                                 break;
53                         Pos = GetNewHashPos(nttlist, 1);
54                         if (GetHashPosFromKey (nttlist, nodename, len, Pos))
55                                 DeleteEntryFromHash(nttlist, Pos);
56                         DeleteHashPos(&Pos);
57                         if (NTTDebugEnabled) syslog(LOG_DEBUG, "nttlist: removed <%s>\n", nodename);
58
59                         break;
60
61                 case NTT_CHECK:
62                         if ((nttlist == NULL) ||
63                             (GetCount(nttlist) == 0))
64                                 break;
65                         if (GetHash(nttlist, nodename, len, &vdata))
66                                 retval ++;
67                         if (NTTDebugEnabled) syslog(LOG_DEBUG, "nttlist: have [%d] <%s>\n", retval, nodename);
68                         break;
69         }
70
71         if (NTTDumpEnabled)
72         {
73                 HashPos *It;
74                 StrBuf *NTTDump;
75                 long len;
76                 const char *Key;
77                 void *v;
78                 NTTDump = NewStrBuf ();
79
80                 It = GetNewHashPos(nttlist, 0);
81                 while (GetNextHashPos(nttlist, It, &len, &Key, &v))
82                 {
83                         if (StrLength(NTTDump) > 0)
84                                 StrBufAppendBufPlain(NTTDump, HKEY("|"), 0);
85                         StrBufAppendBuf(NTTDump, (StrBuf*) v, 0);
86                 }
87                 DeleteHashPos(&It);
88
89                 syslog(LOG_DEBUG, "nttlist: Dump: [%d] <%s>\n", 
90                        GetCount(nttlist),
91                        ChrPtr(NTTDump));
92                 FreeStrBuf(&NTTDump);
93         }
94         end_critical_section(S_NTTLIST);
95         return(retval);
96 }
97
98 void cleanup_nttlist(void)
99 {
100         begin_critical_section(S_NTTLIST);
101         DeleteHash(&nttlist);
102         end_critical_section(S_NTTLIST);
103 }
104
105
106
107 /*
108  * Module entry point
109  */
110 void SetNTTDebugEnabled(const int n)
111 {
112         NTTDebugEnabled = n;
113 }
114
115 void SetNTTDumpEnabled(const int n)
116 {
117         NTTDumpEnabled = n;
118 }
119
120
121
122 /*
123  * Module entry point
124  */
125 CTDL_MODULE_INIT(nttlist)
126 {
127         if (!threading)
128         {
129                 CtdlRegisterDebugFlagHook(HKEY("networktalkingto"), SetNTTDebugEnabled, &NTTDebugEnabled);
130                 CtdlRegisterDebugFlagHook(HKEY("dumpnetworktalkingto"), SetNTTDumpEnabled, &NTTDumpEnabled);
131
132                 CtdlRegisterCleanupHook(cleanup_nttlist);
133         }
134         return "nttlist";
135 }