more-rx-updates-20060504
[openafs.git] / src / WINNT / afsclass / c_notify.cpp
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <winsock2.h>
11 #include <ws2tcpip.h>
12
13 extern "C" {
14 #include <afs/param.h>
15 #include <afs/stds.h>
16 }
17
18 #include <WINNT/afsclass.h>
19 #include "internal.h"
20
21
22 /*
23  * DEFINITIONS ________________________________________________________________
24  *
25  */
26
27 #define cREALLOC_NOTIFY  4      // allocate space for 4 notifies at once
28
29
30 /*
31  * VARIABLES __________________________________________________________________
32  *
33  */
34
35 size_t            NOTIFYCALLBACK::nNotifyList = 0;
36 LPNOTIFYCALLBACK *NOTIFYCALLBACK::aNotifyList = NULL;
37
38
39 /*
40  * PROTOTYPES _________________________________________________________________
41  *
42  */
43
44
45 /*
46  * ROUTINES ___________________________________________________________________
47  *
48  */
49
50 NOTIFYCALLBACK::NOTIFYCALLBACK (NOTIFYCALLBACKPROC procUser, LPARAM lpUser)
51 {
52    procSupplied = procUser;
53    lpSupplied = lpUser;
54    size_t iNotify;
55
56    for (iNotify = 0; iNotify < nNotifyList; ++iNotify)
57       {
58       if (aNotifyList[ iNotify ] == NULL)
59          break;
60       }
61
62    if (iNotify >= nNotifyList)
63       {
64       REALLOC (aNotifyList, nNotifyList, 1+iNotify, cREALLOC_NOTIFY );
65       }
66
67    if (iNotify < nNotifyList)
68       {
69       aNotifyList[ iNotify ] = this;
70       }
71 }
72
73
74 NOTIFYCALLBACK::~NOTIFYCALLBACK (void)
75 {
76    for (size_t iNotify = 0; iNotify < nNotifyList; ++iNotify)
77       {
78       if (aNotifyList[ iNotify ] == this)
79          aNotifyList[ iNotify ] = NULL;
80       }
81 }
82
83
84 BOOL NOTIFYCALLBACK::SendNotificationToAll (NOTIFYEVENT evt, ULONG status)
85 {
86    return SendNotificationToAll (evt, NULL, NULL, NULL, NULL, 0, status);
87 }
88
89 BOOL NOTIFYCALLBACK::SendNotificationToAll (NOTIFYEVENT evt, LPIDENT lpi1, ULONG status)
90 {
91    return SendNotificationToAll (evt, lpi1, NULL, NULL, NULL, 0, status);
92 }
93
94 BOOL NOTIFYCALLBACK::SendNotificationToAll (NOTIFYEVENT evt, LPTSTR psz1, ULONG status)
95 {
96    return SendNotificationToAll (evt, NULL, NULL, psz1, NULL, 0, status);
97 }
98
99 BOOL NOTIFYCALLBACK::SendNotificationToAll (NOTIFYEVENT evt, LPIDENT lpi1, LPTSTR psz1, ULONG status)
100 {
101    return SendNotificationToAll (evt, lpi1, NULL, psz1, NULL, 0, status);
102 }
103
104 BOOL NOTIFYCALLBACK::SendNotificationToAll (NOTIFYEVENT evt, LPIDENT lpi1, LPIDENT lpi2, LPTSTR psz1, LPTSTR psz2, DWORD dw1, ULONG status)
105 {
106    BOOL rc = TRUE;
107
108    NOTIFYPARAMS Params;
109    memset (&Params, 0x00, sizeof(Params));
110    Params.lpi1 = lpi1;
111    Params.lpi2 = lpi2;
112    lstrcpy (Params.sz1, (psz1) ? psz1 : TEXT(""));
113    lstrcpy (Params.sz2, (psz2) ? psz2 : TEXT(""));
114    Params.dw1 = dw1;
115    Params.status = status;
116
117    for (size_t iNotify = 0; iNotify < nNotifyList; ++iNotify)
118       {
119       if (aNotifyList[ iNotify ] != NULL)
120          {
121          Params.lpUser = aNotifyList[ iNotify ]->lpSupplied;
122          if (!aNotifyList[ iNotify ]->SendNotification (evt, &Params))
123             rc = FALSE;
124          }
125       }
126
127    return rc;
128 }
129
130
131 BOOL NOTIFYCALLBACK::SendNotification (NOTIFYEVENT evt, PNOTIFYPARAMS pParams)
132 {
133    BOOL rc = TRUE;
134
135    if (procSupplied != NULL) {
136       try {
137          if (!(*procSupplied)( evt, pParams ))
138             rc = FALSE;
139       } catch(...) {
140          // whoops--never trust a callback.
141 #ifdef DEBUG
142          DebugBreak();
143 #endif
144          rc = FALSE;
145       }
146    }
147
148    return rc;
149 }
150