1 public class SpamFilter extends Thread {
2 DistributedHashMap mydhmap;
7 * Total number of threads
15 public SpamFilter(int numiter, int numemail,int id) {
17 this.numemail=numemail;
31 Random rand = new Random(0);
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) {
46 public static void main(String[] args) {
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
60 * Read options from command prompt
62 SpamFilter sf = new SpamFilter();
63 SpamFilter.parseCmdLine(args, sf);
66 * Create Global data structure
68 DistributedHashMap dhmap;
70 dhmap = global new DistributedHashMap(500, 0.75f);
72 //3. N times iteration of work that needs to be done
77 public static void parseCmdLine(String args[], SpamFilter sf) {
80 while (i < args.length && args[i].startsWith("-")) {
83 if(arg.equals("-n")) { //num of iterations
85 sf.numiter = new Integer(args[i++]).intValue();
87 } else if(arg.equals("-e")) { //num of emails
89 sf.numemail = new Integer(args[i++]).intValue();
91 } else if(arg.equals("-t")) { //num of threads
93 sf.threshold = new Integer(args[i++]).intValue();
95 } else if(arg.equals("-h")) {
99 if(sf.nthreads == 0) {
105 * The usage routine describing the program
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");
115 * Returns result to the Spam filter
117 public boolean checkMail(Mail mail, int userid) {
119 //Vector partsOfMailStrings = createMailStrings(mail);
122 SignatureComputer sigComp = new SignatureComputer();
123 Vector signatures = sigComp.computeSigs(partsOfMailStrings);//vector of strings
125 //check with global data structure
126 int[] confidenceVals = check(signatures,userid);
128 //---- create and return results --------
129 FilterResult filterResult = new FilterResult();
130 boolean spam = filterResult.getResult();
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
148 HashEntry tmphe= (HashEntry)(mydhmap.get(myhe));
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);
156 // ----- now connect to global data structure and ask query -----
157 confidenceVals[i] = tmphe.askForSpam(numparts);
161 // --> the mail client is able to determine if it is spam or not
162 return confidenceVals;