⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 multihopinjector.java

📁 无线传感器网络操作系统源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	    }	}			if (debug >0) 	    System.out.print("\nMissing packets:");	for (int i =0; i < ((length+15)>>4); i++) {	    if (!packets_received[i]) {		if (debug >0)		    System.out.print(i+" ");		if (logPanel != null) {		    if (i % 10 == 0) {logPanel.repaint();}		}		sendCapsule(node, i * 16);		try {		    Thread.currentThread().sleep(500);		} catch (Exception e){}	    }	}		writeDummyPacket(node);    }    public synchronized void multicheck(int nnodes) throws IOException {	boolean redo = false;	int n;	for (int i = 0; i < 64; i+=16) {	    awaitingResponse = nnodes;	    n = 0;	    while ((awaitingResponse > 0) && (n < nretries)) {		if (debug > 0)		    System.out.print("+");		n++;		readCapsule((short)-1, MAX_CODE_SIZE+i, 0);		try {		    wait(400);		} catch (InterruptedException e) {		    System.err.println("Interrupted wait:"+e);		    e.printStackTrace();		}	    }	}			if (debug >0) 	    System.out.print("\nMissing packets:");		for (int q= 0; q < nrepeats; q++) {	for (int i =0; i < ((length+15)>>4); i++) {	    if (!packets_received[i]) {		if (debug >0)		    System.out.print(i+" ");		if (logPanel != null) {		    if (i % 10 == 0) {logPanel.repaint();}		}		redo = true;		sendCapsule((short)-1, i * 16);		try {		    Thread.currentThread().sleep(200);		} catch (Exception e){}	    }	}}			writeDummyPacket((short)-1);	if (redo) {	    for (int i = 0; i < 512; i++) {		packets_received[i] = true;	    }	    multicheck(nnodes);	}    }	    public void id(short node) throws IOException {	readCapsule(node, 32768 - 64);	if (logPanel == null){	    try {		Thread.currentThread().sleep(3000);	    } catch (Exception e) {}}    }    public void setId(short node, short newID) {	byte [] packet = new byte[MSG_LENGTH];	int capsule = 32768 - 64;	packet[0] = (byte) (node & 0xff);	packet[1] = (byte) ((node >> 8) & 0xff);	packet[2] = MSG_WRITE;	packet[3] = group_id;	packet[4] = 1; //this is a request	packet[6] = (byte) ((prog_id >> 8) & 0xff); 	packet[5] = (byte) (prog_id & 0xff);        	packet[8] = (byte) ((capsule >> 8) & 0xff);	packet[7] = (byte) (capsule & 0xff);     	IncSeqNo();	packet[9] = (byte) (seqno & 0xff);	packet[10] = (byte) ((seqno >> 8) & 0xff);     	packet[12] = (byte) ((newID >> 8) & 0xff);	packet[11] = (byte)(newID & 0xff);	packet[14] = (byte) ((prog_id >> 8) & 0xff); 	packet[13] = (byte) (prog_id & 0xff);        	packet[16] = (byte) ((length >> 8) & 0xff); 	packet[15] = (byte) (length & 0xff);        	try {	    preparePacket(packet);	    serialStub.Write(packet); 	} catch (Exception e ) {	    System.err.println("Something bad happened in setID"+e);	    e.printStackTrace();	}    }    public void startProgram(short node)  throws IOException{	byte [] packet = new byte[MSG_LENGTH];	packet[0] = (byte) (node & 0xff);	packet[1] = (byte) ((node >> 8) & 0xff);	packet[2] = MSG_START;	packet[3] = group_id;	IncSeqNo();	packet[4] = (byte) (seqno & 0xff);	packet[5] = (byte) ((seqno >> 8) & 0xff);	packet[7] = (byte) ((prog_id >> 8) & 0xff); 	packet[6] = (byte) (prog_id & 0xff);        	//	packet[6] = (byte) ((i >> 8) & 0xff);	//	packet[5] = (byte) (i & 0xff);          	preparePacket(packet);	serialStub.Write(packet);    }    public void fillBitmap(short node) throws IOException{	byte bitmap[] = new byte[64]; 	for (int i = 0; i < 64; i++) {	    bitmap[i] = (byte)0xff;	}	for (int i =0; i < ((length+15)>>4); i++) {	    bitmap[(i >> 3) & 63] &= (byte)(~(1 << (i & 0x7)));	}	for (int i = 0; i < 64; i++) {	    System.out.print(Integer.toHexString(bitmap[i] & 0xff) + " "); 	}	System.out.println();	for (int i = 0; i < 4; i++) {	    byte [] packet = new byte[MSG_LENGTH];	    int capsule = 8192 + (i * 16);	    packet[0] = (byte) (node & 0xff);	    packet[1] = (byte) ((node >> 8) & 0xff);	    packet[2] = MSG_WRITE;	    packet[3] = group_id;	    packet[4] = 1; //this is a request	    packet[6] = (byte) ((prog_id >> 8) & 0xff); 	    packet[5] = (byte) (prog_id & 0xff);        	    packet[8] = (byte) ((capsule >> 8) & 0xff);	    packet[7] = (byte) (capsule & 0xff);  	    IncSeqNo();	    packet[9] = (byte) (seqno & 0xff);	    packet[10] = (byte) ((seqno >> 8) & 0xff);        	    System.arraycopy(bitmap, i*16, packet, 11, 16);	    int any = 0;	    for (int j = 0; j < 16; j++) {		any |= packet[7+j];	    }	    if (any != 0) {		if (debug > 0)		    System.out.print("+");		preparePacket(packet);		serialStub.Write(packet);		try {		    Thread.currentThread().sleep(300);		} catch (Exception e) {}	    } else {		if (debug > 0)		    System.out.print("?");	    }	}    }        public void newProgram(short node)  throws IOException{	byte [] packet = new byte[MSG_LENGTH];	packet[0] = (byte) (node & 0xff);	packet[1] = (byte) ((node >> 8) & 0xff);	packet[2] = MSG_NEW_PROG;	packet[3] = group_id;	IncSeqNo();	packet[4] = (byte) (seqno & 0xff);	packet[5] = (byte) ((seqno >> 8) & 0xff);	packet[7] = (byte) ((prog_id >> 8) & 0xff); 	packet[6] = (byte) (prog_id & 0xff);        	packet[9] = (byte) ((length >> 8) & 0xff);	packet[8] = (byte) (length & 0xff);          	preparePacket(packet);	serialStub.Write(packet);	fillBitmap(node);    }    private void printPacket(byte[] packet) {	System.out.print("Packet:");	for (int i = 0; i < packet.length; i++) {	    if (i % 16 == 0) {System.out.println();}	    String val = Integer.toHexString((int)(packet[i] & 0xff));	    for (int j = 0; j < (3 - val.length()); j++) {		System.out.print(" ");	    }	    	    System.out.print(val);	}	System.out.println();    }    public void readCapsule(short node, int capsule) throws IOException {	readCapsule(node, capsule, 0);    }    public void readCapsule(short node, int capsule, int check) throws IOException {		byte [] packet = new byte[MSG_LENGTH];	packet[0] = (byte) (node & 0xff);	packet[1] = (byte) ((node >> 8) & 0xff);	packet[2] = MSG_READ;	packet[3] = group_id;	packet[4] = 1; //this is a request	packet[6] = (byte) ((prog_id >> 8) & 0xff); 	packet[5] = (byte) (prog_id & 0xff);        	packet[8] = (byte) ((capsule >> 8) & 0xff);	packet[7] = (byte) (capsule & 0xff);          	packet[10] = (byte) ((GENERIC_BASE_ADDR >> 8) & 0xff);	packet[9] = (byte) (GENERIC_BASE_ADDR & 0xff);	packet[11] = (byte) check;	IncSeqNo();	packet[12] = (byte) (seqno & 0xff);	packet[13] = (byte) ((seqno >> 8) & 0xff);	preparePacket(packet);	serialStub.Write(packet);    }    public static void usage() {	System.out.println("Usage: java codeGUI.MultihopInjector command args\n");	System.out.println("where command is one of the following:\n"+			   "\tnew srec dest-- initialize uploading a new program(erase counters, etc.). \n"+			   "\tstart srec dest -- start reprogramming on a given node\n"+			   "\twrite srec dest -- write capsules to the appropriate nodes\n"+			   "\tread srec dest -- read capsules from the stored nodes\n"+			   "\tcheck srec dest -- resend unreceived packets\n"+			   "\tid [dest] -- find an id of a node. "+			   "dest defaults to broadcast\n" +			   "\tsetid [oldid] newid -- set ID of a mote (requires power cycling). oldid defaults to broadcast\n"+			   "\tmultiprog srec nnodes -- reliably download code to nnodes.\n"+			   "srec file in the above usage refers to the program"+" to download. It is best specified with absolute paths.\n");	    System.exit(-1);	}	    public static void main(String[] args) {	MultihopInjector ic;	SerialStub r;	try{	    try {		ic = new MultihopInjector("localhost", 9000); 		r = ic.getStub();		r.Open();		r.registerPacketListener(ic);	    } catch(IOException e) {		ic = new MultihopInjector("COM1");		r = ic.getStub();		r.Open();		r.registerPacketListener(ic);	    }				    Thread rt = new Thread(ic);	    rt.setDaemon(true);	    rt.start();	    if (args.length == 0) {		usage();	    }	    if (args[0].equals("help")) {		usage();	    } else if (args[0].equals("new")) {		if (args.length != 3) 		    usage();		ic.readCode(args[1]);		int node = Integer.parseInt(args[2]);		ic.newProgram((short)node);	    } else if (args[0].equals("start")) {		if (args.length != 3) 		    usage();		ic.readCode(args[1]);		int node = Integer.parseInt(args[2]);		ic.startProgram((short)node);	    } else if (args[0].equals("write")) {		if (args.length < 3) 		    usage();		else if (args.length > 3){		    shortDelay = Integer.parseInt(args[3]);		    longDelay = Integer.parseInt(args[4]);		}		ic.readCode(args[1]);		int node = Integer.parseInt(args[2]);				ic.download((short)node);	    } else if (args[0].equals("read")) {		if (args.length != 3) 		    usage();		ic.readCode(args[1]);		int node = Integer.parseInt(args[2]);		ic.verify((short)node);	    } else if (args[0].equals("check")) {		if (args.length != 3) 		    usage();		ic.readCode(args[1]);		int node = Integer.parseInt(args[2]);		ic.check((short) node);	    } else if (args[0].equals("id")) {		int node = -1;		if (args.length > 2 ) 		    usage();		if (args.length > 1) 		    node = Integer.parseInt(args[1]);		ic.id((short) node);	    } else if (args[0].equals("setid")) {		int node = -1;		int nid = -1;		if (args.length == 3) {		    node = Integer.parseInt(args[1]);		    nid = Integer.parseInt(args[2]);		} else if (args.length == 2) {		    nid = Integer.parseInt(args[1]);		} else {		    usage();		}		for (int i = 0; i < 3; i++) {		    ic.newProgram((short) node);		    try {			Thread.currentThread().sleep(500);		    } catch (Exception e) {}		}		for (int i = 0; i < 3; i++) {		    ic.setId((short) node, (short) nid);		    try {			Thread.currentThread().sleep(500);		    } catch (Exception e) {}		}	    } else if (args[0].equals("multiprog")) {		if (args.length < 3) 		    usage();		if (args.length > 3)		    ic.nrepeats = Integer.parseInt(args[3]);		if (args.length > 4) 		    ic.nretries = Integer.parseInt(args[4]);		ic.readCode(args[1]);		int nnodes = Integer.parseInt(args[2]);		for (int i = 0; i < nrepeats; i++) {		    ic.newProgram((short)-1);		    try {			Thread.currentThread().sleep(200);		    } catch (Exception e) {}		}		for (int i = 0; i < nrepeats; i++) {		    ic.download((short) -1);		}		ic.multicheck(nnodes);	    }else if (args[0].equals("multicheck")) {		if (args.length < 3) 		    usage();		if (args.length > 3)		    ic.nrepeats = Integer.parseInt(args[3]);		if (args.length > 4) 		    ic.nretries = Integer.parseInt(args[4]);		System.out.println("Will resend "+ic.nrepeats+" times, wait for" +				   " response "+ic.nretries+" packets");		ic.readCode(args[1]);		int nnodes = Integer.parseInt(args[2]);		ic.multicheck(nnodes);	    } else if (args[0].equals("download") ){		ic.reliableDownload((short)Integer.parseInt(args[1]), 				 Integer.parseInt(args[2]),				 Integer.parseInt(args[3]));	    } else {		System.out.println("Unknown command");		usage();	    }				    System.out.println("");				}	catch(Exception e){	    e.printStackTrace();	}    }    /* Sequence number is attached to each packet so that motes know whether they've       already forwarded a packet or not.              This is pretty seriously ugly as it involves a flush to disk on each call to IncSeqNo.    */  static int GetSeqNo() {    String s;    try {      File f = new File("seqno");      BufferedReader r= new BufferedReader(new InputStreamReader(new FileInputStream(f)));      s = r.readLine();      r.close();    } catch (IOException e ) {      e.printStackTrace();      return -1;    }    try {      return Integer.valueOf(s).intValue();    } catch (NumberFormatException e) {      e.printStackTrace();      return -1;    }  }  static int IncSeqNo() {    int newseqno = ++seqno;    if (newseqno > 32700) newseqno = 0;    try {      File f = new File("seqno");      BufferedWriter w = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f)));      w.write(new Integer(newseqno).toString());      w.write("\n");      w.flush();      w.close();    } catch (IOException e) {      e.printStackTrace();      return -1;    }    seqno = newseqno;  //hope this is atomic    return seqno;  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -