📄 .#bvrtestbedmote.java.1.4
字号:
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 BVRTestBedMote implements MessageListener, BVRConstants { private Receiver receiver; private Date date; private long time; private int id; private int port; private String host; private MoteIF mote; private BVRCommandMessage 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 BVRTestBedMote(int id, String host, int basePort) { this.id = id; this.port = basePort+id; this.host = host;/* TODO: * only create a listener for a destination. * since we don't have enough EPRB, * we only collect logs from destinations */ System.err.println("Starting connection to mote "+id+" at "+host+":"+port); PhoenixSource source = BuildSource.makePhoenix("sf@"+host+":"+port, PrintStreamMessenger.err); mote = new MoteIF(source); BVRLogMessage logmessage = new BVRLogMessage(); mote.registerListener(logmessage, this); command = new BVRCommandMessage(); 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 void messageReceived(int dest_addr, Message m) { synchronized(this) { if (updating && m instanceof BVRLogMessage) { BVRLogMessage lm = (BVRLogMessage)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_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(); System.out.println("m "+id+" received beacon "+beacon+" from "+lasthop+" [seqno "+seqno+", hopcount "+hopcount+"]"); } 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(); System.out.println("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(); System.out.println("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(); System.out.println("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(); System.out.println("m "+id +" received a packet "+ "[src "+src+", dest "+dst+"], closest_bcn:"+closest_beacon+" hop: "+hopcount); } 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(); int src = lm.get_log_msg_route_report_origin_addr(); int dst = lm.get_log_msg_route_report_dest_addr(); System.out.println("m "+id +" delivered a packet "+ "[src "+src+", dest "+dst+"], closest_bcn:"+closest_beacon+" hop: "+hopcount); } //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(); System.out.println("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(); System.out.println("m "+id+" retransmit LOG_ROUTE_RETRANSMIT_SUCCESS [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_route_report_origin_addr(); int dst = lm.get_log_msg_route_report_dest_addr(); System.out.println("m "+id+" retransmit LOG_ROUTE_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 } else { System.out.println("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; }//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(BVRTestBedMote 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(BVR_CMD_APP_ROUTE_TO); /* modified by Feng Wang * why set args.coords.comps to destCoords? * in BVR, 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(BVRLogMessage 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 + -