/** * Jeff Rupp * Master's Thesis * 2005 * * various debug functions */ package jdr.mobisim; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import java.util.*; import org.apache.log4j.*; import jdr.utils.*; public class DebugDialog extends JDialog { private static final boolean DO_EXCESSIVE_LOGS = false; private static Logger m_logger = Logger.getLogger(DebugDialog.class); private JButton m_sinkSendFormClustersButton = new JButton("Form Clusters Now"); private JButton m_sinkSendStartNormalCommButton = new JButton("Start Normal Comm Now"); private JButton m_dumpNodeHeardButton = new JButton("Dump Nodes Heard"); private JButton m_dumpNodeQueueButton = new JButton("Dump Node Queue"); private JButton m_dumpClusterInfoButton = new JButton("Dump Cluster Info"); private JButton m_dumpSchedulerStatusButton = new JButton("Dump Scheduler Status"); private FloatPoint m_sinkLocation = new FloatPoint(0, 0); public DebugDialog(JFrame parent) { super(parent, "Jeff Rupp 2005 Thesis, Debug functions"); initGui(); setSize(200, 300); setLocationRelativeTo(null); } /** * adds the GUI elements */ private void initGui() { JPanel mainPnl = new JPanel(new VFlowLayout()); mainPnl.add(m_sinkSendFormClustersButton); mainPnl.add(m_sinkSendStartNormalCommButton); mainPnl.add(m_dumpNodeHeardButton); mainPnl.add(m_dumpNodeQueueButton); mainPnl.add(m_dumpClusterInfoButton); mainPnl.add(m_dumpSchedulerStatusButton); getContentPane().add(mainPnl, BorderLayout.CENTER); ConnectGuiEventHandlers(); } private void ConnectGuiEventHandlers() { m_dumpNodeHeardButton.addActionListener(new ActionListener() { public void actionPerformed(java.awt.event.ActionEvent A) { DumpNodesNear(); } }); m_dumpNodeQueueButton.addActionListener(new ActionListener() { public void actionPerformed(java.awt.event.ActionEvent A) { DumpNodeQueue(); } }); m_sinkSendFormClustersButton.addActionListener(new ActionListener() { public void actionPerformed(java.awt.event.ActionEvent A) { FormClustersNow(); } }); m_sinkSendStartNormalCommButton.addActionListener(new ActionListener() { public void actionPerformed(java.awt.event.ActionEvent A) { BeginNormalCommNow(); } }); m_dumpClusterInfoButton.addActionListener(new ActionListener() { public void actionPerformed(java.awt.event.ActionEvent A) { DumpClusterInfo(); } }); m_dumpSchedulerStatusButton.addActionListener(new ActionListener() { public void actionPerformed(java.awt.event.ActionEvent A) { DumpScheduler(); } }); } /** * print list of which nodes all the nodes heard at what power levels */ private void DumpNodesNear() { AllNodes.Instance().DumpAllNodesHeard(); } /** * print list of which nodes all the nodes heard at what power levels */ private void DumpNodeQueue() { AllNodes.Instance().DumpAllNodesQueue(); } /** * print a list of the cluster head / member for each node */ private void DumpClusterInfo() { AllNodes.Instance().DumpAllNodesClusterInfo(); } /** * print list of which nodes all the nodes heard at what power levels */ private void DumpScheduler() { Scheduler.getInstance().DumpStatus(); } public void setSinkNodeLocation(FloatPoint loc) { m_sinkLocation = loc; } /** * debug method to initiate the choosing of cluster heads * picks the first node near to point 0,0 by searching iteratively for a node at * ever increasing distance, this way the closest node found first is used * Also called automatically after the clusters have formed, based on the Scheduler * queue being empty for a sufficient time */ public void FormClustersNow() { m_logger.debug("Forming clusters", new Throwable()); MobileSimulator.Instance().GetTopographyPanel().SetSinkLocation(m_sinkLocation); PacketIF packet = PacketIF.getNewInstance(); if(packet != null) { byte [] data = new byte[MobilePacket.BEGIN_GENERIC_DATA_BYTE_OFFSET + PacketIF.CRC_SIZE_BYTES]; packet.setData(data); packet.insertInt(data, MobilePacket.SOURCE_NODE_BYTE_OFFSET, -1); packet.insertInt(data, MobilePacket.DESTINATION_NODE_BYTE_OFFSET, -1); // no particular destination, anybody who hears packet.insertInt(data, MobilePacket.LAST_NODE_BYTE_OFFSET, -1); packet.insertInt(data, MobilePacket.NEXT_NODE_BYTE_OFFSET, -1); packet.insertInt(data, MobilePacket.MESSAGE_NUMBER_BYTE_OFFSET, packet.GetNextMessageNumber()); data[MobilePacket.MESSAGE_TYPE_BYTE_OFFSET] = MobilePacket.CHOOSE_HEADS; data[MobilePacket.MESSAGE_LENGTH_BYTE_OFFSET] = 0; boolean nodeNotFound = true; double distance = 1.0; while(nodeNotFound) { HashMap nodesInRange = AllNodes.Instance().GetNodesNear(m_sinkLocation, distance, DO_EXCESSIVE_LOGS); Iterator iter = nodesInRange.keySet().iterator(); if(iter.hasNext()) { NodeIF node = (NodeIF)(nodesInRange.get(iter.next())); node.ReceivePacket(packet); m_logger.debug("Sent 'form cluster' message, type: "+ data[MobilePacket.MESSAGE_TYPE_BYTE_OFFSET]); nodeNotFound = false; } else { distance += 1.0; } } if(nodeNotFound) { m_logger.error("no nodes near "+m_sinkLocation.getX()+", "+m_sinkLocation.getY()+ " to start the cluster formation"); } } } /** * debug method to tell nodes to begin their normal comm (periodic data transmits) */ public void BeginNormalCommNow() { m_logger.debug("Beginning normal communications"); MobileSimulator.Instance().GetTopographyPanel().SetSinkLocation(m_sinkLocation); PacketIF packet = PacketIF.getNewInstance(); if(packet != null) { byte [] data = new byte[MobilePacket.BEGIN_GENERIC_DATA_BYTE_OFFSET + PacketIF.CRC_SIZE_BYTES]; packet.setData(data); int seqNum = PacketIF.GetNextSequenceNumber(); packet.insertInt(data, MobilePacket.SEQUENCE_NUMBER_BYTE_OFFSET, seqNum); packet.insertInt(data, MobilePacket.SOURCE_NODE_BYTE_OFFSET, -1); packet.insertInt(data, MobilePacket.DESTINATION_NODE_BYTE_OFFSET, -1); // no particular destination, anybody who hears packet.insertInt(data, MobilePacket.LAST_NODE_BYTE_OFFSET, -1); packet.insertInt(data, MobilePacket.NEXT_NODE_BYTE_OFFSET, -1); packet.insertInt(data, MobilePacket.MESSAGE_NUMBER_BYTE_OFFSET, packet.GetNextMessageNumber()); data[MobilePacket.MESSAGE_TYPE_BYTE_OFFSET] = MobilePacket.OK_TO_DO_NORMAL_COMM; data[MobilePacket.MESSAGE_LENGTH_BYTE_OFFSET] = 0; boolean nodeNotFound = true; double distance = 1.0; while(nodeNotFound) { HashMap nodesInRange = AllNodes.Instance().GetNodesNear(m_sinkLocation, distance, DO_EXCESSIVE_LOGS); Iterator iter = nodesInRange.keySet().iterator(); if(iter.hasNext()) { NodeIF node = (NodeIF)(nodesInRange.get(iter.next())); node.ReceivePacket(packet); m_logger.debug("Sent 'begin normal comm' message, type: "+ data[MobilePacket.MESSAGE_TYPE_BYTE_OFFSET]); nodeNotFound = false; } else { distance += 1.0; } } if(nodeNotFound) { m_logger.error("no nodes near "+m_sinkLocation.getX()+", "+m_sinkLocation.getY()+ " to start the cluster formation"); } } } }