001: /**
002:  * Program akisinin temelde saglanacagi siniftir.
003:  * Thread'leri aktif hale getirip, taramayi baslatir.
004:  * @author aatay EB
005:  * @version 1.4
006:  */
007: public class PingSweepStart extends Thread implements Constants {
008: 
009:     private String ip_range_beginning;
010:         private String ip_range_end;
011:         private int thread_count;
012:         private PingThread ping_threads[];
013:         private StringBuffer total_result = new StringBuffer();
014:         
015:     /**
016:      * Default Constructor. Baslangic ve bitis 
017:      * ip'lerine gore tarama islemine hazir
018:      * hale getirilir.
019:      * @param ip_range_beginning
020:      * @param ip_range_end
021:      */
022:     public PingSweepStart(String ip_range_beginnig,
023:                           String ip_range_end) {
024:         this.ip_range_beginning =
025:             BasicIPTools.find_lesser_address(
026:                     ip_range_beginnig,ip_range_end);
027:         this.ip_range_end =
028:             BasicIPTools.find_greater_address(
029:                     ip_range_beginnig,ip_range_end);
030:         thread_count =
031:             BasicIPTools.get_required_host_count(
032:                 this.ip_range_beginning,this.ip_range_end);
033:         calculate_required_thread();
034:         ping_threads = new PingThread[thread_count];
035:     }
036: 
037:     /**
038:      * Gerek thread sayisini bulup, thread_count'a 
039:      * aktarir.
040:      */
041:         private void calculate_required_thread()
042:         {
043:                 if( thread_count%PING_PER_THREAD!=0 ) {
044:                         thread_count /= PING_PER_THREAD;
045:                         thread_count++;
046:                 }
047:                 else
048:                         thread_count /= PING_PER_THREAD;
049:         }
050: 
051:         public static void main(String arg[]) throws InterruptedException {
052:           PingSweepStart a = new PingSweepStart("10.0.0.1","10.0.0.45");
053:         a.start();
054:         a.join();
055:           System.out.println("Bitti");
056:         PingSweepStart b = new PingSweepStart("10.0.0.46","10.0.0.75");
057:         b.start();
058:         b.join();
059:         //System.out.println(a.get_result_of_scan());
060:       }
061: 
062:     /**
063:      * 1 saniyede bir ana thread'in bitip bitmedigine karar verir.
064:      */
065:     public void run()
066:     {
067:         startSweeping();
068:         do {
069:             try {
070:                 sleep(1000);
071:             } catch (InterruptedException e) {
072:                 e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
073:             }
074:         } while(!check_if_sweep_completed());
075:         System.out.print(get_result_of_scan());
076:     }
077: 
078:     /**
079:      * Tarama sonucunda ortaya cikan IP'leri toplu olarak
080:      * gostermeye yarar.
081:      * @return - Canli olan IP'ler
082:      */
083:     public StringBuffer get_result_of_scan()
084:     {
085:         return total_result;
086:     }
087: 
088:     /**
089:      * Yaratilan butun thread'lerin bitip bitmedigin
090:      * bulur
091:      * @return true ise, threadler tamamlanmistir
092:      */
093:     private boolean check_if_sweep_completed()
094:     {
095:         for( int i=0;i<thread_count-1;i++ )
096:                 if( ping_threads[i].isAlive() ) return false;
097:         return true;
098:     }
099:     
100:     /**
101:      * Thread'ler yaratilip, ping atma islemi yaptirilir.
102:      */
103:     private void startSweeping() {
104:                 int i=0;
105:                 String start_ip,stop_ip;
106:                 start_ip = ip_range_beginning;
107:                 stop_ip = BasicIPTools.increase_IP(start_ip,PING_PER_THREAD);
108:                 while( i < thread_count ) {
109: 
110:                         if( !if_exceeds(start_ip,ip_range_end,PING_PER_THREAD ) ) {
111:                                 ping_threads[i] = new PingThread(start_ip,stop_ip,total_result);
112:                                 ping_threads[i].start();
113:                                 stop_ip = BasicIPTools.increase_IP(stop_ip);
114:                                 start_ip = stop_ip;
115:                                 stop_ip = BasicIPTools.increase_IP(stop_ip,PING_PER_THREAD);
116:                                 i++;
117:                         }
118:                         else {
119:                                 ping_threads[i] = new PingThread(start_ip,ip_range_end,total_result);
120:                   //System.out.println("Olmasi gereken: "+thread_count+" Olan :"+i);
121:                   ping_threads[i].start();
122:                                 break;
123:                         }
124:                 }
125:         }
126: 
127:     /**
128:      * Eger taranacak alani bir sonraki adimda asiliyorsa
129:      * buna gore sonlandirma islemi yapilmasi gerektigini
130:      * belirtir
131:      * @param start_ip - Baslangic IP
132:      * @param end_ip - Bitis IP
133:      * @param Step - Gidilecek adim sayisi
134:      * @return true ise, asilir. false asilmaz.
135:      */
136:         private boolean if_exceeds(String start_ip, String end_ip,
137:                         int Step)
138:         {
139:                 String after_addition =
140:                         BasicIPTools.increase_IP(start_ip,Step);
141:                 if( after_addition.equals(
142:                                 BasicIPTools.find_greater_address(
143:                                                 after_addition,end_ip)))
144:                         return true;
145:                 return false;
146:         }
147: }