-
Notifications
You must be signed in to change notification settings - Fork 0
/
InternetAccessNode.cs
167 lines (140 loc) · 5.13 KB
/
InternetAccessNode.cs
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
#region usings
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using VVVV.PluginInterfaces.V1;
using VVVV.PluginInterfaces.V2;
using VVVV.Utils.Streams;
using VVVV.Utils.VColor;
using VVVV.Utils.VMath;
using VVVV.Core.Logging;
using System.Net;
using System.Net.NetworkInformation;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Threading;
using NETWORKLIST;
#endregion usings
namespace VVVV.Nodes
{
#region PluginInfo
[PluginInfo(Name = "InternetAccess", Category = "Network", Version = "Value", Author = "jens", Help = "Checks if any internet connection is available", Tags = "")]
#endregion PluginInfo
public class InternetAccessNode : IPluginEvaluate, IPartImportsSatisfiedNotification, IDisposable, INetworkListManagerEvents
{
#region fields & pins
[Output("Internet Connection", IsToggle = true)]
public ISpread<bool> InternetConnections;
[Output("Available Networks")]
public ISpread<string> AvailableNetworks;
[Output("Last Time Networks Changed")]
public ISpread<string> CheckenOn;
[Import()]
public ILogger Logger;
#endregion fields & pins
private int m_cookie = 0;
private IConnectionPoint m_icp;
private INetworkListManager m_nlm;
private bool HasInternet = false;
private DateTime LastCheck = DateTime.MinValue;
#region pin management
public void OnImportsSatisfied()
{
AvailableNetworks.SliceCount = 0;
InternetConnections.SliceCount = 1;
CheckenOn.SliceCount = 1;
}
public InternetAccessNode()
: base()
{
m_nlm = new NetworkListManager();
Networks = NLM.GetNetworks(NLM_ENUM_NETWORK.NLM_ENUM_NETWORK_CONNECTED);
LastCheck = DateTime.Now;
HasInternet = NLM.IsConnectedToInternet;
AdviseforNetworklistManager();
}
IEnumNetworks Networks;
public INetworkListManager NLM
{
get
{
return m_nlm;
}
}
public void ConnectivityChanged(NLM_CONNECTIVITY newConnectivity)
{
try
{
//Console.WriteLine("");
//Console.WriteLine("---------------------------------------------------------------------------");
//Console.WriteLine("NetworkList just informed the connectivity change. The new connectivity is:");
if (newConnectivity == NLM_CONNECTIVITY.NLM_CONNECTIVITY_DISCONNECTED)
{
// Console.WriteLine("The machine is disconnected from Network");
HasInternet = false;
}
if (((int)newConnectivity & (int)NLM_CONNECTIVITY.NLM_CONNECTIVITY_IPV4_INTERNET) != 0)
{
//Console.WriteLine("The machine is connected to internet with IPv4 capability ");
HasInternet = true;
}
if (((int)newConnectivity & (int)NLM_CONNECTIVITY.NLM_CONNECTIVITY_IPV6_INTERNET) != 0)
{
//Console.WriteLine("The machine is connected to internet with IPv6 capability ");
HasInternet = true;
}
if ((((int)newConnectivity & (int)NLM_CONNECTIVITY.NLM_CONNECTIVITY_IPV4_INTERNET) == 0)
&& (((int)newConnectivity & (int)NLM_CONNECTIVITY.NLM_CONNECTIVITY_IPV6_INTERNET) == 0))
{
//Console.WriteLine("The machine is not connected to internet yet ");
HasInternet = false;
}
Networks = NLM.GetNetworks(NLM_ENUM_NETWORK.NLM_ENUM_NETWORK_CONNECTED);
LastCheck = DateTime.Now;
}
catch (Exception)
{
}
}
public void AdviseforNetworklistManager()
{
try
{
IConnectionPointContainer icpc = (IConnectionPointContainer)m_nlm;
Guid tempGuid = typeof(INetworkListManagerEvents).GUID;
icpc.FindConnectionPoint(ref tempGuid, out m_icp);
m_icp.Advise(this, out m_cookie);
}
catch (Exception)
{
}
}
public void UnAdviseforNetworklistManager()
{
try
{
m_icp.Unadvise(m_cookie);
}
catch (Exception)
{
}
}
#endregion
public void Dispose()
{
UnAdviseforNetworklistManager();
}
// Called when data for any output pin is requested.
public void Evaluate(int SpreadMax)
{
try
{
InternetConnections[0] = HasInternet;
CheckenOn[0] = LastCheck.ToString();
AvailableNetworks.AssignFrom(from INetwork net in Networks where net.IsConnectedToInternet select net.GetName());
}
catch (Exception e) { }
}
}
}