ce0930b0a7811b1e7ded6db301b5c7151715f360
[olsrd.git] / gui / win32 / Main / MyDialog3.cpp
1 /*
2  * The olsr.org Optimized Link-State Routing daemon (olsrd)
3  * Copyright (c) 2004, Thomas Lopatic (thomas@lopatic.de)
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without 
7  * modification, are permitted provided that the following conditions 
8  * are met:
9  *
10  * * Redistributions of source code must retain the above copyright 
11  *   notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above copyright 
13  *   notice, this list of conditions and the following disclaimer in 
14  *   the documentation and/or other materials provided with the 
15  *   distribution.
16  * * Neither the name of olsr.org, olsrd nor the names of its 
17  *   contributors may be used to endorse or promote products derived 
18  *   from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
24  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * Visit http://www.olsr.org for more information.
34  *
35  * If you find this software useful feel free to make a donation
36  * to the project. For more information see the website or contact
37  * the copyright holders.
38  *
39  */
40
41 #include "stdafx.h"
42 #include "Frontend.h"
43 #include "MyDialog3.h"
44
45 #include "MprEntry.h"
46 #include "MidEntry.h"
47 #include "HnaEntry.h"
48
49 #ifdef _DEBUG
50 #define new DEBUG_NEW
51 #undef THIS_FILE
52 static char THIS_FILE[] = __FILE__;
53 #endif /* _DEBUG */
54
55 MyDialog3::MyDialog3(CWnd* pParent)
56         : CDialog(MyDialog3::IDD, pParent)
57 {
58         LastUpdate = 0;
59         Info = NULL;
60
61         //{{AFX_DATA_INIT(MyDialog3)
62         //}}AFX_DATA_INIT
63 }
64
65 BOOL MyDialog3::Create(CWnd *Parent)
66 {
67         return CDialog::Create(MyDialog3::IDD, Parent);
68 }
69
70 void MyDialog3::DoDataExchange(CDataExchange* pDX)
71 {
72         CDialog::DoDataExchange(pDX);
73         //{{AFX_DATA_MAP(MyDialog3)
74         DDX_Control(pDX, IDC_LIST6, m_HnaList);
75         DDX_Control(pDX, IDC_LIST5, m_MidList);
76         DDX_Control(pDX, IDC_LIST4, m_MprList);
77         DDX_Control(pDX, IDC_LIST1, m_NodeList);
78         //}}AFX_DATA_MAP
79 }
80
81 BEGIN_MESSAGE_MAP(MyDialog3, CDialog)
82         //{{AFX_MSG_MAP(MyDialog3)
83         ON_BN_CLICKED(IDOK, OnOK)
84         ON_BN_CLICKED(IDCANCEL, OnCancel)
85         ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST1, OnItemchangedNodeList)
86         //}}AFX_MSG_MAP
87 END_MESSAGE_MAP()
88
89 void MyDialog3::OnOK()
90 {
91 }
92
93 void MyDialog3::OnCancel()
94 {
95 }
96
97 BOOL MyDialog3::OnInitDialog() 
98 {
99         CDialog::OnInitDialog();
100         
101         m_NodeList.InsertColumn(0, "Address", LVCFMT_LEFT, 110, 0);
102         m_NodeList.InsertColumn(1, "Timeout", LVCFMT_LEFT, 110, 1);
103         m_NodeList.InsertColumn(2, "MID", LVCFMT_LEFT, 68, 2);
104         m_NodeList.InsertColumn(3, "HNA", LVCFMT_LEFT, 67, 3);
105
106         m_MprList.InsertColumn(0, "MPR", LVCFMT_LEFT, 169, 0);
107
108         m_MidList.InsertColumn(0, "MID", LVCFMT_LEFT, 169, 0);
109
110         m_HnaList.InsertColumn(0, "HNA", LVCFMT_LEFT, 169, 0);
111
112         return TRUE;
113 }
114
115 void MyDialog3::ClearNodeInfo(void)
116 {
117         m_NodeList.DeleteAllItems();
118
119         m_MprList.DeleteAllItems();
120         m_MidList.DeleteAllItems();
121         m_HnaList.DeleteAllItems();
122
123         if (Info != NULL)
124         {
125                 delete[] Info;
126                 Info = NULL;
127         }
128 }
129
130 void MyDialog3::UpdateNodeInfo(CList<class NodeEntry, class NodeEntry &> &NodeList)
131 {
132         SYSTEMTIME SysTime;
133         FILETIME FileTime;
134         unsigned __int64 Now;
135         class NodeEntry Entry;
136         POSITION Pos;
137         CString AddrStr;
138         CString TimeoutStr;
139         int Idx, Num;
140         POSITION Pos2;
141         class MprEntry MprEntry;
142         class MidEntry MidEntry;
143         class HnaEntry HnaEntry;
144         CString CurrNode;
145
146         ::GetSystemTime(&SysTime);
147         ::SystemTimeToFileTime(&SysTime, &FileTime);
148
149         Now = *(unsigned __int64 *)&FileTime;
150
151         if (Now < LastUpdate + (__int64)3 * (__int64)10000000)
152                 return;
153
154         LastUpdate = Now;
155
156         int CurrItem = m_NodeList.GetNextItem(-1, LVNI_SELECTED);
157
158         if (CurrItem >= 0)
159                 CurrNode = m_NodeList.GetItemText(CurrItem, 0);
160
161         m_NodeList.DeleteAllItems();
162
163         if (Info != NULL)
164         {
165                 delete[] Info;
166                 Info = NULL;
167         }
168
169         Num = NodeList.GetCount();
170
171         if (Num == 0)
172                 return;
173
174         Info = new class NodeInfo[Num];
175
176         Pos = NodeList.GetHeadPosition();
177
178         Idx = 0;
179
180         while (Pos != NULL)
181         {
182                 Entry = NodeList.GetNext(Pos);
183
184                 Pos2 = Entry.MprList.GetHeadPosition();
185
186                 while (Pos2 != NULL)
187                 {
188                         MprEntry = Entry.MprList.GetNext(Pos2);
189
190                         AddrStr.Format("%d.%d.%d.%d",
191                                 ((unsigned char *)&MprEntry.Addr)[0], ((unsigned char *)&MprEntry.Addr)[1],
192                                 ((unsigned char *)&MprEntry.Addr)[2], ((unsigned char *)&MprEntry.Addr)[3]);
193
194                         Info[Idx].MprList.Add(AddrStr);
195                 }
196
197                 Pos2 = Entry.MidList.GetHeadPosition();
198
199                 while (Pos2 != NULL)
200                 {
201                         MidEntry = Entry.MidList.GetNext(Pos2);
202
203                         AddrStr.Format("%d.%d.%d.%d",
204                                 ((unsigned char *)&MidEntry.Addr)[0], ((unsigned char *)&MidEntry.Addr)[1],
205                                 ((unsigned char *)&MidEntry.Addr)[2], ((unsigned char *)&MidEntry.Addr)[3]);
206
207                         Info[Idx].MidList.Add(AddrStr);
208                 }
209
210                 Pos2 = Entry.HnaList.GetHeadPosition();
211
212                 while (Pos2 != NULL)
213                 {
214                         HnaEntry = Entry.HnaList.GetNext(Pos2);
215
216                         unsigned int Mask = 0x80000000;
217                         unsigned int Val = ::ntohl(HnaEntry.Mask);
218                         int Bits;
219
220                         for (Bits = 0; Bits < 32; Bits++)
221                         {
222                                 if ((Val & Mask) == 0)
223                                         break;
224
225                                 Mask >>= 1;
226                         }
227
228                         AddrStr.Format("%d.%d.%d.%d/%d",
229                                 ((unsigned char *)&HnaEntry.Addr)[0], ((unsigned char *)&HnaEntry.Addr)[1],
230                                 ((unsigned char *)&HnaEntry.Addr)[2], ((unsigned char *)&HnaEntry.Addr)[3],
231                                 Bits);
232
233                         Info[Idx].HnaList.Add(AddrStr);
234                 }
235
236                 AddrStr.Format("%d.%d.%d.%d",
237                         ((unsigned char *)&Entry.Addr)[0], ((unsigned char *)&Entry.Addr)[1],
238                         ((unsigned char *)&Entry.Addr)[2], ((unsigned char *)&Entry.Addr)[3]);
239
240                 m_NodeList.InsertItem(Idx, AddrStr);
241
242                 if (AddrStr == CurrNode)
243                         m_NodeList.SetItemState(Idx, LVIS_SELECTED, LVIS_SELECTED);
244
245                 SYSTEMTIME SysTime;
246                 FILETIME LocalFileTime;
247
248                 ::FileTimeToLocalFileTime((FILETIME *)&Entry.Timeout, &LocalFileTime);
249                 ::FileTimeToSystemTime(&LocalFileTime, &SysTime);
250
251                 TimeoutStr.Format("%02d:%02d:%02d", SysTime.wHour, SysTime.wMinute,
252                         SysTime.wSecond);
253
254                 m_NodeList.SetItemText(Idx, 1, TimeoutStr);
255
256                 if (Entry.MidList.IsEmpty())
257                         m_NodeList.SetItemText(Idx, 2, "no");
258
259                 else
260                         m_NodeList.SetItemText(Idx, 2, "yes");
261
262                 if (Entry.HnaList.IsEmpty())
263                         m_NodeList.SetItemText(Idx, 3, "no");
264
265                 else
266                         m_NodeList.SetItemText(Idx, 3, "yes");
267
268                 Idx++;
269         }
270 }
271
272 void MyDialog3::OnItemchangedNodeList(NMHDR* pNMHDR, LRESULT* pResult) 
273 {
274         int Item;
275         int Idx;
276         int Num;
277
278         pNMHDR = pNMHDR;
279
280         *pResult = 0;
281
282         Item = m_NodeList.GetNextItem(-1, LVNI_SELECTED);
283
284         if (Item < 0)
285                 return;
286
287         m_MprList.DeleteAllItems();
288
289         Num = Info[Item].MprList.GetSize();
290         
291         for (Idx = 0; Idx < Num; Idx++)
292                 m_MprList.InsertItem(Idx, Info[Item].MprList.GetAt(Idx));
293
294         m_MidList.DeleteAllItems();
295
296         Num = Info[Item].MidList.GetSize();
297         
298         for (Idx = 0; Idx < Num; Idx++)
299                 m_MidList.InsertItem(Idx, Info[Item].MidList.GetAt(Idx));
300
301         m_HnaList.DeleteAllItems();
302
303         Num = Info[Item].HnaList.GetSize();
304         
305         for (Idx = 0; Idx < Num; Idx++)
306                 m_HnaList.InsertItem(Idx, Info[Item].HnaList.GetAt(Idx));
307 }