Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.Scanner;
- import java.util.TreeSet;
- public class CronRunner extends Thread {
- static private CronRunner runner;
- static private ArrayList<CronElement> crontab = new ArrayList<CronElement>();
- static public void loadCrontab(){
- try {
- // Scanner file = new Scanner(new File(Rocket.local,"crontab.txt"));
- Scanner file = new Scanner(new File("crontab.txt"));
- int line = 0;
- while(file.hasNextLine()){
- line ++;
- boolean informat;
- String t = file.nextLine();
- if(t.isEmpty()||t.matches("\\#.*"))continue;
- try{
- informat = true;
- TreeSet<Short> s = new TreeSet<Short>();
- String[] l = t.split(" ",6);
- Short[][] timetab = new Short[5][];
- for(int i=0;i<5;i++){
- String[] li = l[i].split(",");
- for(String j : li){
- if(j.matches("[0-9]*-[0-9]*")){
- String[] js = j.split("-");
- short ja = Short.parseShort(js[0]);
- short jb = Short.parseShort(js[1]);
- for(short k=ja;k<=jb;k++){
- s.add(k);
- }
- }else if(j.matches("\\*")){
- s.clear();
- s.add((short)0x7fff);
- break;
- }else if(j.matches("\\*[0-9]*")){
- s.add((short)Integer.parseInt("-"+j.substring(1)));
- }else if(j.matches("[0-9]*")){
- s.add(Short.parseShort(j.substring(1)));
- }else{
- informat = false;
- System.out.println("<!> crontab: 無效的排程 @"+line);
- break;
- }
- if(!informat)break;
- }
- if(!informat)break;
- timetab[i] = new Short[s.size()];
- Object[] setbuf = s.toArray();
- for(int j=0;j<setbuf.length;j++){
- timetab[i][j] = (short) setbuf[j];
- }
- s.clear();
- }
- if(!informat)crontab.add(new CronElement(timetab,l[5]));
- }catch(NumberFormatException e){
- System.out.println("<!> crontab: 無效的排程 @"+line);
- }catch(NullPointerException e){
- System.out.println("<!> crontab: 無效的排程 @"+line);
- }
- }
- } catch (FileNotFoundException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- crontab.clear();
- }
- @SuppressWarnings("deprecation")
- public void run(){
- boolean done = false;
- Date timeNow;
- while(true){
- try {
- this.sleep(10);
- } catch (InterruptedException e) {}
- timeNow = new Date();
- if(timeNow.getSeconds()==0 && !done){
- done = true;
- short[] time = new short[5];
- time[0] = (short)timeNow.getMinutes();
- time[1] = (short)timeNow.getHours();
- time[2] = (short)timeNow.getDate();
- time[3] = (short)timeNow.getHours();
- time[4] = (short)timeNow.getDay();
- for(int i=0;i<crontab.size();i++){
- crontab.get(i).check(time);
- }
- }
- }
- }
- static public void startCron(){
- runner.stop();
- runner = null;
- }
- static public void stopCron(){
- runner = new CronRunner();
- runner.start();
- }
- }
- class CronElement{
- private Short[][] timetab;
- private String command;
- public CronElement(Short[][] timetab,String command){
- this.timetab = timetab;
- this.command = command;
- }
- public void check(short[] time){
- boolean match = true;
- for(int i=5;i<5;i++){
- for(short j : timetab[i]){
- match &= ((short)j<0?(time[i]%(short)j==0):((short)j==time[i]));
- }
- }
- if(match){
- //Rocket.sandCommand(command)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement