I2P

Invisible Internet Project


root/src/kadNodesListView.cpp @ f795f01a025d61deff07e2f90673085b2effa456

Revision f795f01a025d61deff07e2f90673085b2effa456, 11.9 KB (checked in by mkvore-commit@…, 13 months ago)

compiles up to PrefsUnifiedDlg?.cpp

Line 
1#include "kadNodesListView.h"
2#include "kademlia/routing/Contact.h"
3
4#include "common/Format.h"
5#include "Logger.h"
6
7#include <wx/colour.h>
8#include <wx/thread.h>
9#include <algorithm>
10
11typedef enum {TYPE, CONTACT_KAD_VERSION, UDP_READABLE, TCP_READABLE, LAST_SEEN, KAD_ID_STRING, KAD_ID, DISTANCE, UDP_ID } columns_enum ;
12
13static columns_enum __sort_order = TYPE ;
14static bool __reverse = false ;
15//static wxMutex contacts_mutex ;
16
17struct Compare {
18        bool operator() ( const Kademlia::CContact & lhs, const Kademlia::CContact & rhs ) const {
19                /*AddDebugLogLineN(logKadPrefs,
20                                   CFormat ( wxT ( "sort --> Compare enter %x %x" ) ) % (int) &lhs % (int) &rhs  );
21                */
22                bool inf = true ;
23                try {
24                        switch ( __sort_order ) {
25                        case TYPE         :
26                                inf = __reverse ? ( rhs.GetType() < lhs.GetType() )
27                                      : ( lhs.GetType() < rhs.GetType() );
28                                break;
29                        case CONTACT_KAD_VERSION :
30                                inf = __reverse ? ( rhs.GetVersion() < lhs.GetVersion() )
31                                      : ( lhs.GetVersion() < rhs.GetVersion() );
32                                break;
33                        case UDP_READABLE :
34                                inf = __reverse ? ( rhs.GetUDPDest().humanReadable() < lhs.GetUDPDest().humanReadable() )
35                                      : ( lhs.GetUDPDest().humanReadable() < rhs.GetUDPDest().humanReadable() );
36                                break;
37                        case TCP_READABLE :
38                                inf = __reverse ? ( rhs.GetTCPDest().humanReadable() < lhs.GetTCPDest().humanReadable() )
39                                      : ( lhs.GetTCPDest().humanReadable() < rhs.GetTCPDest().humanReadable() );
40                                break;
41                        case LAST_SEEN    :
42                                inf = __reverse ? ( rhs.GetLastSeen() < lhs.GetLastSeen() )
43                                      : ( lhs.GetLastSeen() < rhs.GetLastSeen() );
44                                break;
45                        case KAD_ID_STRING:
46                        case KAD_ID       :
47                                inf = __reverse ? ( rhs.GetClientID() < lhs.GetClientID() )
48                                      : ( lhs.GetClientID() < rhs.GetClientID() );
49                                break;
50                        case DISTANCE     :
51                                inf = __reverse ? ( rhs.GetDistance() < lhs.GetDistance() )
52                                      : ( lhs.GetDistance() < rhs.GetDistance() );
53                                break;
54                        case UDP_ID       :
55                                inf = __reverse ? ( rhs.GetUDPDest().toString() < lhs.GetUDPDest().toString() ) :
56                                      ( lhs.GetUDPDest().toString() < rhs.GetUDPDest().toString() );
57                                break;
58                        }
59
60                        /*AddDebugLogLineN(logKadPrefs,
61                                           CFormat ( wxT ( "sort --> Compare exit" ) )  );
62                        */
63                } catch (...) {
64                        AddDebugLogLineC(logKadPrefs,
65                                           CFormat ( wxT ( "sort --> Compare EXCEPTION" ) )  );
66                }
67                return inf;
68        }
69};
70
71
72KadNodesListView::KadNodesListView ( wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style, const wxValidator& validator, const wxString& name )
73        :
74        wxListView ( parent, id, pos, size, wxLC_REPORT | wxLC_VIRTUAL, validator, name )
75{
76        wxListItem itemCol;
77        itemCol.SetText ( _ ( "node type" ) );
78        itemCol.SetAlign ( wxLIST_FORMAT_CENTRE );
79        InsertColumn ( 0, itemCol );
80
81        itemCol.SetText ( _ ( "version" ) );
82        itemCol.SetAlign ( wxLIST_FORMAT_CENTRE );
83        InsertColumn ( 1, itemCol );
84
85        itemCol.SetText ( _ ( "  id  " ) );
86        itemCol.SetAlign ( wxLIST_FORMAT_CENTRE );
87        InsertColumn ( 2, itemCol );
88
89        itemCol.SetText ( _ ( "stream id" ) );
90        itemCol.SetAlign ( wxLIST_FORMAT_CENTRE );
91        InsertColumn ( 3, itemCol );
92
93        itemCol.SetText ( _ ( "            last time seen            " ) );
94        itemCol.SetAlign ( wxLIST_FORMAT_CENTRE );
95        InsertColumn ( 4, itemCol );
96
97        itemCol.SetText ( _ ( " kad id " ) );
98        itemCol.SetAlign ( wxLIST_FORMAT_LEFT );
99        InsertColumn ( 5, itemCol );
100
101        itemCol.SetText ( _ ( " binary id " ) );
102        itemCol.SetAlign ( wxLIST_FORMAT_LEFT );
103        InsertColumn ( 6, itemCol );
104
105        itemCol.SetText ( _ ( " distance " ) );
106        itemCol.SetAlign ( wxLIST_FORMAT_LEFT );
107        InsertColumn ( 7, itemCol );
108
109        itemCol.SetText ( _ ( "   key   " ) );
110        itemCol.SetAlign ( wxLIST_FORMAT_LEFT );
111        InsertColumn ( 8, itemCol );
112
113        SetTextColour ( *wxBLUE );
114
115        for ( int i=0; i<9; i++ )
116                SetColumnWidth ( i, wxLIST_AUTOSIZE_USEHEADER );
117
118        SetItemCount ( 0 );
119        Show();
120}
121
122
123
124void KadNodesListView::AddNode( Kademlia::CContact * contact )
125{
126        Kademlia::CContact node = * contact ;
127        delete contact ;
128
129        CUInt128 kad_id = node . GetClientID() ;
130
131        AddDebugLogLineN(logKadPrefs, CFormat ( wxT ( "KadNodesListView::AddNode" ) ) );
132
133        // is this node being added or updated ?
134        bool updated = _contacts_map.count ( kad_id );
135
136        if ( updated ) {
137                int i = _contacts_map[ kad_id ] ;
138                AddDebugLogLineN(logKadPrefs,
139                                   CFormat ( wxT ( "Updating contact n°%ld" ) ) % i );
140                _contacts_list[ i ] = node ;
141        } else {
142                AddDebugLogLineN(logKadPrefs,
143                                   CFormat ( wxT ( "Adding unknown contact n°%ld" ) ) % _contacts_list.size() );
144                _contacts_map[ kad_id ] = _contacts_list . size() ;
145                _contacts_list . push_back ( node ) ;
146        }
147        SortItems();
148}
149
150void KadNodesListView::DeleteNode( Kademlia::CContact * contact )
151{
152        Kademlia::CContact node = * contact ;
153        delete contact ;
154
155        CUInt128 kad_id = node . GetClientID() ;
156
157        wxASSERT ( _contacts_map.count ( kad_id ) );
158        unsigned int i = _contacts_map[ kad_id ];
159        _contacts_list . erase ( _contacts_list.begin() + i ) ;
160        _contacts_map  . erase ( kad_id );
161        while ( i<_contacts_list.size() ) {
162                _contacts_map[ _contacts_list[i].GetClientID() ]-- ;
163                i++ ;
164        }
165        SortItems();
166}
167
168void KadNodesListView::SortItems()
169{
170        //wxMutexLocker lock( contacts_mutex ) ;
171
172        std::vector<Kademlia::CContact> old_list = _sorted_contacts_list ;
173        _sorted_contacts_list = _contacts_list ;
174
175        std::sort ( _sorted_contacts_list.begin(), _sorted_contacts_list.end(), Compare() );
176
177        if (_sorted_contacts_list.size() != old_list.size()) {
178                SetItemCount ( _sorted_contacts_list.size() );
179        }
180        for (unsigned int i=0 ; i<_sorted_contacts_list.size(); i++) {
181                bool refresh_it = false ;
182                if (i>=old_list.size()) {
183                        refresh_it = true ;
184                } else {
185                        Kademlia::CContact current = _sorted_contacts_list.at(i);
186                        Kademlia::CContact old     = old_list.at(i);
187                        if ( ( current.GetType() != old.GetType() )
188                                        || ( current.GetVersion() != old.GetVersion() )
189                                        || ( current.GetUDPDest() != old.GetUDPDest() )
190                                        || ( current.GetTCPDest() != old.GetTCPDest() )
191                                        || ( current.GetLastSeen() != old.GetLastSeen() )
192                                        || ( current.GetClientID() != old.GetClientID() )
193                                        || ( current.GetDistance() != old.GetDistance() ) ) {
194                                refresh_it = true;
195                        }
196                }
197                if (refresh_it) {
198                        RefreshItem( i );
199                }
200        }
201}
202
203
204
205wxString KadNodesListView::OnGetItemText ( long line, long col ) const
206{
207        //wxMutexLocker lock( contacts_mutex ) ;
208        unsigned int item = (unsigned int) line, column = (unsigned int) col ;
209        if (item >= _sorted_contacts_list.size()) return wxEmptyString ;
210
211        Kademlia::CContact node = _sorted_contacts_list.at ( item ) ;
212
213        switch ( column ) {
214        case TYPE         :  return wxString() << node.GetType();
215        case CONTACT_KAD_VERSION :  return wxString() << node.GetVersion();
216        case UDP_READABLE :  return node.GetUDPDest().humanReadable() ;
217        case TCP_READABLE :  return node.GetTCPDest().humanReadable() ;
218        case LAST_SEEN    : {
219                time_t lastSeen = node.GetLastSeen() ;
220                return ( lastSeen==0 ? wxString() << _ ( "never" ) : wxDateTime ( lastSeen ).Format ( wxT ( "%c" ) ) ) ;
221        }
222        case KAD_ID_STRING:  return node.GetClientIDString() ;
223        case KAD_ID       :  return node.GetClientID().ToBinaryString().Mid ( 0,12 ) ;
224        case DISTANCE     :  return node.GetDistance().ToBinaryString().Mid ( 0,12 ) ;
225        case UDP_ID       :  return node.GetUDPDest().toString() ;
226        default           :  return ( wxString(wxT("BAD REQUEST")) );
227        }
228}
229wxListItemAttr * KadNodesListView::OnGetItemAttr ( long line ) const
230{
231        //wxMutexLocker lock( contacts_mutex ) ;
232        static wxListItemAttr attr ;
233        unsigned int item = (unsigned int) line ;
234
235        if (item >= _sorted_contacts_list.size()) return NULL ;
236        Kademlia::CContact node = _sorted_contacts_list.at ( item ) ;
237
238        // set the colour according to type
239        wxColour colour ( 0,0,0 ) ;
240        switch ( node.GetType() ) {
241        case 0 : colour = wxColour ( 0,0,255 ) ; break;
242        case 1 : colour = wxColour ( 85,0,170 ); break;
243        case 2 : colour = wxColour ( 170,0, 85 ) ; break;
244        case 3 : colour = wxColour ( 255,0,  0 ) ; break;
245        default: break;
246        }
247        attr.SetTextColour ( colour );
248        return &attr ;
249}
250
251void KadNodesListView::OnColumnLClick ( wxListEvent & event )
252{
253        AddDebugLogLineN(logKadPrefs,
254                           CFormat ( wxT ( "ColumnLClick enter °%d" ) ) % (int) event.GetColumn() );
255        wxASSERT ( event.GetColumn() >= 0 && event.GetColumn() <= UDP_ID );
256        if ( __sort_order == event.GetColumn() ) {
257                __reverse = ! __reverse ;
258        } else {
259                __sort_order = ( columns_enum ) event.GetColumn() ;
260        }
261        {
262                //wxMutexLocker lock( contacts_mutex ) ;
263                AddDebugLogLineN(logKadPrefs,
264                                   CFormat ( wxT ( "ColumnLClick sort °%d" ) ) % (int) event.GetColumn() );
265                /*for (unsigned int i=0; i<_sorted_contacts_list.size(); i++)
266                {
267                        _sorted_contacts_list.at(i).GetDataMutex().Lock();
268                }*/
269                std::sort ( _sorted_contacts_list.begin(), _sorted_contacts_list.end(), Compare() );
270                /*for (unsigned int i=0; i<_sorted_contacts_list.size(); i++)
271                {
272                        _sorted_contacts_list.at(i).GetDataMutex().Unlock();
273                }*/
274                AddDebugLogLineN(logKadPrefs,
275                                   CFormat ( wxT ( "ColumnLClick refresh items °%d" ) ) % (int) event.GetColumn() );
276                RefreshItems ( 0, _sorted_contacts_list.size()-1 ) ;
277                AddDebugLogLineN(logKadPrefs,
278                                   CFormat ( wxT ( "ColumnLClick exit °%d" ) ) % (int) event.GetColumn() );
279        }
280}
281
282
283BEGIN_EVENT_TABLE ( KadNodesListView, wxListView )
284        EVT_LIST_COL_CLICK ( -1, KadNodesListView::OnColumnLClick )
285END_EVENT_TABLE()
Note: See TracBrowser for help on using the browser.