package com.dev.data; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.MulticastSocket; import java.net.SocketAddress; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.Date; import java.util.Enumeration; import java.util.List; import com.base.BaseData; import com.base.Com; import com.config.AppConfig; import com.sql.MysqlConnPool; public class Dev_UDPServer_Thread implements Runnable{ public static final int ZIZHAN_DATATYPE = 1; //×ÓÕ¾Êý¾Ý public static final int QIFA_DATATYPE = 2; //Æû·¢Êý¾Ý public static final int BATTERY_DATA_TYPE = 3; //Ðîµç³Ø×éÊý¾Ý public MysqlConnPool conn_pool; public List devices; public List configs; public static int thread_count = 0; //×ÓÏ̸߳öÊý public static final int port = 6556; //×ÓվͨѶ¶Ë¿Ú public static final String server_ip = "235.0.0.5"; //×é²¥ipµØÖ· public static final int MAX_DATA_COUNT = 1024; public AppConfig cfg; public DatagramSocket datagramSocket; public Dev_UDPServer_Thread(MysqlConnPool gB_MysqlConnPool, List devices,List configs,AppConfig cfg) { this.conn_pool = gB_MysqlConnPool; this.devices = devices; this.configs = configs; this.cfg = cfg; } @Override public void run() { System.out.println("Dev_UDPServer_Thread start at "+Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms)); InetAddress group = null; MulticastSocket socket = null; try { new Thread(new Runnable() { @Override public void run() { // Ö÷Ï߳̽ÓÊÜ¿ØÖÆÌ¨ÊäÈë²¢¹ã²¥³öÈ¥ InetAddress heartgroup = null; DatagramPacket heartpack = null; MulticastSocket socket1 = null; int heart_error_count = 0; //ͨѶ´íÎó¼ÆÊý while(true) { try { heartgroup = InetAddress.getByName(server_ip); heartpack = new DatagramPacket(new byte[0], 0, heartgroup, port); // Ä¿µÄ¶Ë¿ÚºÍMulticastSocket¶Ë¿ÚÒ»Ñù£¡£¡ socket1 = createMulticastGroupAndJoin(heartgroup,port); socket1.setSoTimeout(4000);; heart_error_count = 0; //boolean isNewThread = true; //String last = Com.getDateTimeFormat(new Date(2000,1,1), Com.DTF_Y_M_D); //ÉÏÒ»´Î½¨±íµÄymd while(true) { //·¢ËÍÐÄÌø°ü try { /*if(isNewThread || !last.equals(nowdate)) { if(isNewThread) { } isNewThread = false; last = nowdate; }*/ //System.out.println(Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms)); //System.out.println("Ïß³Ì×ÜÊý£º"+thread_count); //²âÊÔÒì³£Çé¿öϳÌÐòÊÇ·ñÄÜÕý³£¿ªÆô¶Ë¿Ú¼àÌý //System.out.println(1/0); //¹¹ÔìÐÄÌø°üÒÔ¼°·¢ËÍÐÄÌø°ü byte[] buf = BaseData.createHeartData(); heartpack.setData(buf); socket1.send(heartpack); // System.out.println(socket1.getNetworkInterface().getInetAddresses()); // Enumeration ips = socket1.getNetworkInterface().getInetAddresses(); // while(ips.hasMoreElements()) { // System.out.println(ips.nextElement().getHostAddress()); // } //System.err.println("·¢ËÍÐÄÌø°ü"+socket1.isConnected()+"==="+socket1.isClosed()+"=="+socket1.isBound()+Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms)); Thread.sleep(5000); if(heart_error_count >10) { break; } } catch (Exception e) { heart_error_count++; try { Thread.sleep(1000); } catch (InterruptedException e1) { e1.printStackTrace(); } e.printStackTrace(); } } } catch (Exception e1) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } e1.printStackTrace(); } finally { try { socket1.leaveGroup(heartgroup); } catch (IOException e) { e.printStackTrace(); } if(socket1 != null) { socket1.close(); } } } } }).start(); byte[] data = null; DatagramPacket packet= null; int conn_error_count = 0; while(true) { try { data = new byte[MAX_DATA_COUNT]; group = InetAddress.getByName(server_ip); socket = createMulticastGroupAndJoin(group,port); //¼ÓÈë×é²¥×飬ÉèÖÃ×é²¥×éµÄ¼àÌý¶Ë¿ÚΪ6556 socket.setSoTimeout(5000); DatagramPacket outpack = new DatagramPacket(new byte[0], 0, group, port); // Ä¿µÄ¶Ë¿ÚºÍMulticastSocket¶Ë¿ÚÒ»Ñù£¡£¡ packet = new DatagramPacket(data, data.length); conn_error_count = 0; while(true) { try { socket.receive(packet); // ͨ¹ýMulticastSocketʵÀý¶Ë¿Ú´Ó×é²¥×é½ÓÊÕÊý¾Ý byte[] headCount = new byte[2]; System.arraycopy(data, 0, headCount, 0, headCount.length); //System.out.println("Êý¾Ý°ü³¤¶È"+createPackHeadCount(data)); int dataCount = BaseData.createPackHeadCount(headCount); if(dataCount > data.length) { continue; //Êý¾ÝÁ¿³¬±êÊý¾ÝÖ¡ } ByteBuffer bf = ByteBuffer.allocate(dataCount); bf.order(ByteOrder.LITTLE_ENDIAN); bf.put(data,0,dataCount); bf.flip(); //System.out.println(ComBase.calchecksum(bf.array())); //System.out.println("½ÓÊÕµ½Êý¾Ý:"+ComFn.bytesToHexString(bf.array(), bf.array().length)); BaseData baseData = new BaseData(); if(baseData.putByteBuffer(bf,cfg.isEquie_device_udppackage_cheak())) { //System.err.println("packtype:"+baseData.packtype); if(BaseData.ClildStood_DevType == baseData.packtype) { //×ÓÕ¾ÀàÐÍÊý¾Ý°ü if(baseData.devident >= 0x0701 && baseData.devident <= 0x0707) { String client_ip = packet.getSocketAddress().toString(); RecordUDPPackage.RecordUDPPackageData(RecordUDPPackage.PackageType_ChildStood, client_ip, bf.array()); ChildStoodParse_Thread childthread = new ChildStoodParse_Thread(conn_pool, devices,configs, baseData,ZIZHAN_DATATYPE); new Thread(childthread).start(); } //ChildStoodParse_Thread childthread = new ChildStoodParse_Thread(conn_pool, devices,configs, baseData,ZIZHAN_DATATYPE); //new Thread(childthread).start(); thread_count ++; }else if(BaseData.Battery_DevType == baseData.packtype) { //Ðîµç³Ø×éÀàÐÍÊý¾Ý°ü String client_ip = packet.getSocketAddress().toString(); RecordUDPPackage.RecordUDPPackageData(RecordUDPPackage.PackageType_Battery, client_ip, bf.array()); BatteryParse_Thread battthread = new BatteryParse_Thread(conn_pool, devices, baseData); new Thread(battthread).start(); thread_count ++; }else if(BaseData.SwitchBoard_DevType == baseData.packtype) { String client_ip = packet.getSocketAddress().toString(); RecordUDPPackage.RecordUDPPackageData(RecordUDPPackage.PackageType_SwitchBoard, client_ip, bf.array(),baseData.getDateFromDate()); //¿ª¹ØÁ¿°åÀàÐÍÊý¾Ý°ü SwitchBoardParse_Thread switchthread = new SwitchBoardParse_Thread(conn_pool, devices, baseData); new Thread(switchthread).start(); thread_count ++; } } } catch (Exception e) { conn_error_count ++; if(conn_error_count >=6) { break; } //e.printStackTrace(); } //²âÊÔ³ÌÐòÕý³£ÔËÐÐÒì³£Çé¿öÏÂÒì³£Çé¿ö //System.out.println(1/0); Thread.sleep(10); } } catch (Exception e) { e.printStackTrace(); } finally { socket.leaveGroup(group); if(socket != null) { try { socket.close(); } catch (Exception e) { e.printStackTrace(); } } } } } catch (Exception e1) { try { Thread.sleep(500); } catch (InterruptedException e) { //e.printStackTrace(); } e1.printStackTrace(); } finally { System.out.println("Ïß³ÌÍ˳ö¡£¡£¡£¡£¡£¡£¡£¡£¡£"); // if (socket != null) { // try { // socket.leaveGroup(group); // } catch (Exception e1) { // e1.printStackTrace(); // } // socket.close(); // } } } /** * ´´½¨ºÍ¼ÓÈëÖ¸¶¨×é²¥ * @param groupurl * @param port * @return */ private MulticastSocket createMulticastGroupAndJoin(InetAddress group,int port) { try { MulticastSocket socket = new MulticastSocket(port); // ³õʼ»¯MulticastSocketÀಢ½«¶Ë¿ÚºÅÓëÖ®¹ØÁª //InetAddress bind = new InetSocketAddress("192.0.0.74", port); //MulticastSocket socket = new MulticastSocket(bind); // ³õʼ»¯MulticastSocketÀಢ½«¶Ë¿ÚºÅÓëÖ®¹ØÁª socket.setLoopbackMode(false); socket.joinGroup(group); // ¼ÓÈë´Ë×é²¥×é return socket; } catch (Exception e1) { System.err.println("Error: " + e1 +"\n ¼ÓÈë×鲥ʧ°Ü at "+Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms)); // ²¶×½Òì³£Çé¿ö return null; } } public static void main(String[] args) { Short[] b = {0x44,0x00,0x07,0x78,0x03,0x0d,0x08,0x01,0x00,0x00,0x04,0x02,0x81,0x08,0x00,0x00, 0x80,0x3f,0x00,0x00,0x00,0x40,0x00,0x00,0x40,0x40,0x00,0x00,0x80,0x40,0x00,0x00, 0xa0,0x40,0x00,0x00,0xc0,0x40,0x00,0x00,0xe0,0x40,0x00,0x00,0x00,0x41,0x00,0x00, 0x10,0x41,0x00,0x00,0x20,0x41,0x00,0x00,0x30,0x41,0x00,0x00,0x40,0x41,0x01,0x00, 0x07,0x00,0xfc,0x66}; } }