-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathStatusPanel.java
More file actions
903 lines (807 loc) · 30.4 KB
/
StatusPanel.java
File metadata and controls
903 lines (807 loc) · 30.4 KB
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
/**
* Copyright 2013 Ronald W Hoffman
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package JavaBitcoin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
/**
* This is the status panel for the main window. It displays information about the current
* block chain and stored blocks in the database.
*/
public class StatusPanel extends JPanel implements AlertListener, ChainListener, ConnectionListener {
/** Service names */
private static final String[] serviceNames = {"Network"};
/** Create our logger */
private static final Logger log = LoggerFactory.getLogger(StatusPanel.class);
/** Block status table column classes */
private static final Class<?>[] blockColumnClasses = {
Date.class, Integer.class, String.class, String.class};
/** Block status table column names */
private static final String[] blockColumnNames = {
"Date", "Height", "Block", "Status"};
/** Block status table column types */
private static final int[] blockColumnTypes = {
SizedTable.DATE, SizedTable.INTEGER, SizedTable.HASH, SizedTable.STATUS};
/** Block status table model */
private BlockTableModel blockTableModel;
/** Block status table */
private JTable blockTable;
/** Block status scroll pane */
private JScrollPane blockScrollPane;
/** Alert table column classes */
private static final Class<?>[] alertColumnClasses = {
Integer.class, Date.class, String.class, String.class};
/** Alert table column names */
private static final String[] alertColumnNames = {
"ID", "Expires", "Status", "Message"};
/** Alert table column types */
private static final int[] alertColumnTypes = {
SizedTable.INTEGER, SizedTable.DATE, SizedTable.STATUS, SizedTable.MESSAGE};
/** Alert table model */
private AlertTableModel alertTableModel;
/** Alert table */
private JTable alertTable;
/** Alert scroll pane */
private JScrollPane alertScrollPane;
/** Connection table column classes */
private static final Class<?>[] connectionColumnClasses = {
String.class, Integer.class, String.class, String.class};
/** Connection table column names */
private static final String[] connectionColumnNames = {
"Address", "Version", "Subversion", "Services"};
/** Connection table column types */
private static final int[] connectionColumnTypes = {
SizedTable.ADDRESS, SizedTable.INTEGER, SizedTable.SUBVERSION, SizedTable.SERVICES};
/** Connection table model */
private ConnectionTableModel connectionTableModel;
/** Connection table */
private JTable connectionTable;
/** Connection scroll pane */
private JScrollPane connectionScrollPane;
/** Chain head field */
private JLabel chainHeadField;
/** Chain height field */
private JLabel chainHeightField;
/** Network difficulty field */
private JLabel networkDifficultyField;
/** Peer connections field */
private JLabel peerConnectionsField;
/**
* Create the status panel
*/
public StatusPanel() {
super(new BorderLayout());
setOpaque(true);
setBackground(Color.WHITE);
setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
JPanel tablePane = new JPanel();
tablePane.setBackground(Color.WHITE);
//
// Get the main window size
//
int frameWidth = 640;
int frameHeight = 580;
String frameSize = Main.properties.getProperty("window.main.size");
if (frameSize != null) {
int sep = frameSize.indexOf(',');
frameWidth = Integer.parseInt(frameSize.substring(0, sep));
frameHeight = Integer.parseInt(frameSize.substring(sep+1));
}
//
// Create the alert table
//
int tableHeight;
int rowHeight;
try {
alertTableModel = new AlertTableModel(alertColumnNames, alertColumnClasses);
alertTable = new SizedTable(alertTableModel, alertColumnTypes);
alertTable.setRowSorter(new TableRowSorter<TableModel>(alertTableModel));
alertTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
tableHeight = frameHeight/8;
rowHeight = alertTable.getRowHeight();
tableHeight = (tableHeight/rowHeight)*rowHeight;
alertTable.setPreferredScrollableViewportSize(new Dimension(frameWidth-70, tableHeight));
alertScrollPane = new JScrollPane(alertTable);
tablePane.add(Box.createGlue());
tablePane.add(new JLabel("<html><h3>Alerts</h3></html>"));
tablePane.add(alertScrollPane);
} catch (BlockStoreException exc) {
log.error("Block store exception while creating alert table", exc);
}
//
// Create the connection table
//
connectionTableModel = new ConnectionTableModel(connectionColumnNames, connectionColumnClasses);
connectionTable = new SizedTable(connectionTableModel, connectionColumnTypes);
connectionTable.setRowSorter(new TableRowSorter<TableModel>(connectionTableModel));
connectionTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
tableHeight = frameHeight/5;
rowHeight = connectionTable.getRowHeight();
tableHeight = (tableHeight/rowHeight)*rowHeight;
connectionTable.setPreferredScrollableViewportSize(new Dimension(frameWidth-70, tableHeight));
connectionScrollPane = new JScrollPane(connectionTable);
tablePane.add(Box.createGlue());
tablePane.add(new JLabel("<html><h3>Connections</h3></html>"));
tablePane.add(connectionScrollPane);
//
// Create the block status table
//
try {
blockTableModel = new BlockTableModel(blockColumnNames, blockColumnClasses);
blockTable = new SizedTable(blockTableModel, blockColumnTypes);
blockTable.setRowSorter(new TableRowSorter<TableModel>(blockTableModel));
blockTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
tableHeight = frameHeight/4;
rowHeight = blockTable.getRowHeight();
tableHeight = (tableHeight/rowHeight)*rowHeight;
blockTable.setPreferredScrollableViewportSize(new Dimension(frameWidth-70, tableHeight));
blockScrollPane = new JScrollPane(blockTable);
tablePane.add(Box.createGlue());
tablePane.add(new JLabel("<html><h3>Recent Blocks</h3></html>"));
tablePane.add(blockScrollPane);
tablePane.add(Box.createGlue());
} catch (BlockStoreException exc) {
log.error("Block store exception while creating block status table", exc);
}
//
// Create the status pane containing the chain head, chain height, network difficulty,
// and number of peer connections
//
chainHeadField = new JLabel();
JPanel chainHeadPane = new JPanel();
chainHeadPane.add(Box.createGlue());
chainHeadPane.add(chainHeadField);
chainHeadPane.add(Box.createGlue());
chainHeightField = new JLabel();
JPanel chainHeightPane = new JPanel();
chainHeightPane.add(Box.createGlue());
chainHeightPane.add(chainHeightField);
chainHeightPane.add(Box.createGlue());
networkDifficultyField = new JLabel();
JPanel networkDifficultyPane = new JPanel();
networkDifficultyPane.add(Box.createGlue());
networkDifficultyPane.add(networkDifficultyField);
networkDifficultyPane.add(Box.createGlue());
peerConnectionsField = new JLabel();
JPanel peerConnectionsPane = new JPanel();
peerConnectionsPane.add(Box.createGlue());
peerConnectionsPane.add(peerConnectionsField);
peerConnectionsPane.add(Box.createGlue());
JPanel statusPane = new JPanel();
statusPane.setLayout(new BoxLayout(statusPane, BoxLayout.Y_AXIS));
statusPane.setOpaque(true);
statusPane.setBackground(Color.WHITE);
statusPane.add(chainHeadPane);
statusPane.add(chainHeightPane);
statusPane.add(networkDifficultyPane);
statusPane.add(peerConnectionsPane);
statusPane.add(Box.createVerticalStrut(20));
//
// Set up the content pane
//
add(statusPane, BorderLayout.NORTH);
add(tablePane, BorderLayout.CENTER);
//
// Register for chain notifications
//
Parameters.blockChain.addListener((ChainListener)this);
//
// Register for connection notifications
//
Parameters.networkListener.addListener((ConnectionListener)this);
//
// Register for alert notifications
//
Parameters.networkListener.addListener((AlertListener)this);
//
// Get the initiali connections and update the status
//
connectionTableModel.updateConnections();
updateStatus();
}
/**
* Notification when a new block is stored in the database
*
* @param storedBlock The stored block
*/
@Override
public void blockStored(StoredBlock storedBlock) {
blockTableModel.blockStored(storedBlock);
}
/**
* Notification when a block status is changed
*
* @param storedBlock The stored block
*/
@Override
public void blockUpdated(StoredBlock storedBlock) {
blockTableModel.blockStored(storedBlock);
}
/**
* Notification when the chain is updated
*/
@Override
public void chainUpdated() {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
updateStatus();
}
});
}
/**
* Notification when a connection starts
*
* @param peer Remote peer
* @param count Connection count
*/
@Override
public void connectionStarted(Peer peer, int count) {
connectionTableModel.addConnection(peer);
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
updateStatus();
}
});
}
/**
* Notification when a connection ends
*
* @param peer Remote peer
* @param count Connection count
*/
@Override
public void connectionEnded(Peer peer, int count) {
connectionTableModel.removeConnection(peer);
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
updateStatus();
}
});
}
/**
* Notification when an alert is received
*
* @param alert Alert
*/
@Override
public void alertReceived(Alert alert) {
alertTableModel.addAlert(alert);
}
/**
* Update the status fields
*/
private void updateStatus() {
Sha256Hash chainHead = Parameters.blockStore.getChainHead();
chainHeadField.setText(String.format("<html><b>Chain head: %s</b></html>",
chainHead.toString()));
int chainHeight = Parameters.blockStore.getChainHeight();
chainHeightField.setText(String.format("<html><b>Chain height: %d</b></html>",
chainHeight));
BigInteger targetDifficulty = Parameters.blockStore.getTargetDifficulty();
BigInteger networkDifficulty = Parameters.PROOF_OF_WORK_LIMIT.divide(targetDifficulty);
String displayDifficulty = Utils.numberToShortString(networkDifficulty);
networkDifficultyField.setText(String.format("<html><b>Network difficulty: %s</b></html>",
displayDifficulty));
peerConnectionsField.setText(String.format("<html><b>Peer connections: %d</b></html>",
connectionTable.getRowCount()));
repaint(SwingUtilities.calculateInnerArea(this, null));
}
/**
* Comparator to sort the block status list from newest to oldest
*/
private class BlockStatusComparator implements Comparator<BlockStatus> {
/**
* Creates the comparator
*/
public BlockStatusComparator() {
}
/**
* Compares two block status objects in descending order
*
* @param o1 The first status object
* @param o2 The second status object
* @return -1 if less than, 0 if equal to, 1 if greater than
*/
@Override
public int compare(BlockStatus o1, BlockStatus o2) {
long t1 = o1.getTimeStamp();
long t2 = o2.getTimeStamp();
return (t1==t2 ? 0 : (t1>t2 ? -1 : 1));
}
}
/**
* Table model for the block status table
*/
private class BlockTableModel extends AbstractTableModel {
/** Column names */
private String[] columnNames;
/** Column classes */
private Class<?>[] columnClasses;
/** Block status list */
private BlockStatus[] blocks;
/** Block hash map */
private Map<Sha256Hash, BlockStatus> blockMap = new HashMap<>(50);
/** Block height map */
private Map<Integer, BlockStatus> heightMap = new HashMap<>(50);
/** Table refresh pending */
private boolean refreshPending;
/**
* Create the table model
*
* @param columnName Column names
* @param columnClasses Column classes
* @throws BlockStoreException Unable to get block status from database
*/
public BlockTableModel(String[] columnNames, Class<?>[] columnClasses) throws BlockStoreException {
super();
this.columnNames = columnNames;
this.columnClasses = columnClasses;
//
// Get the current block status and build an array sorted by descending timestamp
//
blocks = (BlockStatus[])Parameters.blockStore.getBlockStatus(150).toArray(new BlockStatus[0]);
Arrays.sort(blocks, new BlockStatusComparator());
for (BlockStatus block : blocks) {
blockMap.put(block.getHash(), block);
if (block.isOnChain())
heightMap.put(Integer.valueOf(block.getHeight()), block);
}
}
/**
* Get the number of columns in the table
*
* @return The number of columns
*/
@Override
public int getColumnCount() {
return columnNames.length;
}
/**
* Get the column class
*
* @param column Column number
* @return The column class
*/
@Override
public Class<?> getColumnClass(int column) {
return columnClasses[column];
}
/**
* Get the column name
*
* @param column Column number
* @return Column name
*/
@Override
public String getColumnName(int column) {
return columnNames[column];
}
/**
* Get the number of rows in the table
*
* @return The number of rows
*/
@Override
public int getRowCount() {
return blocks.length;
}
/**
* Get the value for a cell
*
* @param row Row number
* @param column Column number
* @return Returns the object associated with the cell
*/
@Override
public Object getValueAt(int row, int column) {
if (row >= blocks.length || column >= columnNames.length)
return "";
Object value;
BlockStatus status;
synchronized(Parameters.lock) {
status = blocks[row];
}
//
// Get the value for the requested cell
//
switch (column) {
case 0: // Date
value = new Date(status.getTimeStamp()*1000);
break;
case 1: // Height
value = Integer.valueOf(status.isOnChain() ? status.getHeight() : 0);
break;
case 2: // Block hash
value = status.getHash().toString();
break;
case 3: // Block status
if (status.isOnChain())
value = "On Chain";
else if (status.isOnHold())
value = "Held";
else
value = "Ready";
break;
default:
throw new IndexOutOfBoundsException("Table column "+column+" is not valid");
}
return value;
}
/**
* Notification when a new block is stored in the database
*
* @param storedBlock The stored block
*/
public void blockStored(StoredBlock storedBlock) {
Sha256Hash blockHash = storedBlock.getHash();
Integer blockHeight = Integer.valueOf(storedBlock.getHeight());
synchronized(Parameters.lock) {
//
// Update the block status
//
BlockStatus blockStatus = blockMap.get(blockHash);
if (blockStatus == null) {
blockStatus = new BlockStatus(blockHash, storedBlock.getBlock().getTimeStamp(),
storedBlock.getHeight(), storedBlock.isOnChain(),
storedBlock.isOnHold());
BlockStatus[] newBlocks = new BlockStatus[blocks.length+1];
System.arraycopy(blocks, 0, newBlocks, 0, blocks.length);
newBlocks[blocks.length] = blockStatus;
Arrays.sort(newBlocks, new BlockStatusComparator());
blocks = newBlocks;
blockMap.put(blockHash, blockStatus);
} else {
blockStatus.setHeight(storedBlock.getHeight());
blockStatus.setChain(storedBlock.isOnChain());
blockStatus.setHold(storedBlock.isOnHold());
}
//
// Check for an existing block at the same height. This happens
// when the chain is reorganized and blocks are removed from the chain.
//
if (storedBlock.isOnChain()) {
BlockStatus chkStatus = heightMap.get(blockHeight);
if (chkStatus == null) {
heightMap.put(blockHeight, blockStatus);
} else if (!chkStatus.getHash().equals(blockHash)) {
chkStatus.setChain(false);
chkStatus.setHeight(0);
heightMap.put(blockHeight, blockStatus);
}
}
}
//
// Update the table on the GUI thread
//
if (!refreshPending) {
refreshPending = true;
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
fireTableDataChanged();
refreshPending = false;
}
});
}
}
}
/**
* Table model for the alert table
*/
private class AlertTableModel extends AbstractTableModel {
/** Column names */
private String[] columnNames;
/** Column classes */
private Class<?>[] columnClasses;
/** Alert list */
private List<Alert> alertList;
/** Table refresh pending */
private boolean refreshPending = false;
/**
* Create the table model
*
* @param columnName Column names
* @param columnClasses Column classes
* @throws BlockStoreException Unable to get alerts from the database
*/
public AlertTableModel(String[] columnNames, Class<?>[] columnClasses) throws BlockStoreException {
super();
this.columnNames = columnNames;
this.columnClasses = columnClasses;
//
// Get the current alert list
//
alertList = Parameters.blockStore.getAlerts();
}
/**
* Get the number of columns in the table
*
* @return The number of columns
*/
@Override
public int getColumnCount() {
return columnNames.length;
}
/**
* Get the column class
*
* @param column Column number
* @return The column class
*/
@Override
public Class<?> getColumnClass(int column) {
return columnClasses[column];
}
/**
* Get the column name
*
* @param column Column number
* @return Column name
*/
@Override
public String getColumnName(int column) {
return columnNames[column];
}
/**
* Get the number of rows in the table
*
* @return The number of rows
*/
@Override
public int getRowCount() {
return alertList.size();
}
/**
* Get the value for a cell
*
* @param row Row number
* @param column Column number
* @return Returns the object associated with the cell
*/
@Override
public Object getValueAt(int row, int column) {
Object value = null;
if (row > alertList.size())
return "";
//
// The database returns the alerts sorted by the alert ID.
// We want to show the most recent alert first, so process the
// list in reverse order.
//
Alert alert = alertList.get(alertList.size()-1-row);
switch (column) {
case 0: // Alert ID
value = Integer.valueOf(alert.getID());
break;
case 1: // Expiration date
value = new Date(alert.getExpireTime()*1000);
break;
case 2: // Status
if (alert.isCanceled())
value = "Canceled";
else if (alert.getExpireTime() < System.currentTimeMillis()/1000)
value = "Expired";
else
value = "";
break;
case 3: // Alert message
value = alert.getMessage();
break;
}
return value;
}
/**
* Add a new alert to the table
*
* @param alert Alert
*/
public void addAlert(Alert alert) {
alertList.add(alert);
//
// Update the table on the GUI thread
//
if (!refreshPending) {
refreshPending = true;
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
fireTableDataChanged();
refreshPending = false;
}
});
}
}
}
/**
* Table model for the connections table
*/
private class ConnectionTableModel extends AbstractTableModel {
/** Column names */
private String[] columnNames;
/** Column classes */
private Class<?>[] columnClasses;
/** Connection list */
private List<Peer> connectionList = new LinkedList<>();
/**
* Create the table model
*
* @param columnName Column names
* @param columnClasses Column classes
*/
public ConnectionTableModel(String[] columnNames, Class<?>[] columnClasses) {
super();
this.columnNames = columnNames;
this.columnClasses = columnClasses;
}
/**
* Get the number of columns in the table
*
* @return The number of columns
*/
@Override
public int getColumnCount() {
return columnNames.length;
}
/**
* Get the column class
*
* @param column Column number
* @return The column class
*/
@Override
public Class<?> getColumnClass(int column) {
return columnClasses[column];
}
/**
* Get the column name
*
* @param column Column number
* @return Column name
*/
@Override
public String getColumnName(int column) {
return columnNames[column];
}
/**
* Get the number of rows in the table
*
* @return The number of rows
*/
@Override
public int getRowCount() {
return connectionList.size();
}
/**
* Get the value for a cell
*
* @param row Row number
* @param column Column number
* @return Returns the object associated with the cell
*/
@Override
public Object getValueAt(int row, int column) {
Object value = null;
if (row >= connectionList.size() || column >= columnClasses.length)
return "";
Peer peer = connectionList.get(row);
switch (column) {
case 0: // IP address
value = peer.getAddress().toString();
break;
case 1: // Protocol version
value = Integer.valueOf(peer.getVersion());
break;
case 2: // Subversion
value = peer.getUserAgent();
break;
case 3: // Services
long services = peer.getServices();
StringBuilder serviceString = new StringBuilder(32);
for (int i=0; i<serviceNames.length; i++) {
if ((services & (1<<i)) != 0)
serviceString.append(serviceNames[i]);
}
value = serviceString.toString();
break;
}
return value;
}
/**
* Update the connection list
*/
public void updateConnections() {
List<Peer> connections = Parameters.networkListener.getConnections();
for (Peer peer : connections) {
if (!connectionList.contains(peer))
connectionList.add(peer);
}
}
/**
* Add a new connection
*
* @param peer Peer
*/
public void addConnection(Peer peer) {
ConnectionUpdate updateTask = new ConnectionUpdate(connectionList, peer, true);
javax.swing.SwingUtilities.invokeLater(updateTask);
}
/**
* Remove a connection
*
* @param peer Peer
*/
public void removeConnection(Peer peer) {
ConnectionUpdate updateTask = new ConnectionUpdate(connectionList, peer, false);
javax.swing.SwingUtilities.invokeLater(updateTask);
}
}
/**
* Update the connection table on the GUI thread to avoid table repaint errors
* for connections that begin and end immediately
*/
private class ConnectionUpdate implements Runnable {
/** Update action */
private boolean addConnection;
/** Peer */
private Peer peer;
/** Connection list */
private List<Peer> connectionList;
/**
* Creates a new connection update
*
* @param connectionList Connection list
* @param peer Peer connection to add or remove
* @param addConnection TRUE to add a connection, FALSE to remove a connection
*/
public ConnectionUpdate(List<Peer> connectionList, Peer peer, boolean addConnection) {
this.connectionList = connectionList;
this.peer = peer;
this.addConnection = addConnection;
}
/**
* Process the event
*/
@Override
public void run() {
//
// Add or remove the peer connection
//
if (addConnection) {
if (!connectionList.contains(peer))
connectionList.add(peer);
} else {
connectionList.remove(peer);
}
//
// Update the table
//
connectionTableModel.fireTableDataChanged();
}
}
}