summaryrefslogtreecommitdiff
path: root/windows/WinUI/NetworkMonitor.cs
blob: 7743c5b9fa580e7c41143dc64baf10bc4a2cf346 (plain)
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace WinUI
{
    class NetworkMonitor
    {
        public delegate void NetworkListCallback(List<ZeroTierNetwork> networks);
        public delegate void StatusCallback(ZeroTierStatus status);

        private Thread runThread;
        private NetworkListCallback _nwCb;
        private StatusCallback _stCb;


        private List<ZeroTierNetwork> _knownNetworks = new List<ZeroTierNetwork>();

        private static NetworkMonitor instance;
        private static object syncRoot = new object();

        public static NetworkMonitor Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                        {
                            instance = new NetworkMonitor();
                        }
                    }
                }

                return instance;
            }
        }

        private NetworkMonitor()
        {
            runThread = new Thread(new ThreadStart(run));
            loadNetworks();

            runThread.Start();
        }

        ~NetworkMonitor()
        {
            runThread.Interrupt();
        }

        private void loadNetworks()
        {
            String dataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\ZeroTier\\One";
            String dataFile = Path.Combine(dataPath, "networks.dat");
            
            if (File.Exists(dataFile))
            {
                List<ZeroTierNetwork> netList;

                using (Stream stream = File.Open(dataFile, FileMode.Open))
                {
                    var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                    netList = (List<ZeroTierNetwork>)bformatter.Deserialize(stream);
                    stream.Close();
                }

                lock (_knownNetworks)
                {
                    _knownNetworks = netList;
                }
            }
        }

        private void writeNetworks()
        {
            String dataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\ZeroTier\\One";
            String dataFile = Path.Combine(dataPath, "networks.dat");

            if (!Directory.Exists(dataPath))
            {
                Directory.CreateDirectory(dataPath);
            }

            using (Stream stream = File.Open(dataFile, FileMode.OpenOrCreate))
            {
                lock (_knownNetworks)
                {
                    var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                    bformatter.Serialize(stream, _knownNetworks);
                    stream.Flush();
                    stream.Close();
                }
            }
        }

        private void apiNetworkCallback(List<ZeroTierNetwork> networks)
        {
            lock (_knownNetworks)
            {
                _knownNetworks = _knownNetworks.Union(networks, new NetworkEqualityComparer()).ToList();

                foreach (ZeroTierNetwork n in _knownNetworks)
                {
                    if (networks.Contains(n))
                    {
                        n.IsConnected = true;
                    }
                    else
                    {
                        n.IsConnected = false;
                    }
                }

                _nwCb(_knownNetworks);
            }

            writeNetworks();
        }

        private void apiStatusCallback(ZeroTierStatus status)
        {
            _stCb(status);
        }

        private void run()
        {
            try
            {
                while (runThread.IsAlive)
                {
                    APIHandler handler = APIHandler.Instance;

                    if (handler != null)
                    {
                        handler.GetNetworks(apiNetworkCallback);
                        handler.GetStatus(apiStatusCallback);
                    }

                    Thread.Sleep(2000);
                }
            }
            catch
            {
                Console.WriteLine("Monitor Thread Ended");
            }
        }

        public void SubscribeStatusUpdates(StatusCallback cb)
        {
            _stCb += cb;
        }

        public void UnsubscribeStatusUpdates(StatusCallback cb)
        {
            _stCb -= cb;
        }

        public void SubscribeNetworkUpdates(NetworkListCallback cb)
        {
            _nwCb += cb;
        }

        public void UnsubscribeNetworkUpdates(NetworkListCallback cb)
        {
            _nwCb -= cb;
        }

        public void RemoveNetwork(String networkID)
        {
            lock(_knownNetworks)
            {
                foreach (ZeroTierNetwork n in _knownNetworks)
                {
                    if (n.NetworkId.Equals(networkID))
                    {
                        _knownNetworks.Remove(n);
                        writeNetworks();
                        break;
                    }
                }
            }
        }
    }
}