public class EphemeralSignature {
- private int serverSeed;
- private String serverSeparator;
+ int serverSeed;
+ String serverSeparator;
Random rand;
public EphemeralSignature() {
--- /dev/null
+public class GString {
+ char value[];
+ int count;
+ int offset;
+
+ public GString() {
+ }
+
+ public GString(char c) {
+ char[] str = global new char[1];
+ str[0] = c;
+ GString(str);
+ }
+
+ public GString(String str) {
+ value = global new char[str.count];
+ for(int i =0; i< str.count;i++) {
+ value[i] = str.value[i+str.offset];
+ }
+ count = str.count;
+ offset = 0;
+ }
+
+ public GString(GString gstr) {
+ this.value = gstr.value;
+ this.count = gstr.count;
+ this.offset = gstr.offset;
+ }
+
+ public GString(StringBuffer gsb) {
+ value = global new char[gsb.length()];
+ count = gsb.length();
+ offset = 0;
+ for (int i = 0; i < count; i++)
+ value[i] = gsb.value[i];
+ }
+
+ public GString(char str[]) {
+ char charstr[]=new char[str.length];
+ for(int i=0; i<str.length; i++)
+ charstr[i]=str[i];
+ this.value=charstr;
+ this.count=str.length;
+ this.offset=0;
+ }
+
+ public static char[] toLocalCharArray(GString str) {
+ char[] c;
+ int length;
+
+ length = str.length();
+
+ c = new char[length];
+
+ for (int i = 0; i < length; i++) {
+ c[i] = str.value[i+str.offset];
+ }
+ return c;
+ }
+
+ public String toLocalString() {
+ return new String(toLocalCharArray(this));
+ }
+
+ public int length() {
+ return count;
+ }
+
+ public int indexOf(int ch, int fromIndex) {
+ for (int i = fromIndex; i < count; i++)
+ if (this.charAt(i) == ch)
+ return i;
+ return -1;
+ }
+
+ public int lastindexOf(int ch) {
+ return this.lastindexOf(ch, count - 1);
+ }
+
+ public int lastindexOf(int ch, int fromIndex) {
+ for (int i = fromIndex; i > 0; i--)
+ if (this.charAt(i) == ch)
+ return i;
+ return -1;
+ }
+
+ public char charAt(int i) {
+ return value[i+offset];
+ }
+
+ public int indexOf(String str) {
+ return this.indexOf(str, 0);
+ }
+
+ public int indexOf(String str, int fromIndex) {
+ if (fromIndex < 0)
+ fromIndex = 0;
+ for (int i = fromIndex; i <= (count-str.count); i++)
+ if (regionMatches(i, str, 0, str.count))
+ return i;
+ return -1;
+ }
+
+ public boolean regionMatches(int toffset, String other, int ooffset, int len) {
+ if (toffset < 0 || ooffset < 0 || (toffset+len) > count || (ooffset+len) > other.count)
+ return false;
+
+ for (int i = 0; i < len; i++) {
+ if (other.value[i+other.offset+ooffset] != this.value[i+this.offset+toffset])
+ return false;
+ }
+ return true;
+ }
+
+ public String subString(int beginIndex, int endIndex) {
+ return substring(beginIndex, endIndex);
+ }
+
+ public String substring(int beginIndex, int endIndex) {
+ String str;
+ str = global new String();
+ str.value = this.value;
+ str.count = endIndex-beginIndex;
+ str.offset = this.offset + beginIndex;
+ return str;
+ }
+
+ public static String valueOf(Object o) {
+ if (o==null)
+ return "null";
+ else
+ return o.toString();
+ }
+}
public class HashEntry {
- String engine;
- String signature;
+ GString engine;
+ GString signature;
HashStat stats;
public HashEntry() {
}
return result;
}
- public void setengine(String engine) {
+ public void setengine(GString engine) {
this.engine=engine;
}
this.stats=stats;
}
- public void setsig(String signature) {
+ public void setsig(GString signature) {
this.signature=signature;
}
- public String getEngine() {
+ public GString getEngine() {
return engine;
}
- public String getSignature() {
+ public GString getSignature() {
return signature;
}
FilterStatistic[] userstat;
int[] listofusers;
public HashStat() {
- userid = new int[8]; //max users for our system=8
- userstat = new FilterStatistic[8];
+ userid = global new int[8]; //max users for our system=8
+ userstat = global new FilterStatistic[8];
for(int i=0; i<8; i++) {
- userstat[i] = new FilterStatistic();
+ userstat[i] = global new FilterStatistic();
}
}
public int[] getUsers() {
int nusers = numUsers();
- listofusers = new int[nusers];
+ listofusers = global new int[nusers];
int j=0;
for(int i=0; i<8; i++) {
if(userid[i] == 1) {
String body;
String noURLBody;
String sourceCode;
+ String spam;
boolean hasAttachement;
String encoding; //rich text, plain, html
while((line = fileinput.readLine()) != null)
{
Vector splittedLine = line.split();
- if(((String)(splittedLine.elementAt(0))).equals("Header:")) // message id
+ if(((String)(splittedLine.elementAt(0))).equals("Spam:"))
+ {
+ spam = (String)splittedLine.elementAt(1);
+ }
+ else if(((String)(splittedLine.elementAt(0))).equals("Header:")) // message id
{
header = (String)splittedLine.elementAt(1);
}
return sourceCode;
}
- // TODO: String? Is this a boolean, a number, or can be both?
public void setHasAttachement(boolean hasAttachement) {
this.hasAttachement = hasAttachement;
}
}
public boolean getIsSpam() {
- return isSpam;
+ if(spam.equals("yes"))
+ return true;
+ return false;
}
/**
thid = id;
}
- Random rand = new Random(0);
+ Random rand = new Random(thid);
+ Random myrand = new Random(0);
+
+ /*
+ if(id==0) {
+ //Randomly set Spam vals for each email
+ for(int i=0; i<nemails; i++) {
+ Mail email = new Mail("emails/email"+i);
+ int spamval = rand.nextInt(100);
+ if(spamval<60) { //assume 60% are spam and rest are ham
+ email.setIsSpam(false);
+ } else {
+ email.setIsSpam(true);
+ }
+ }
+ }
+ */
for(int i=0; i<niter; i++) {
for(int j=0; j<nemails; j++) {
int pickemail = rand.nextInt(100);
Mail email = new Mail("emails/email"+pickemail);
- //Mail email = getEmail(pickemail);
Vector signatures = email.checkMail(thid);
+
//check with global data structure
int[] confidenceVals=null;
atomic {
FilterResult filterResult = new FilterResult();
boolean filterAnswer = filterResult.getResult(confidenceVals);
+ //---- get user's take on email and send feedback ------
+ /*
+ int spamval = rand.nextInt(100);
+ if(spamval<60) { //assume 60% are spam and rest are ham
+ email.setIsSpam(false);
+ } else {
+ email.setIsSpam(true);
+ }
+ */
boolean userAnswer = email.getIsSpam();
if(filterAnswer != userAnswer) {
atomic {
SpamFilter.parseCmdLine(args, sf);
int nthreads = sf.nthreads;
- Random rand = new Random(8);
- //Randomly set Spam vals for each email
- for(int i=0; i<sf.numemail; i++) {
- Mail email = new Mail("./emails/email"+i);
- int spamval = rand.nextInt(100);
- if(spamval<60) { //assume 60% are spam and rest are ham
- email.setIsSpam(false);
- } else {
- email.setIsSpam(true);
- }
- }
-
//Create Global data structure
DistributedHashMap dhmap;
SpamFilter[] spf;
for(int i=0; i<numparts; i++) {
String part = (String)(signatures.elementAt(i));
char tmpengine = part.charAt(0);
- String engine = global new String(tmpengine);
- String signature = global new String(part.substring(2));
- //String signature = part.substring(2); //a:b index(a)=0, index(:)=1, index(b)=2
+ GString engine=null;
+ if(tmpengine == '4') { //Ephemeral Signature calculator
+ String tmpstr = new String("4");
+ engine = global new GString(tmpstr);
+ }
+ if(tmpengine == '8') { //Whiplash Signature calculator
+ String tmpstr = new String("8");
+ engine = global new GString(tmpstr);
+ }
+ String str = new String(part.substring(2));//a:b index of a =0, index of : =1, index of b =2
+ GString signature = global new GString(str);
HashEntry myhe = global new HashEntry();
myhe.setengine(engine);
myhe.setsig(signature);
// ----- now connect to global data structure and ask for spam -----
HashEntry tmphe = (HashEntry)(mydhmap.getKey(myhe));
FilterStatistic fs = (FilterStatistic) (mydhmap.get(myhe)); //get the value from hash
+
confidenceVals[i] = fs.getChecked();
}
}
// --> the mail client is able to determine if it is spam or not
+ // --- According to the "any"-logic (in Core#check_logic) in original Razor ---
+ // If any answer is spam, the entire email is spam.
return confidenceVals;
}
+ /**
+ * This method sends feedback from the user to a distributed
+ * spam database and trains the spam database to check future
+ * emails and detect spam
+ **/
public void sendFeedBack(Mail mail, boolean isSpam, int id) {
Vector partsOfMailStrings = mail.getCommonPart();
partsOfMailStrings.addElement(mail.getBodyString());
for(int i=0;i<signatures.size();i++) {
String part = (String)(signatures.elementAt(i));
- char tmpengine = part.charAt(0);
- String engine = global new String(tmpengine);
- String signature = global new String(part.substring(2));
- //String signature = part.substring(2); //a:b index(a)=0, index(:)=1, index(b)=2
+ //
+ // Signature is of form a:b
+ // where a = string representing a signature engine
+ // either "4" or "8"
+ // b = string representing signature
+ //
+ char tmpengine = part.charAt(0); //
+ GString engine;
+ if(tmpengine == '4') {
+ String tmpstr = new String("4");
+ engine = global new GString(tmpstr);
+ }
+ if(tmpengine == '8') {
+ String tmpstr = new String("8");
+ engine = global new GString(tmpstr);
+ }
+ String tmpsig = new String(part.substring(2));
+ GString signature = global new GString(tmpsig);
HashEntry myhe = global new HashEntry();
myhe.setengine(engine);
myhe.setsig(signature);
- // ----- now connect to global data structure and upate spam count -----
+ // ----- now connect to global data structure and update stats -----
HashEntry tmphe = (HashEntry)(mydhmap.getKey(myhe));
if(tmphe.stats.userid[id] != 1) {
tmphe.stats.setuserid(id);
}
- FilterStatistic fs = (FilterStatistic) (mydhmap.get(myhe)); //get the value from hash
+ //---- get value from distributed hash and update spam count
+ FilterStatistic fs = (FilterStatistic) (mydhmap.get(myhe));
+
+ //TODO: Allow users to give incorrect feedback
//Increment spam or ham value
if(isSpam) {
sprintf(fileNameBuffer,"%s%d",fileName,i+1);
newFile = fopen(fileNameBuffer,"w");
+ // write spam or no spam
+ // 60% of email is spam and rest is ham
+ char yes[] = "yes";
+ char no[] = "no";
+ int tmprandindex = rand() % num_email;
+ if(tmprandindex<60)
+ fprintf(newFile,"Spam: %s\n",yes);
+ else
+ fprintf(newFile,"Spam: %s\n",no);
+
// write header
fprintf(newFile,"Header: %d\n",i+1);
SignatureComputer.java \
FilterStatistic.java \
EphemeralSignature.java \
+ GString.java \
WhiplashSignature.java
FLAGS1=-dsm -optimize -mainclass ${MAINCLASS}
FLAGS2=-dsm -dsmcaching -optimize -mainclass ${MAINCLASS}
-FLAGS3=-dsm -dsmcaching -prefetch -optimize -mainclass ${MAINCLASS}
+FLAGS3=-dsm -dsmcaching -rangeprefetch -optimize -mainclass ${MAINCLASS}
default:
../../../buildscript ${FLAGS1} -o ${MAINCLASS}NPNC ${SRC}
-# ../../../buildscript ${FLAGS2} -o ${MAINCLASS}NPC ${SRC}
-# ../../../buildscript ${FLAGS3} -o ${MAINCLASS}RangeN ${SRC}
+ ../../../buildscript ${FLAGS2} -o ${MAINCLASS}NPC ${SRC}
+ ../../../buildscript ${FLAGS3} -o ${MAINCLASS}RangeN ${SRC}
clean:
rm -rf tmpbuilddirectory