libroken: Build on windows
[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 <afsconfig.h>
15 #include <afs/param.h>
16 #include <afs/stds.h>
17 #include <roken.h>
18 }
19
20 #include <WINNT/afsclass.h>
21 #include "internal.h"
22
23
24 /*
25  * DEFINITIONS ________________________________________________________________
26  *
27  */
28
29 #define cREALLOC_NOTIFY  4      // allocate space for 4 notifies at once
30
31
32 /*
33  * VARIABLES __________________________________________________________________
34  *
35  */
36
37 size_t            NOTIFYCALLBACK::nNotifyList = 0;
38 LPNOTIFYCALLBACK *NOTIFYCALLBACK::aNotifyList = NULL;
39
40
41 /*
42  * PROTOTYPES _________________________________________________________________
43  *
44  */
45
46
47 /*
48  * ROUTINES ___________________________________________________________________
49  *
50  */
51
52 NOTIFYCALLBACK::NOTIFYCALLBACK (NOTIFYCALLBACKPROC procUser, LPARAM lpUser)
53 {
54    procSupplied = procUser;
55    lpSupplied = lpUser;
56    size_t iNotify;
57
58    for (iNotify = 0; iNotify < nNotifyList; ++iNotify)
59       {
60       if (aNotifyList[ iNotify ] == NULL)
61          break;
62       }
63
64    if (iNotify >= nNotifyList)
65       {
66       REALLOC (aNotifyList, nNotifyList, 1+iNotify, cREALLOC_NOTIFY );
67       }
68
69    if (iNotify < nNotifyList)
70       {
71       aNotifyList[ iNotify ] = this;
72       }
73 }
74
75
76 NOTIFYCALLBACK::~NOTIFYCALLBACK (void)
77 {
78    for (size_t iNotify = 0; iNotify < nNotifyList; ++iNotify)
79       {
80       if (aNotifyList[ iNotify ] == this)
81          aNotifyList[ iNotify ] = NULL;
82       }
83 }
84
85
86 BOOL NOTIFYCALLBACK::SendNotificationToAll (NOTIFYEVENT evt, ULONG status)
87 {
88    return SendNotificationToAll (evt, NULL, NULL, NULL, NULL, 0, status);
89 }
90
91 BOOL NOTIFYCALLBACK::SendNotificationToAll (NOTIFYEVENT evt, LPIDENT lpi1, ULONG status)
92 {
93    return SendNotificationToAll (evt, lpi1, NULL, NULL, NULL, 0, status);
94 }
95
96 BOOL NOTIFYCALLBACK::SendNotificationToAll (NOTIFYEVENT evt, LPTSTR psz1, ULONG status)
97 {
98    return SendNotificationToAll (evt, NULL, NULL, psz1, NULL, 0, status);
99 }
100
101 BOOL NOTIFYCALLBACK::SendNotificationToAll (NOTIFYEVENT evt, LPIDENT lpi1, LPTSTR psz1, ULONG status)
102 {
103    return SendNotificationToAll (evt, lpi1, NULL, psz1, NULL, 0, status);
104 }
105
106 BOOL NOTIFYCALLBACK::SendNotificationToAll (NOTIFYEVENT evt, LPIDENT lpi1, LPIDENT lpi2, LPTSTR psz1, LPTSTR psz2, DWORD dw1, ULONG status)
107 {
108    BOOL rc = TRUE;
109
110    NOTIFYPARAMS Params;
111    memset (&Params, 0x00, sizeof(Params));
112    Params.lpi1 = lpi1;
113    Params.lpi2 = lpi2;
114    lstrcpy (Params.sz1, (psz1) ? psz1 : TEXT(""));
115    lstrcpy (Params.sz2, (psz2) ? psz2 : TEXT(""));
116    Params.dw1 = dw1;
117    Params.status = status;
118
119    for (size_t iNotify = 0; iNotify < nNotifyList; ++iNotify)
120       {
121       if (aNotifyList[ iNotify ] != NULL)
122          {
123          Params.lpUser = aNotifyList[ iNotify ]->lpSupplied;
124          if (!aNotifyList[ iNotify ]->SendNotification (evt, &Params))
125             rc = FALSE;
126          }
127       }
128
129    return rc;
130 }
131
132
133 BOOL NOTIFYCALLBACK::SendNotification (NOTIFYEVENT evt, PNOTIFYPARAMS pParams)
134 {
135    BOOL rc = TRUE;
136
137    if (procSupplied != NULL) {
138       try {
139          if (!(*procSupplied)( evt, pParams ))
140             rc = FALSE;
141       } catch(...) {
142          // whoops--never trust a callback.
143 #ifdef DEBUG
144          DebugBreak();
145 #endif
146          rc = FALSE;
147       }
148    }
149
150    return rc;
151 }
152