📄 clnn_listen_clusters.java
字号:
// $Id: Listen.java,v 1.5 2004/08/19 00:13:49 idgay Exp $/* tab:4 * "Copyright (c) 2000-2003 The Regents of the University of California. * All rights reserved. * * Permission to use, copy, modify, and distribute this software and its * documentation for any purpose, without fee, and without written agreement is * hereby granted, provided that the above copyright notice, the following * two paragraphs and the author appear in all copies of this software. * * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS." * * Copyright (c) 2002-2003 Intel Corporation * All rights reserved. * * This file is distributed under the terms in the attached INTEL-LICENSE * file. If you do not find these files, copies can be found by writing to * Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, Berkeley, CA, * 94704. Attention: Intel License Inquiry. */package net.tinyos.tools;import java.io.*;import java.util.*;//import net.tinyos.thymus_database.*; // thymus data base fileimport net.tinyos.packet.*;import net.tinyos.util.*;import net.tinyos.message.*;public class CLNN_Listen_Clusters { public static double[][] finalIntervals=new double[100][4]; public static int nIntervals=0; static int state_id=0;//new state after the marzullo static int prev_state=0; // previous state public static void main(String args[]) throws IOException { Timer timer = new Timer(); if (args.length > 0) { System.err.println("usage: java net.tinyos.tools.Listen"); System.exit(2); } PacketSource reader = BuildSource.makePacketSource(); if (reader == null) { System.err.println("Invalid packet source (check your MOTECOM environment variable)"); System.exit(2); } nIntervals = 0; try { reader.open(PrintStreamMessenger.err); for (;;) { byte[] packet = reader.readPacket(); anycast_info(packet); } } catch (IOException e) { System.err.println("Error on " + reader.getName() + ": " + e); } } public static String addzero(String in) { while(in.length() != 2) in = "0" + in; return in; } public static void anycast_info(byte[] packet) { double prob[] = new double[4]; double min[] = new double[4]; double max[] = new double[4]; if(packet.length != 34) { System.out.println("Unknown packet received"); return; } int i=5; int type = Integer.parseInt((Integer.toHexString(packet[i] & 0xff).toUpperCase()),16); System.out.println("Type: " + type); /* THIS IS REALLY BAD. My uart port is loosing a first packet for some reason. I do not know why. Dig into it later. So far it is only happens on my machine: IBM T41, RedHat9. Some how this comments stops it. HUMMMMM. SO just in case DON'T REMOVE THEM:( */ i++; String t1 = addzero(Integer.toHexString(packet[i+1] & 0xff).toUpperCase())+""+ addzero(Integer.toHexString(packet[i] & 0xff).toUpperCase()); int train = Integer.parseInt(t1,16); i+=2; String m1 = addzero(Integer.toHexString(packet[i+1] & 0xff).toUpperCase())+""+ addzero(Integer.toHexString(packet[i] & 0xff).toUpperCase()); int mote = Integer.parseInt(m1,16); String type_name=""; if(type == 3){ type_name="MARZULLO"; }else if(type == 4 ){ type_name = "FAULT_DATA"; } System.out.println("################################################"); System.out.println("Received " + type_name + " from a mode:"+mote); for(int j=0; j<3; j++) { i+=2; String a1 = addzero(Integer.toHexString(packet[i+1] & 0xff).toUpperCase())+""+ addzero(Integer.toHexString(packet[i] & 0xff).toUpperCase()); int a = Integer.parseInt(a1,16); i+=2; String b1 = addzero(Integer.toHexString(packet[i+1] & 0xff).toUpperCase())+""+ addzero(Integer.toHexString(packet[i] & 0xff).toUpperCase()); int b = Integer.parseInt(b1,16); i+=2; String c1 = addzero(Integer.toHexString(packet[i+1] & 0xff).toUpperCase())+""+ addzero(Integer.toHexString(packet[i] & 0xff).toUpperCase()); int c = Integer.parseInt(c1,16); i+=2; String d1 = addzero(Integer.toHexString(packet[i+1] & 0xff).toUpperCase())+""+ addzero(Integer.toHexString(packet[i] & 0xff).toUpperCase()); int d = Integer.parseInt(d1,16); if(j == 0 ){ //number of time a cluster won prob[0]=(double)a; prob[1]=(double)b; prob[2]=c; prob[3]=(double)d; }else if( j== 1){//minimum learned values min[0]=(double)a;min[1]=(double)b;min[2]=(double)c;min[3]=(double)d; }else{//maximum learned values max[0]=(double)a;max[1]=(double)b;max[2]=(double)c;max[3]=(double)d; } } int k=0; //DATA packet if(type == 2){ for(k=0;k<4;k++){ if(prob[k]!=0.0){ finalIntervals[k][0] = min[k]; finalIntervals[k][1] = max[k]; finalIntervals[k][2] = prob[k]; System.out.println("I_"+k+" ["+min[k]+", "+max[k]+"] P(I_"+k+")="+prob[k]); } } }//end marzullo //FAULT_DATA packet if(type == 4){ finalIntervals[0][0] = train;//number of faults finalIntervals[0][1] = prob[0];//minimum falty finalIntervals[0][2] = prob[1];//maximum faulty finalIntervals[0][3] = prob[2];//number of nodes asking for retraining //9 buffer readings finalIntervals[1][0] = min[0]; finalIntervals[1][1] = min[1]; finalIntervals[1][2] = min[2]; finalIntervals[1][3] = min[3]; finalIntervals[2][0] = max[0]; finalIntervals[2][1] = max[1]; finalIntervals[2][2] = max[2]; finalIntervals[2][3] = max[3]; finalIntervals[3][0] = prob[3]; System.out.println("Faults:"+finalIntervals[0][0]+" min:"+ finalIntervals[0][1]+" max:"+finalIntervals[0][2]+ " numFaulMode:"+finalIntervals[0][3]); System.out.println("Readings:"+finalIntervals[1][0]+" "+finalIntervals[1][1]+ " "+finalIntervals[1][2]+" "+finalIntervals[1][3]+ " "+finalIntervals[2][0]+" "+finalIntervals[2][1]+ " "+finalIntervals[2][2]+" "+finalIntervals[2][3]+ " "+finalIntervals[3][0]); } //MARZULLO Intevals else if(type == 3){ for(k=0;k<4;k++){ if(prob[k]!= 0.0){ nIntervals++; finalIntervals[k][0] = min[k]; finalIntervals[k][1] = max[k]; finalIntervals[k][2] = prob[k]/100; System.out.println("I_"+k+" ["+min[k]+", "+max[k]+"] P(I_"+k+")="+prob[k]); } } }//end marzullo //Fault notification //CALL DATABASE class try{ if(type_name.compareTo("MARZULLO")== 0){ int[] parameters=new int[3];//extra parameters to be passed into DB if(state_id != 0){ prev_state=state_id; } state_id++; parameters[0]=nIntervals; parameters[1]=state_id; parameters[2]=prev_state; //DataBaseAccess st = //new DataBaseAccess(finalIntervals, type_name, parameters); } //clear final intervals for(k=0;k<100;k++){ finalIntervals[k][0]=finalIntervals[k][1]=0.0; finalIntervals[k][2]=finalIntervals[k][3]=0.0; } }catch(Exception exc){ System.out.println("Exeption from database class "+exc); exc.printStackTrace(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -