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

📄 gatewaymap.java

📁 state strech routing protocl
💻 JAVA
字号:
package net.tinyos.cr;import java.util.*;import java.io.*;import net.tinyos.cr.GatewayMote;import net.tinyos.cr.messages.*;/* read the mapping file gateway_map between ordinary nodes and gateways   */public class GatewayMap {    //public HashMap map;    public Vector gatewayMotes;    public static final int MAX_MOTES = 50;    public GatewayMote [] motes; //the mapping between motes to gateway objects    public int n_totalMotes;    public int maxMoteId;    public GatewayMap(String filename) throws FileNotFoundException {        BufferedReader br;        String line;        //map = new HashMap();        n_totalMotes = 0;        maxMoteId = 0;        motes = new GatewayMote[MAX_MOTES];                br = new BufferedReader( new FileReader(filename) );        try {            while ((line = br.readLine()) != null) {                if (line.charAt(0)=='#')                    continue;                StringTokenizer st = new StringTokenizer(line);                if(st.countTokens()==0) continue;                String firstToken = st.nextToken();                if (firstToken.compareTo("gateway_num") == 0) {                    if (st.countTokens() < 1){                        throw new Exception("Error parsing line: too few arguments for gateway_num" + line);                    }                    int gateway_num = Integer.parseInt(st.nextToken());                    gatewayMotes = new Vector();                    for (int i=1;i<=gateway_num;i++) {                        GatewayMote m = new GatewayMote(i, "cmd"+Integer.toString(i));                        gatewayMotes.add(m);                    }                }/*                else if (firstToken.compareTo("motes_num") == 0) {                    if (st.countTokens() < 1){                        throw new Exception("Error parsing line: too few arguments for gateway_num" + line);                    }                    int motes_num = Integer.parseInt(st.nextToken());                    motes = new GatewayMote[motes_num];                    }*/                else if (firstToken.compareTo("mote") == 0) {                    //format is mote gateway_id mote_id                    int gateway_id;                    int id;                    if (st.countTokens() < 2){                        throw new Exception("Error parsing line: too few arguments" + line);                    }                    gateway_id = Integer.parseInt(st.nextToken());                    id = Integer.parseInt(st.nextToken());                    //map.put(id, gatewayMotes.elementAt(gateway_id-1));                    motes[id-1] = (GatewayMote) gatewayMotes.elementAt(gateway_id-1);                    n_totalMotes ++;                    if (maxMoteId<id) maxMoteId = id;                }                else {                    System.out.println("Ignoring line:"+line);                }            }        } catch (Exception e) {            e.printStackTrace();            System.err.println(e);            System.exit(0);        }        System.out.println("max "+maxMoteId+" total "+ n_totalMotes);    }    public int getRandomMoteID() {        /* get one from all existing motes*/        int relative_id = 1+(int)Math.floor( Math.random()*n_totalMotes);        int i, count = 0;        for (i=0;i<maxMoteId;i++){            if (motes[i]!=null) count++;            if (count==relative_id) return i+1;        }        System.err.println("can't find ID"+relative_id);        return 0;    }    public S4CommandResponseMessage runCommand(String argv, boolean printMsg) {        return runCommand(argv.split("\\s"),printMsg);    }    public S4CommandResponseMessage runCommand(int mote_id, String        argv, boolean printMsg) {        String s = Integer.toString(mote_id) + " " + argv;        return runCommand(s.split("\\s"),printMsg);    }    public short [] getCoords(int mote_id){                S4CommandResponseMessage bcrm = runCommand(mote_id, "get_coords", true);        short [] c = null;        if (bcrm!=null)            c = bcrm.get_type_data_data_args_coords_comps();        return c;    }    public S4CommandResponseMessage runCommand(String [] argv, boolean printMsg) {        /* may run the command on one or all nodes, only return the last node's response.           may change the return value to an array in the future*/        int first_id = 0, last_id = 0;        S4CommandResponseMessage ret = null;        boolean run_on_all = false;        if (argv[0].equals("all")) {            first_id = 1;            last_id = maxMoteId;            run_on_all = true;        }else {            int mote_id = Integer.parseInt(argv[0]);            first_id = mote_id;            last_id = mote_id;        }        for (int i=first_id; i<=last_id;i++) {            GatewayMote gm = motes[i-1];            if (gm!=null) {                argv[0] = Integer.toString(i);                if ((run_on_all) && (printMsg))                    System.out.println("on mote "+i);                                ret = gm.runCommand(argv, printMsg);            }else {                if (!run_on_all) {                    System.err.println("cant find in gateway mapping: "+i);                    ret = null;                }            }        }        return ret;    }    public void runCommands(Reader r, boolean printMsg) {        BufferedReader br;        String line;        //map = new HashMap();                br = new BufferedReader( r );        try {            while ((line = br.readLine()) != null) {                if (line.charAt(0)=='#')                    continue;                //System.out.println(line);                runCommand(line, printMsg);                            }        } catch (Exception e) {            e.printStackTrace();            System.err.println(e);            System.exit(0);        }            }    public void runCommandsFromStdin(boolean printMsg){        runCommands(new InputStreamReader(System.in), printMsg);    }    public void runCommandsFromFile(String filename, boolean printMsg){        try{            runCommands(new FileReader(filename), printMsg);        }catch (java.io.FileNotFoundException e){            e.printStackTrace();            System.err.println(e);            System.exit(0);        }    }    public static void main(String[] argv) throws IOException{        GatewayMap m= new GatewayMap(argv[0]);        /*        m.runCommand("1 get_coords", true);        m.runCommand("5 get_coords", true);        m.runCommand("all get_coords", true);        */        m.runCommandsFromStdin(true);        System.exit(0);    }    }

⌨️ 快捷键说明

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