// private IGatewayService igs;
/**
*
*/
private static final long serialVersionUID = -3858789379884703168L;
@Autowired
private GateWayService igs;
private DatagramSocket ds;
private DatagramPacket dp;
private Map<String, GateWay> allOnlineGateway;
private byte[] key = null;
private boolean flag_datagram = true;
private Random numRandom;
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
super.init();
getServletContext().setAttribute("title", "XXXX");
// WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
// igs = (IGatewayService) wac.getBean("gatewayServiceImpl");
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
allOnlineGateway = new HashMap<String, GateWay>();
try {
ds = new DatagramSocket(8080);
} catch (SocketException ex) {
Logger.getLogger(P2PServlet.class.getName()).log(Level.ERROR, null, ex);
}
System.out.println("启动中 .......servlet .......");
key = "sxycSongsk".getBytes();
byte[] buf = new byte[512];
dp = new DatagramPacket(buf, 512);
numRandom = new Random();
//数据接收线程
new Thread() {
//private byte [] buf = new byte[512];
//private DatagramPacket recvdp ;
private final String replyack = "/ack/ok";
@Override
public void run() {
System.out.println("测试 ....1");
while (flag_datagram) {
try {
ds.receive(dp);
} catch (IOException ex) {
Logger.getLogger(P2PServlet.class.getName()).log(Level.ERROR, null, ex);
}
String d = new String(dp.getData(), 0, dp.getLength());
System.out.println(d);
try {
ds.send(new DatagramPacket(d.getBytes(), d.length(), dp.getSocketAddress()));
// try {
// d = Decode(new String(dp.getData(), 0,
// dp.getLength()));
// } catch (Exception ex) {
// Logger.getLogger(P2PServlet.class.getName()).log(Level.SEVERE,
// null, ex);
// }
} catch (SocketException ex) {
Logger.getLogger(P2PServlet.class.getName()).log(Level.ERROR, null, ex);
} catch (IOException ex) {
Logger.getLogger(P2PServlet.class.getName()).log(Level.ERROR, null, ex);
}
if (d == null) {
// do nothing when data is wrong;
continue;
}
String[] data = d.split("/");
if (data[0].equals("ack")) {
System.out.println("ack");
if (allOnlineGateway.containsKey(data[1])) {
// when receive currect data, check gateway;
allOnlineGateway.get(data[1]).setCount(0);
try {
ds.send(new DatagramPacket(replyack.getBytes(), replyack.length(),
dp.getSocketAddress()));
} catch (SocketException ex) {
Logger.getLogger(P2PServlet.class.getName()).log(Level.ERROR, null, ex);
} catch (IOException ex) {
Logger.getLogger(P2PServlet.class.getName()).log(Level.ERROR, null, ex);
}
try {
igs.updateGatewayStatus(data[1], 1, dp.getAddress().getHostAddress(), data[2]);
} catch (Exception ex) {
Logger.getLogger(P2PServlet.class.getName()).log(Level.ERROR, null, ex);
}
} else {
GateWay g = null;
try {
g = igs.getgatewaybyid(data[1]);
} catch (Exception ex) {
Logger.getLogger(P2PServlet.class.getName()).log(Level.ERROR, null, ex);
}
if (g != null) {
g.setCount(0);
try {
igs.updateGatewayStatus(g.getGateid(), 1, dp.getAddress().getHostAddress(), data[2]);
} catch (Exception ex) {
Logger.getLogger(P2PServlet.class.getName()).log(Level.ALL, null, ex);
}
allOnlineGateway.put(g.getGateid(), g);
}
}
} else {
}
}
}
}.start();
// 数据发送线程
new Thread() {
@Override
public void run() {
while (flag_datagram) {
Iterator<Map.Entry<String, GateWay>> iter = allOnlineGateway.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<String, GateWay> e = iter.next();
if (e.getValue().getCount() <= 10) {
int count = e.getValue().getCount();
e.getValue().setCount(++count);
} else {
try {
igs.updateGatewayStatus(e.getValue().getGateid(), 0, null, e.getValue().getGateid());
} catch (Exception ex) {
Logger.getLogger(P2PServlet.class.getName()).log(Level.ERROR, null, ex);
}
System.out.println("remove gateway");
iter.remove();
}
}
try {
Thread.sleep(30000);
} catch (InterruptedException ex) {
Logger.getLogger(P2PServlet.class.getName()).log(Level.ERROR, null, ex);
}
}
}
}.start();
}
@Override
public void destroy() {
super.destroy(); //To change body of generated methods, choose Tools | Templates.
if (ds != null) {
ds.close();
ds = null;
}
flag_datagram = false;
}
}