📄 s4testbedmote.java
字号:
package net.tinyos.cr;import net.tinyos.util.*;import java.io.*;import java.util.*;import net.tinyos.message.*;import net.tinyos.packet.*;import net.tinyos.util.*;import net.tinyos.cr.messages.*;public class S4TestBedMote extends Mote implements MessageListener, S4Constants { private Date date; private long time; private int id; private int port; private MoteIF mote; private S4CommandMessage command; private short sequenceNo = 0; private int updateCount = 0; private short coords[]; private boolean updating = true; /* This creates a new mote listener. It assumes that a mote with * programmed id <i> is connected to a serial forwarder at port * basePort + i, at host host. */ public S4TestBedMote(int id) { this.id = id; this.port = baseport+id; System.err.println("Starting connection to mote "+id+" at "+host+":"+port); PhoenixSource source = BuildSource.makePhoenix("sf@"+host+":"+port, PrintStreamMessenger.err); mote = new MoteIF(source); S4LogMessage logmessage = new S4LogMessage(); mote.registerListener(logmessage, this); command = new S4CommandMessage(); command.set_header_last_hop(TOS_UART_ADDR); command.set_type_data_hopcount((short)1); //command.set_type_data_origin(TOS_UART_ADDR); command.set_type_data_cmd_addr(id); command.set_type_data_gateway_addr(id); command.set_type_data_data_flags((short)0); } public static void log(String s){ System.out.println((new Date()).getTime() + ":"+s); } public void messageReceived(int dest_addr, Message m) { synchronized(this) { if (updating && m instanceof S4LogMessage) { S4LogMessage lm = (S4LogMessage)m; updateCount++; short type = lm.get_log_msg_type(); if (type == LOG_CHANGE_COORDS) { //System.out.println("coordinates changed"); updateCoordinates(lm); } //added by Feng Wang to get route report else if (type == LOG_RECEIVE_BEACON) { int source = lm.get_log_msg_receive_beacon_source(); short seqno = lm.get_log_msg_receive_beacon_seqno(); short hopcount = lm.get_log_msg_receive_beacon_hopcount(); int lasthop = lm.get_log_msg_receive_beacon_last_hop(); //log("m "+id+" received a DV from "+source+" [seqno "+seqno+", hopcount "+hopcount+", lasthop "+lasthop+"]"); } else if (type == LOG_RECEIVE_ROOT_BEACON) { short beacon = lm.get_log_msg_receive_root_beacon_id(); short seqno = lm.get_log_msg_receive_root_beacon_seqno(); short hopcount = lm.get_log_msg_receive_root_beacon_hopcount(); int lasthop = lm.get_log_msg_receive_root_beacon_last_hop(); //log("m "+id+" received a beacon from "+beacon+" [seqno "+seqno+", hopcount "+hopcount+", lasthop "+lasthop+"]"); } else if (type == LOG_ROUTE_START) { short closest_beacon = lm.get_log_msg_route_report_closest_beacon(); short hopcount = lm.get_log_msg_route_report_hopcount(); int src = lm.get_log_msg_route_report_origin_addr(); int dst = lm.get_log_msg_route_report_dest_addr(); log("m "+id +" start routing "+ "[src "+src+", dest "+dst+"], closest_bcn:"+closest_beacon+" hop: "+hopcount); } else if (type == LOG_ROUTE_SENT_NORMAL_OK) { int src = lm.get_log_msg_route_report_origin_addr(); int dst = lm.get_log_msg_route_report_dest_addr(); log("m "+id+" sent a packet [src "+src+", dest "+dst+"]"); } else if (type == LOG_ROUTE_FAIL_STUCK) { int src = lm.get_log_msg_route_report_origin_addr(); int dst = lm.get_log_msg_route_report_dest_addr(); log("m "+id+" !!!!!!routing failure [src "+src+", dest "+dst+"]"); } else if (type == LOG_ROUTE_RECEIVED_OK) { short closest_beacon = lm.get_log_msg_route_report_closest_beacon(); short hopcount = lm.get_log_msg_route_report_hopcount(); int src = lm.get_log_msg_route_report_origin_addr(); int dst = lm.get_log_msg_route_report_dest_addr(); //added by Feng Wang on Sept. 22, to log # of retransmissions short rexmit_count = lm.get_log_msg_route_report_rexmit_count(); short tried_hopcount = lm.get_log_msg_route_report_tried_hopcount(); log("m "+id +" received a packet "+ "[src "+src+", dest "+dst+"], closest_bcn:"+closest_beacon+" hop: "+hopcount+" (tried: "+tried_hopcount+") retx: "+rexmit_count); } //added by Feng Wang on Sept. 26, to log duplicate receptions else if (type == LOG_ROUTE_RECEIVED_DUPLICATE) { short closest_beacon = lm.get_log_msg_route_report_closest_beacon(); short hopcount = lm.get_log_msg_route_report_hopcount(); int src = lm.get_log_msg_route_report_origin_addr(); int dst = lm.get_log_msg_route_report_dest_addr(); //added by Feng Wang on Sept. 22, to log # of retransmissions short rexmit_count = lm.get_log_msg_route_report_rexmit_count(); short tried_hopcount = lm.get_log_msg_route_report_tried_hopcount(); log("m "+id +" received a duplicate packet "+ "[src "+src+", dest "+dst+"], closest_bcn:"+closest_beacon+" hop: "+hopcount+" (tried: "+tried_hopcount+") retx: "+rexmit_count); } else if (type == LOG_ROUTE_INVALID_STATUS) { short closest_beacon = lm.get_log_msg_route_report_closest_beacon(); short hopcount = lm.get_log_msg_route_report_hopcount(); int src = lm.get_log_msg_route_report_origin_addr(); int dst = lm.get_log_msg_route_report_dest_addr(); //added by Feng Wang on Sept. 22, to log # of retransmissions short rexmit_count = lm.get_log_msg_route_report_rexmit_count(); short tried_hopcount = lm.get_log_msg_route_report_tried_hopcount(); log("m "+id +" invalid status "+ "[src "+src+", dest "+dst+"], closest_bcn:"+closest_beacon+" hop: "+hopcount+" (tried: "+tried_hopcount+") retx: "+rexmit_count); } else if (type == LOG_ROUTE_SUCCESS) { short closest_beacon = lm.get_log_msg_route_report_closest_beacon(); short hopcount = lm.get_log_msg_route_report_hopcount(); //added by Feng Wang on Sept. 22, to log # of retransmissions short rexmit_count = lm.get_log_msg_route_report_rexmit_count(); short tried_hopcount = lm.get_log_msg_route_report_tried_hopcount(); int src = lm.get_log_msg_route_report_origin_addr(); int dst = lm.get_log_msg_route_report_dest_addr(); log("m "+id +" **********************delivered a packet:::"+ "src:"+src+",dest:"+dst+",closest_bcn:"+closest_beacon+", hop:"+hopcount+",tried:"+tried_hopcount+",retx: "+rexmit_count); } //maoy: some unhandled log cases else if (type == LOG_ROUTE_FAIL_STUCK_0) { short closest_beacon = lm.get_log_msg_route_report_closest_beacon(); short hopcount = lm.get_log_msg_route_report_hopcount(); int src = lm.get_log_msg_route_report_origin_addr(); int dst = lm.get_log_msg_route_report_dest_addr(); log("m "+id +" !!!!!!routing failure STUCK_0 "+ "[src "+src+", dest "+dst+"], closest_bcn:"+closest_beacon+" hop: "+hopcount); } else if (type == LOG_ROUTE_RETRANSMIT_SUCCESS) { int src = lm.get_log_msg_retransmit_report_origin_addr(); int dst = lm.get_log_msg_retransmit_report_dest_addr(); int hc = lm.get_log_msg_retransmit_report_hopcount(); int nh = lm.get_log_msg_retransmit_report_next_hop(); int retx = lm.get_log_msg_retransmit_report_retransmit_count(); if (retx == 0) { //first transmission log("m "+id+" transmit transmit_success(ACKed) [src "+src +", dest "+dst+"]" + " retx_count:"+retx + " hopcount:"+hc + " next_hop:"+nh); } else { log("m "+id+" retransmit retransmit_success(ACKed) [src "+src +", dest "+dst+"]" + " retx_count:"+retx + " hopcount:"+hc + " next_hop:"+nh); } } //added by Feng Wang on Sept. 26 else if (type == LOG_ROUTE_TRANSMIT_TRIAL ) { int src = lm.get_log_msg_retransmit_report_origin_addr(); int dst = lm.get_log_msg_retransmit_report_dest_addr(); log("m "+id+" transmit_trial(not ACKed) [src "+src +", dest "+dst+"]" + " retx_count:"+lm.get_log_msg_retransmit_report_retransmit_count() + " hopcount:"+lm.get_log_msg_retransmit_report_hopcount() + " next_hop:" + lm.get_log_msg_retransmit_report_next_hop()); } else if (type == LOG_ROUTE_RETRANSMIT_FAIL ) { int src = lm.get_log_msg_retransmit_report_origin_addr(); int dst = lm.get_log_msg_retransmit_report_dest_addr(); log("m "+id+" retransmit retransmit_fail [src "+src +", dest "+dst+"]" + " retx_count:"+lm.get_log_msg_retransmit_report_retransmit_count() + " hopcount:"+lm.get_log_msg_retransmit_report_hopcount() + " next_hop:" + lm.get_log_msg_retransmit_report_next_hop()); } else if (type == LOG_CHANGE_COORD){ //output nothing } //added by Feng Wang on Sept. 22, for more statistics logging else if (type == LOG_SENT_DV) { int total = lm.get_log_msg_sent_dv_total(); log("m "+id+" sent totally "+total+" distance vector packets]"); } else if (type == LOG_SENT_BV) { int total = lm.get_log_msg_sent_bv_total(); log("m "+id+" sent totally "+total+" beacon vector packets]"); } else if (type == LOG_SENT_DATA) { int total = lm.get_log_msg_sent_data_total(); log("m "+id+" sent totally "+total+" data packets]"); } else if (type == LOG_RT_STATE) { int total = lm.get_log_msg_rt_state_total(); log("m "+id+" used totally "+total+" bytes of memory]"); } else { log("m "+id+" unknown type "+ type); } } } } public void stopUpdating() { updating = false; } public void startUpdating() { updating = true; } public boolean isUpdating() { return updating; } public int getId() { return id; } public int getUpdateCount() { return updateCount; } public short[] getCoords() { return coords; } public void setCoords(short [] c) { coords = c; } //maoy: get the closest beacon of myself public short getClosestBeacon() { return getClosestBeacon(getCoords()); }//added by Feng Wang to get the closest beacon public short getClosestBeacon(short[] dest_coords) { short beacon = 255; //invalid becon id for (short i = 0; i < dest_coords.length; i++) { if (dest_coords[i] != 255) { if (beacon == 255 || dest_coords[beacon] > dest_coords[i]) { beacon = i; } } } return beacon; }//end of added code public short countValid() { short count = 0; if (coords != null) { for (int i = 0; i < coords.length; i++) { if (coords[i] != 255) { count++; } } } return count; } public void sendRouteCommand(S4TestBedMote dest) { System.out.print("Sending route command from " + id + " to " + dest.getId() + " ["); short[] destCoords = dest.getCoords(); System.out.print(destCoords[0]); for (int i = 1; i < destCoords.length; i++) { System.out.print("," + destCoords[i]); } System.out.println("]"); sequenceNo++; command.set_header_seqno(sequenceNo); command.set_type_data_data_seqno(sequenceNo); command.set_type_data_type(S4_CMD_APP_ROUTE_TO); /* modified by Feng Wang * why set args.coords.comps to destCoords? * in S4, shouldn't we set args.dest.coords.comps to destCoords instead? * in CR, we need to set the closest beacon */ //command.set_type_data_data_args_coords_comps(dest.getCoords()); command.set_type_data_data_args_dest_closest_beacon(getClosestBeacon(destCoords)); command.set_type_data_data_args_dest_addr(dest.getId()); //command.set_type_data_data_args_dest_mode((byte)2); sendCommand(); } private void sendCommand() { System.out.print("Sending payload: "); for (int i = 0; i < command.dataLength(); i++) { System.out.print(Integer.toHexString(command.dataGet()[i] & 0xff)+ " "); } System.out.println(); try { mote.send(id, command); } catch (IOException e) {e.printStackTrace(System.err);} } private void updateCoordinates(S4LogMessage m) { this.coords = m.get_log_msg_update_coordinates_Coords_comps(); System.out.print("Update coordinates: "); this.printCoordinates(); } public void printCoordinates() { System.out.print("m "+id+": ["); if (coords != null) { System.out.print(coords[0]); for (int i = 1; i < coords.length; i++) { System.out.print("," + coords[i]); } } System.out.println("]"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -