forked from jnode/jnode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetstatCommand.java
More file actions
103 lines (90 loc) · 3.29 KB
/
Copy pathNetstatCommand.java
File metadata and controls
103 lines (90 loc) · 3.29 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
/*
* $Id$
*
* Copyright (C) 2003-2015 JNode.org
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; If not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.jnode.command.net;
import java.io.PrintWriter;
import org.jnode.driver.net.NetworkException;
import org.jnode.net.NetworkLayer;
import org.jnode.net.NetworkLayerManager;
import org.jnode.net.TransportLayer;
import org.jnode.net.util.NetUtils;
import org.jnode.shell.AbstractCommand;
import org.jnode.vm.objects.Statistic;
import org.jnode.vm.objects.Statistics;
/**
* @author epr
*/
public class NetstatCommand extends AbstractCommand {
private static final String help_super = "Print statistics for all network devices";
private static final String fmt_stat = "%s: ID %s\n";
private static final String str_none = "none";
public NetstatCommand() {
super(help_super);
}
public static void main(String[] args) throws Exception {
new NetstatCommand().execute(args);
}
/**
* Execute this command
*/
public void execute() throws Exception {
final NetworkLayerManager nlm = NetUtils.getNLM();
for (NetworkLayer nl : nlm.getNetworkLayers()) {
showStats(getOutput().getPrintWriter(), nl, 80);
}
}
private void showStats(PrintWriter out, NetworkLayer nl, int maxWidth) throws NetworkException {
out.format(fmt_stat, nl.getName(), nl.getProtocolID());
showStats(out, nl.getStatistics(), 4);
for (TransportLayer tl : nl.getTransportLayers()) {
padOutput(out, 4);
out.format(fmt_stat, tl.getName(), tl.getProtocolID());
showStats(out, tl.getStatistics(), 8);
}
out.println();
}
private void showStats(PrintWriter out, Statistics stat, int padSize)
throws NetworkException {
final Statistic[] statistics = stat.getStatistics();
if (statistics.length == 0) {
padOutput(out, padSize);
out.print(str_none);
} else {
StringBuilder buffer = new StringBuilder();
for (Statistic statistic : statistics) {
buffer.append(paddedString(padSize)).append(statistic.getName()).append(' ')
.append(statistic.getValue()).append("\n");
}
out.print(buffer.toString());
}
out.println();
}
private String paddedString(int padSize) {
String result = "";
for (int i = 0; i < padSize; i++) {
result += " ";
}
return result;
}
private void padOutput(PrintWriter out, int size) {
for (int i = 0; i < size; i++) {
out.print(" ");
}
}
}