more changes
[IRC.git] / Robust / src / Benchmarks / Distributed / SpamFilter / SpamFilter.java
1 public class SpamFilter extends Thread {
2   DistributedHashMap mydhmap;
3   int id; //thread id
4   int numiter;
5   int numemail;
6   /**
7    * Total number of threads
8    **/
9   int nthreads;
10
11   public SpamFilter() {
12
13   }
14
15   public SpamFilter(int numiter, int numemail,int id) {
16     this.numiter=numiter;
17     this.numemail=numemail;
18     this.id = id;
19   }
20
21   public void run() {
22     int niter;
23     int nemails;
24     int thid;
25     atomic {
26       niter=numiter;
27       nemails=numemails;
28       thid = id;
29     }
30
31     Random rand = new Random(0);
32
33     for(int i=0; i<niter; i++) {
34       for(int j=0; j<nemails; j++) {
35         int pickemail = rand.nextInt(100);
36         //Mail email = getEmail(pickemail);
37         boolean filterAnswer = checkMail(email, thid);
38         boolean userAnswer = email.getIsSpam();
39         if(filterAnswer != userAnswer) {
40           sendFeedBack(email);
41         }
42       }
43     }
44   }
45
46   public static void main(String[] args) {
47     int nthreads;
48     int[] mid = new int[8];
49     mid[0] = (128<<24)|(195<<16)|(136<<8)|162; //dc-1.calit2
50     mid[1] = (128<<24)|(195<<16)|(136<<8)|163; //dc-2.calit2
51     mid[2] = (128<<24)|(195<<16)|(136<<8)|164; //dc-3.calit2
52     mid[3] = (128<<24)|(195<<16)|(136<<8)|165; //dc-4.calit2
53     mid[4] = (128<<24)|(195<<16)|(136<<8)|166; //dc-5.calit2
54     mid[5] = (128<<24)|(195<<16)|(136<<8)|167; //dc-6.calit2
55     mid[6] = (128<<24)|(195<<16)|(136<<8)|168; //dc-7.calit2
56     mid[7] = (128<<24)|(195<<16)|(136<<8)|169; //dc-8.calit2
57
58
59     /**
60      * Read options from command prompt
61      **/
62     SpamFilter sf = new SpamFilter();
63     SpamFilter.parseCmdLine(args, sf);
64
65     /**
66      * Create Global data structure 
67      **/
68     DistributedHashMap dhmap;
69     atomic {
70       dhmap = global new DistributedHashMap(500, 0.75f);
71     }
72     //3. N times iteration of work that needs to be done
73     //     by each client
74
75   }
76
77   public static void parseCmdLine(String args[], SpamFilter sf) {
78     int i = 0;
79     String arg;
80     while (i < args.length && args[i].startsWith("-")) {
81       arg = args[i++];
82       //check options
83       if(arg.equals("-n")) { //num of iterations
84         if(i < args.length) {
85           sf.numiter = new Integer(args[i++]).intValue();
86         }
87       } else if(arg.equals("-e")) { //num of emails
88         if(i < args.length) {
89           sf.numemail = new Integer(args[i++]).intValue();
90         }
91       } else if(arg.equals("-t")) { //num of threads
92         if(i < args.length) {
93           sf.threshold = new Integer(args[i++]).intValue();
94         }
95       } else if(arg.equals("-h")) {
96         sf.usage();
97       }
98     }
99     if(sf.nthreads == 0) {
100       sf.usage();
101     }
102   }
103
104   /**
105    * The usage routine describing the program
106    **/
107   public void usage() {
108     System.out.println("usage: ./spamfilter -n <num iterations> -e <num emails> -t <num threads>\n");
109     System.out.println(                   "  -n : num iterations");
110     System.out.println(                   "  -e : number of emails");
111     System.out.println(                   "  -t : number of threads");
112   }
113
114   /**
115    *  Returns result to the Spam filter
116    **/
117   public boolean checkMail(Mail mail, int userid) {
118     //Preprocess emails
119     //Vector partsOfMailStrings = createMailStrings(mail);
120
121     //Compute signatures
122     SignatureComputer sigComp = new SignatureComputer();
123     Vector signatures = sigComp.computeSigs(partsOfMailStrings);//vector of strings
124
125     //check with global data structure
126     int[] confidenceVals = check(signatures,userid);
127
128     //---- create and  return results --------
129     FilterResult filterResult = new FilterResult();
130     boolean spam = filterResult.getResult();
131
132     return spam;
133   } 
134
135   public int[] check(Vector emailParts, int userid) {
136     int numparts = emailParts.size();
137     int[] confidenceVals = new int[numparts];
138     for(int i=0; i<numparts; i++) {
139       String part = (String)(emailParts.elementAt(i));
140       char tmpengine = part.charAt(0);
141       String engine =  new String(tmpengine);
142       String signature = part.substring(2); //a:b index(a)=0, index(:)=1, index(b)=2
143       HashEntry myhe = new HashEntry();
144       myhe.setengine(engine);
145       myhe.setsig(signature);
146       //find object in distributedhashMap: if no object then add object 
147       //else read object
148       HashEntry tmphe= (HashEntry)(mydhmap.get(myhe));
149       if(tmphe == null) {
150         //add new object
151         myhe.stats = new HashStat();
152         myhe.stats.setuser(userid, 0, 0, -1);
153         FilterStatistic fs = new FilterStatistic(0,0,-1);
154         mydhmap.put(myhe, fs);
155       } else {
156         // ----- now connect to global data structure and ask query -----
157         confidenceVals[i] = tmphe.askForSpam(numparts);
158       }
159     }
160
161     //  --> the mail client is able to determine if it is spam or not
162     return confidenceVals;
163   }
164 }