File: NetworkTracker.cpp

package info (click to toggle)
boinc 5.4.11-4%2Betch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 21,440 kB
  • ctags: 16,986
  • sloc: cpp: 70,682; ansic: 45,747; php: 35,513; xml: 10,487; sh: 9,324; python: 4,291; makefile: 1,958; asm: 1,258; perl: 914; sql: 395; csh: 126; pascal: 124
file content (306 lines) | stat: -rwxr-xr-x 9,786 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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// Berkeley Open Infrastructure for Network Computing
// http://boinc.berkeley.edu
// Copyright (C) 2005 University of California
//
// This is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation;
// either version 2.1 of the License, or (at your option) any later version.
//
// This software 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 Lesser General Public License for more details.
//
// To view the GNU Lesser General Public License visit
// http://www.gnu.org/copyleft/lesser.html
// or write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


#include "stdafx.h"
#include "BOINCSENSSink.h"
#include "SENSSubscriptions.h"
#include "SENSNetworkSubscriptions.h"
#include "SENSOnNowSubscriptions.h"
#include "SENSLogonSubscriptions.h"


IEventSystem*                       gpIEventSystem = NULL;

SENS_SUBSCRIPTION_GROUP             gSubscriptionGroups[3];
unsigned int                        giSubscriptionGroupCount = 0;

std::vector<PNETWORK_CONNECTION>    gpNetworkConnections;


EXTERN_C __declspec(dllexport) BOOL BOINCIsNetworkAlive( LPDWORD lpdwFlags )
{
    unsigned int i = 0;
    BOOL bReturnValue = FALSE;
    BOOL bCachePopulated = FALSE;
    PNETWORK_CONNECTION pNetworkConnection = NULL;

    // Check cached connection state
    if (!gpNetworkConnections.empty()) {
        bCachePopulated = TRUE;
        for (i=0; i<gpNetworkConnections.size(); i++) {
            pNetworkConnection = gpNetworkConnections[i];
            if (pNetworkConnection->ulType & *lpdwFlags) {
                bReturnValue = TRUE;
            }
        }
    }

    // If we do not have any cached information, then fall back to other
    //   methods
    if (!bCachePopulated) {
        bReturnValue = IsNetworkAlive( lpdwFlags );
        if (!bReturnValue) {
            DWORD current_flags = NULL;
            DWORD desired_flags = NULL;

            if (NETWORK_ALIVE_LAN & *lpdwFlags)
                desired_flags |= INTERNET_CONNECTION_LAN;

            if (NETWORK_ALIVE_WAN & *lpdwFlags)
                desired_flags |= INTERNET_CONNECTION_MODEM;

            // TODO: Find out if AOL is registered as a LAN or WAN connection.
            //   Until then, assume both are okay.
            if (NETWORK_ALIVE_AOL & *lpdwFlags)
                desired_flags |= INTERNET_CONNECTION_LAN | INTERNET_CONNECTION_MODEM;

            BOOL retval = InternetGetConnectedState(&current_flags, 0);
            if (retval && (current_flags & desired_flags)) {
                bReturnValue = TRUE;
            } else {
                bReturnValue = FALSE;
            }
        }
    }

    return bReturnValue;
}


EXTERN_C __declspec(dllexport) BOOL BOINCIsNetworkAlwaysOnline()
{
    DWORD dwFlags = NETWORK_ALIVE_LAN;
    return BOINCIsNetworkAlive(&dwFlags);
}


BOOL NetworkTrackerStartup()
{
    USES_CONVERSION;
    unsigned int                i = 0, j = 0;
	HRESULT                     hr = 0;
    IBOINCSENSSink*             pIBOINCSENSSink = NULL;

    CComBSTR                    bstrPROGID_EventSubscription;
	IEventSubscription*         pIEventSubscription = NULL;

    PSENS_SUBSCRIPTION_GROUP    pSubscriptionGroup = NULL;
    PSENS_SUBSCRIPTION          pSubscription = NULL;

    CComBSTR                    bstrSubscriberCLSID;
	CComBSTR                    bstrSubscriptionID;
	CComBSTR                    bstrSubscriptionName;
	CComBSTR                    bstrMethodName;


    // Assign the correct program is value to be used by the event
    //   registration store.
	bstrPROGID_EventSubscription = PROGID_EventSubscription;


    // Clear the cache
    gpNetworkConnections.clear();


    // Try and create some important references to COM objects before
    //   doing anything else.
    //

    // IEventSystem
	hr = CoCreateInstance(
                            CLSID_CEventSystem,
							NULL,
							CLSCTX_SERVER,
							IID_IEventSystem,
							(LPVOID*)&gpIEventSystem);
	if (FAILED(hr))
    {
		ATLTRACE(TEXT("Error creating event system! (0x%x)"), hr);
		return FALSE;
    }
    
    // IBOINCSENSSink
    hr = CoCreateInstance(
							CLSID_CBOINCSENSSink,
							NULL,
							CLSCTX_SERVER,
							IID_IBOINCSENSSink,
							(LPVOID*)&pIBOINCSENSSink);
	if (FAILED(hr))
	{
		ATLTRACE(TEXT("Failed to create pIBOINCSENSSink (0x%x)"), hr);
		return FALSE;
	}


    // Prepare for registration by pre-populating the registration
    //   multidimensional array with the known good stuff.

    // Prepare the SENS Network registration group.
    gSubscriptionGroups[0].bstrPublisherID = SENSGUID_PUBLISHER;
    gSubscriptionGroups[0].bstrEventClassID = SENSGUID_EVENTCLASS_NETWORK;
    gSubscriptionGroups[0].bstrInterfaceID = IID_ISensNetwork;
    gSubscriptionGroups[0].uiSubscriptionCount = SENS_NETWORK_SUBSCRIPTIONS_COUNT;
    gSubscriptionGroups[0].pSubscriptions = (PSENS_SUBSCRIPTION)&gSENSNetworkSubscriptions;

    // Prepare the SENS OnNow registration group.
    gSubscriptionGroups[1].bstrPublisherID = SENSGUID_PUBLISHER;
    gSubscriptionGroups[1].bstrEventClassID = SENSGUID_EVENTCLASS_ONNOW;
    gSubscriptionGroups[1].bstrInterfaceID = IID_ISensOnNow;
    gSubscriptionGroups[1].uiSubscriptionCount = SENS_ONNOW_SUBSCRIPTIONS_COUNT;
    gSubscriptionGroups[1].pSubscriptions = (PSENS_SUBSCRIPTION)&gSENSOnNowSubscriptions;

    // Prepare the SENS Logon registration group.
    gSubscriptionGroups[2].bstrPublisherID = SENSGUID_PUBLISHER;
    gSubscriptionGroups[2].bstrEventClassID = SENSGUID_EVENTCLASS_LOGON;
    gSubscriptionGroups[2].bstrInterfaceID = IID_ISensLogon;
    gSubscriptionGroups[2].uiSubscriptionCount = SENS_LOGON_SUBSCRIPTIONS_COUNT;
    gSubscriptionGroups[2].pSubscriptions = (PSENS_SUBSCRIPTION)&gSENSLogonSubscriptions;


    // Start to register the events by walking through the registration groups
    //
    giSubscriptionGroupCount = 
        sizeof(gSubscriptionGroups) / sizeof(SENS_SUBSCRIPTION_GROUP);

    for (i = 0; i < giSubscriptionGroupCount; i++)
    {
        pSubscriptionGroup = &gSubscriptionGroups[i];
        for (j = 0; j < pSubscriptionGroup->uiSubscriptionCount; j++)
        {
            pSubscription = &pSubscriptionGroup->pSubscriptions[j];

		    // Get a new IEventSubscription object.
		    hr = CoCreateInstance(
								    CLSID_CEventSubscription,
								    NULL,
								    CLSCTX_SERVER,
								    IID_IEventSubscription,
								    (LPVOID*) &pIEventSubscription);

		    if (FAILED(hr))
		    {
			    ATLTRACE(TEXT("Error getting IEventSubscription object (0x%x)"), hr);
			    return FALSE;
		    }

		    bstrSubscriptionID = *pSubscription->pSubscriptionID;
		    hr = pIEventSubscription->put_SubscriptionID(bstrSubscriptionID);
		    if (FAILED(hr))
		    {
			    ATLTRACE(TEXT("pIEventSubscription->put_SubscriptionID (0x%x)"), hr);
			    return FALSE;
		    }

		    bstrSubscriptionName = pSubscription->strSubscriptionName;
		    hr = pIEventSubscription->put_SubscriptionName(bstrSubscriptionName);
		    if (FAILED(hr))
		    {
			    ATLTRACE(TEXT("pIEventSubscription->put_SubscriptionName (0x%x)"), hr);
			    return FALSE;
		    }

		    bstrMethodName = pSubscription->strMethodName;
		    hr = pIEventSubscription->put_MethodName(bstrMethodName);
		    if (FAILED(hr))
		    {
			    ATLTRACE(TEXT("pIEventSubscription->put_MethodName (0x%x)"), hr);
			    return FALSE;
		    }

		    hr = pIEventSubscription->put_EventClassID(pSubscriptionGroup->bstrEventClassID);
		    if (FAILED(hr))
		    {
			    ATLTRACE(TEXT("pIEventSubscription->put_EventClassID (0x%x)"), hr);
			    return FALSE;
		    }

		    hr = pIEventSubscription->put_SubscriberInterface(pIBOINCSENSSink);
		    if (FAILED(hr))
		    {
			    ATLTRACE(TEXT("pIEventSubscription->put_SubscriberInterface (0x%x)"), hr);
			    return FALSE;
		    }

		    // Initialize it.
		    hr = gpIEventSystem->Store(
                                         bstrPROGID_EventSubscription, 
                                         pIEventSubscription);
		    if (FAILED(hr))
		    {
			    ATLTRACE(TEXT("gpIEventSystem->Store (0x%x)"), hr);
			    return FALSE;
		    }

            pIEventSubscription->Release();
		    pIEventSubscription = NULL;
        }
    }

	if (pIEventSubscription != NULL)
		pIEventSubscription->Release();

	if (pIBOINCSENSSink != NULL)
		pIBOINCSENSSink->Release();

    return TRUE;
}


void NetworkTrackerShutdown()
{
    USES_CONVERSION;
    unsigned int                i = 0, j = 0;
	HRESULT                     hr = 0;
	int                         errorIndex = 0;
	CComBSTR                    bstrQuery;
    PSENS_SUBSCRIPTION_GROUP    pSubscriptionGroup = NULL;
    PSENS_SUBSCRIPTION          pSubscription = NULL;
    PNETWORK_CONNECTION         pNetworkConnection = NULL;

	for (i = 0; i < giSubscriptionGroupCount; i++)
	{
        pSubscriptionGroup = &gSubscriptionGroups[i];
        for (j = 0; j < pSubscriptionGroup->uiSubscriptionCount; j++)
        {
            pSubscription = &pSubscriptionGroup->pSubscriptions[j];

            bstrQuery.Empty();
		    bstrQuery = TEXT("SubscriptionID=");
		    bstrQuery += *pSubscription->pSubscriptionID;

		    hr = gpIEventSystem->Remove(
									    PROGID_EventSubscription,
									    bstrQuery,
									    &errorIndex);
		    if (FAILED(hr))
		    {
			    ATLTRACE(TEXT("gpIEventSystem->Remove (0x%x)"), hr);
		    }
        }
	}


    // Clear the cache
    for (i=0; i<gpNetworkConnections.size(); i++) {
        delete gpNetworkConnections[i];
    }
    gpNetworkConnections.clear();
}