1 46 47 48 52 53 54 package sample.blocks.scalable.webmonitor; 55 56 import java.net.URL ; 57 import java.util.HashMap ; 58 import java.util.Iterator ; 59 60 import org.mr.api.blocks.ScalableDispatcher; 61 import org.mr.api.blocks.ScalableFactory; 62 import org.mr.api.blocks.ScalableHandler; 63 import org.mr.api.blocks.ScalableStage; 64 65 69 public class WebMonitorEngine extends Thread implements ScalableHandler { 70 73 public ScalableStage engineStage; 74 77 public ScalableDispatcher guiDispatcher; 78 81 public HashMap urlsToStatus = new HashMap (); 82 83 88 public WebMonitorEngine(boolean distributed){ 89 engineStage = ScalableFactory.getStage("engine", distributed); 91 engineStage.addHandler(this); 92 guiDispatcher = ScalableFactory.getDispatcher("gui", distributed); 94 } 95 96 101 public void handle(Object event) { 102 synchronized(urlsToStatus){ 104 String urlStr = (String ) event; 105 if(!urlStr.startsWith("HTTP://")&& !urlStr.startsWith("http://")){ 106 urlStr = "http://"+urlStr; 107 } 108 urlsToStatus.put(urlStr, "Checking"); 110 guiDispatcher.dispatch(urlsToStatus); 111 } 112 113 } 114 115 118 public void run(){ 119 while(true){ 120 try { 122 sleep(1500); 123 } catch (InterruptedException e1) { 124 e1.printStackTrace(); 125 } 126 HashMap result = new HashMap (); 127 synchronized(urlsToStatus){ 129 Iterator urlsToCheck =urlsToStatus.keySet().iterator(); 130 while(urlsToCheck.hasNext()){ 131 String urlStr =(String ) urlsToCheck.next(); 132 133 String status = ""; 134 try { 135 URL url = new URL (urlStr); 136 url.openStream().read(); 137 status= "OK :)"; 138 } catch (Exception e) { 139 status = "Not responding"; 140 } 141 if(!urlsToStatus.get(urlStr).equals(status)){ 143 result.put(urlStr, status); 144 urlsToStatus.put(urlStr, status); 145 } 146 147 } if(!result.isEmpty()) 150 guiDispatcher.dispatch(urlsToStatus); 151 } } 154 155 } 156 157 158 public static void main(String [] args) throws Exception { 159 WebMonitorEngine engine = new WebMonitorEngine(true); 161 engine.start(); 162 163 } 164 165 } 166 | Popular Tags |