📄 realtimetrafficapplet.java
字号:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Insets;
import javax.swing.JPanel;
import javax.swing.JApplet;
import javax.swing.JLabel;
import java.awt.Font;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
public class RealtimeTrafficApplet extends JApplet implements Runnable{
/** applet state constants */
private static final int asFinal = 0;
private static final int asInitialized = 1;
private static final int asStarted = 2;
private static final int asStopped = 3;
/** applet 最大流量 这里是有问题的 那么其他的applet 对象共享此变量*/
private static int maxInput = 0;
private static int maxOutput = 0;
/** 传给servlet的值,从tag中取*/
private String routerIp = "";
private String interfaceName = "";
/** the window containing this applet */
//JSObject m_window;
/** name of the JavaScript function to call when new tupple arrives */
//String m_strDataHandler = null;
/**
* applet state - see asXxxx and init() destroy() start() stop()
*/
int m_iAppletState = asFinal;
/** worker thread object */
Thread m_worker = null;// new Thread(this);
/** URL to perfmon ASP/CGI */
URL m_url = null;
/** input stream to m_url */
InputStream m_input = null;
int m_iTickSecs = 0;
TrendChart trendChart = new TrendChart();
UtilitiesMeter outputChart = new UtilitiesMeter();
UtilitiesMeter inputChart = new UtilitiesMeter();
boolean m_bShowLegend = true;
boolean m_bShowBorder = false;
private int iBandWidth;
private JPanel jContentPane = null;
private JPanel nChart = null;
private JLabel jLabel = null;
private JLabel jLabel1 = null;
private JLabel jLabel2 = null;
private JLabel jLabel3 = null;
private JLabel jLabel4 = null;
private JLabel jLabel5 = null;
private JLabel jLabel6 = null;
private JLabel jLabel7 = null;
private JLabel jLabel8 = null;
private JLabel jLabel9 = null;
private JLabel jLabel10 = null;
private JLabel jLabel11 = null;
private JLabel jLabel_Input = null;
private JLabel jLabel_Output = null;
private JLabel jLabel14 = null;
private JLabel jLabel15 = null;
private JLabel jLabel16 = null;
private JLabel jLabel17 = null;
private JLabel jLabel_MaxInput = null;
private JLabel jLabel_MaxOutput = null;
private JLabel jLabel20 = null;
private JLabel jLabel21 = null;
/**
* This is the default constructor
*/
public RealtimeTrafficApplet() {
super();
}
/**
* This method initializes this
*
* @return void
*/
public void init() {
this.setSize(600, 395);
TRACE("init methord \t");
TRACE("init()");
setLayout(new GridLayout(2,1));
this.setContentPane(getJContentPane());
/**从tag中读取数值*/
m_bShowLegend = (getIntParameter("showLegend", 1) != 0);
m_bShowBorder = (getIntParameter("showBorder", 0) != 0);
m_iTickSecs = getIntParameter("tick", 2, 1, 100000);
iBandWidth = getIntParameter("bandWidth",100);
routerIp = getParameter("routerIP");
interfaceName = getParameter("interfaceName");
trendChart.setColors(getColorParameter("backgroundColor"), getColorParameter("gridColor"), null);
outputChart.setColors(getColorParameter("backgroundColor"), getColorParameter("gridColor"), null);
inputChart.setColors(getColorParameter("backgroundColor"), getColorParameter("gridColor"), null);
/** 设置哪一个是输入的*/
outputChart.setInput(false);
inputChart.setInput(true);
String url = getParameter("url");
if(url == null)
return;
int iMaxChannels = getIntParameter("counters", 1, 1, 32);
trendChart.setMaxChannels(iMaxChannels);
/**
* 对两个cpumeter 进行初始化
* 设置当前接口的最大带宽
* 同时也设置了,y轴的刻度值
*/
trendChart.setBandWidth(iBandWidth);
outputChart.setBandWidth(iBandWidth);
inputChart.setBandWidth(iBandWidth);
for(int iChannel = 0; iChannel < iMaxChannels; iChannel++)
{
// String cntr = getParameter("counter" + iChannel);
int iScale = getIntParameter("scale" + iChannel, 1);
Color clr = getColorParameter("color" + iChannel);
/**
* 从html tag 中读取到各个channel 的属性设置后
* 给m_chart发消息设置m_chart,相应channel的属性
*/
trendChart.setChannel(iChannel, iScale, clr);
// clr = trendChart.getChannelColor(iChannel);
// String row[] = {"______", Integer.toString(iScale), cntr, "0"};
}
// build m_url
try
{
java.net.URL base = getDocumentBase();
m_url = new java.net.URL(base, url);
}
catch(Exception x)
{
TRACE("Caught in init: " + x);
//m_iMaxChannels = 0;
return;
}
TRACE("url built: " + m_url);
// done!
m_iAppletState = asInitialized;
}
/**
* Applet callback
* Do some cleanup
*/
public void destroy()
{
TRACE("destroy methord \t");
TRACE("destroy()");
if(m_input != null)
{
try { m_input.close(); } catch(Exception x) {}
m_input = null;
m_url = null;
}
m_iAppletState = asFinal;
}
/**
* Applet callback
*/
public void start()
{
TRACE("start methord \t");
TRACE("start()");
m_worker = new Thread(this);
m_worker.start();
m_iAppletState = asStarted;
}
/**
* Applet callback
*/
public void stop()
{
TRACE("stop methord \t");
TRACE("stop()");
m_worker = null;
m_iAppletState = asStopped;
// shut down the input stream if any
// this will also cause worker thread to terminate!
if(m_input != null)
{
try { m_input.close(); } catch(Exception x) {}
m_input = null;
TRACE("INPUT STREAM SHUT!");
}
}
/**
* background worker thread
*/
public void run()
{
TRACE("run methord \t");
// not to produce extra garbage... let's reuse this thing
int tuple[] = new int[trendChart.getMaxChannels()];
// open input stream to the m_url!
try
{
TRACE("connecting to ... " + m_url);
URLConnection cntn = m_url.openConnection();
cntn.setDoOutput(true); // we will be POSTing!
cntn.setUseCaches(false);
// cntn.connect();
// do POST args
OutputStream out = cntn.getOutputStream(); // this will also call cntn.connect()
PrintWriter outWriter = new PrintWriter(out);
String postArgs = "tick=" + Integer.toString(m_iTickSecs) + "\n";
TRACE("postArgs: " + postArgs);
outWriter.println(postArgs);
outWriter.close();
m_input = cntn.getInputStream();
}
catch(Exception x)
{
TRACE("Caught in run(): " + x);
return;
}
try
{
// keep reading while there is something to read
// while((m_worker != null) && (m_input != null))
// {
// int res[] = readTuple(tuple);
// if(res == null)
// break;
// /**
// *res是一个int型数组,并且这个数组是一个大小可变的数组
// *数组中存的是每个线程从url中读取到的数据,因为每个线程要绘制多条折线
// *所以要传一个数组上来,并且要知道传来的是时间点的一些数据
// *如果按照,现在的设计,一个applet读取5个图的数据,我觉得应该从url中读取
// *看示意图
// */
// processTuple(res);
// }
//替换上面的while
for(int i = 0;i < 100;i++){
int[] res = new int[2];
res[0] = i;
res[1] = 100 - i;
processTuple(res);
for(long l = 0;l<500000000;l++){
l++;
l--;
}
}
}
catch(Exception x)
{
TRACE("2Caught in run(): " + x);
x.printStackTrace();
}
// shut down the input stream if any
if(m_input != null)
{
try { m_input.close(); } catch(Exception x) {}
m_input = null;
TRACE("INPUT STREAM SHUT!");
}
}
/**
* Buffer for reading a line of text.
* Made a member just to save on object alloc/dealloc
*/
byte m_lineBuf[] = null;
/**
* m_iLineBufSize is m_lineBuf.length
*/
int m_iLineBufSize = 0;
/**
* read a line of text from the m_input
* 从m_input中依次读写字符,然后构造成一个字符串
*/
private String readLine()
throws IOException
{
int i = 0; //pointer in m_lineBuf
for(;;)
{
if(m_input == null)
return null;
int ch = m_input.read();
//TRACE("m_input.read() => " + ch);
if(ch < 0)
{
if(i == 0)
return null;
break;
}
// treat \r\n as an end of line
else if(ch == '\r')
continue;
else if(ch == '\n')
break;
// ensureCapacity
if(i >= m_iLineBufSize)
{
byte newBuf[] = new byte[m_iLineBufSize + 256];
if(m_lineBuf != null)
System.arraycopy(m_lineBuf, 0, newBuf, 0, m_iLineBufSize);
m_lineBuf = newBuf;
m_iLineBufSize += 256;
}
m_lineBuf[i++] = (byte)ch;
}
return new String(m_lineBuf, 0, i);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -