law(secondLaw,language(java)) import java.util.*; import java.io.*; authority(CA,keyHash(E3928FBF60B2AF049F4B529F777EF4C0)). public class secondLaw extends Law{ //////////////////////////////////////////////////////////////////////////////////////////////////////// public void sent(String source, String message, String dest) { System.out.println("source "+source); System.out.println("destination "+dest); int start=0; while((start+1<=source.length())&&(!source.substring(start,start+1).equals("@"))){ start++; } if(CS.has("certified_"+source.substring(0,start) )){ if(message.startsWith("register") && dest.equals("s@arthur.rutgers.edu")){ if(!CS.has(source+"_member")){ //doDiscloseAllCS(); System.out.println("SENT"); doForward(); return; }else if(CS.has(source+"_member")){ doDeliver("controller","you are a member already!",Self); return; } } if(CS.has(source+"_member")) if(message.startsWith("name@")) doForward(); else doForward(source, message+"@"+source.substring(0,start),dest); //which should be equal to his sertified name else doDeliver("controller","you need to register first!",Self); } else doDeliver("controller","you need to have a certificate!",Self); } //////////////////////////////////////////////////////////////////////////////////////////////////// public void arrived(String source, String message, String dest) { System.out.println("source "+source); System.out.println("destination "+dest); System.out.println("message "+message); int start=0; while((start+1<=dest.length())&&(!dest.substring(start,start+1).equals("@"))){ start++; } int start2=0; while((start2+1<=source.length())&&(!source.substring(start2,start2+1).equals("@"))){ start2++; } if(CS.has("certified_"+dest.substring(0,start))){ if( message.startsWith("member") && source.equals("s@arthur.rutgers.edu")){ doAdd(dest+"_member"); doImposeObligation("alive",60,"sec"); //System.out.println("IT WORKED!!!!!!!!!"); doDeliver(); return; }//if if( message.startsWith("register") && dest.equals("s@arthur.rutgers.edu")){ if( (message.substring(9,message.length())).equals(source.substring(0,start2))){ // certified name is equal to his real name!!!! if(!CS.has(source.substring(0,start2))){ //make sure the certificate name is unique doAdd(source); doAdd(source.substring(0,start2)); doImposeObligation(source,65,"sec"); System.out.println("ARRIVED"); doForward("s@arthur.rutgers.edu","member_"+source,source); }//if has else { System.out.println("repeating name, recreate ."); return; }///else }//if cert else System.out.println("name does not match certificate ."); } if( message.startsWith("tick") && dest.equals("s@arthur.rutgers.edu") && CS.has(source)){ System.out.println(source+" is alive!"); doRepealObligation(source); doImposeObligation(source,65,"sec"); doForward("s@arthur.rutgers.edu","tuck_"+source,source); return; } if(message.startsWith("tuck") && source.equals("s@arthur.rutgers.edu")&& CS.has(dest+"_member")){ doImposeObligation("alive",60,"sec"); doDeliver(); return; } if(message.startsWith("NOT_MEMBER") && source.equals("s@arthur.rutgers.edu") && CS.has(dest+"_member")){ doRemove(dest+"_member"); System.out.println(dest +" is not a member anymore \n"); return; } if( message.startsWith("name@") && dest.equals("s@arthur.rutgers.edu") && CS.has(source)){ if(CS.has(message.substring(5, message.length()))){ System.out.println("The secretary has this information\n"); CSIterator my=new CSIterator(CS); while(my.hasNext()){ String address=(my.next()).toString(); if(address.startsWith(message.substring(5,message.length())+"@")) doForward("s@arthur.rutgers.edu", address, source); }//end while } } doDeliver(); //if an member is certified, Deliver a message to him } else System.out.println(dest+" is not certified \n"); } ///////////////////////////////// public void adopted(String arg, String[] issuer, String[] subject, String[] attributes) { System.out.println("ADOPTED "); } public void adopted(String arg){} public void certified(String source, String issuer, String subject, String attributes){ int start=0; while((start+1<=source.length())&&(!source.substring(start,start+1).equals("@"))){ start++; } if (issuer.equals("CA") && attributes.equals(source.substring(0,start))) { doAdd("certified_"+attributes); //doDiscloseAllCS(); System.out.println("CERTIFIED for sure"); if(!source.equals("s@arthur.rutgers.edu")) doDeliver("controller","Now, you must register with the secretary!",Self); } } ////////////////////////////////// public void obligationDue(Term obligationTerm) { if (obligationTerm.toString().equals("alive")){ doForward(Self,"tick_"+Self,"s@arthur.rutgers.edu"); //doImposeObligation("alive",60,"sec"); }else if (obligationTerm.toString().equals("toQuit")) doQuit(); else{ if(Self.equals("s@arthur.rutgers.edu")){ doRemove(obligationTerm.toString()); doForward(Self,"NOT_MEMBER_"+obligationTerm.toString(),obligationTerm.toString()); } } } //////////////////////////////////////////////////////// public void disconnected() { System.out.println("DISCONNECTED"); //doImposeObligation("toQuit", 10, "min"); } ///////////////////////////////////////////////////// public void reconnected() { //doRepealObligation("toQuit"); } /////////////////////////////////////////////////////// /*public void exception(Message m, String diagnostic) { doDeliver("law", "exception(type(" + m.type + "),source(" + m.source + "),payload(" + m.s_payload + "),dest(" + m.dest + "),diagnostic(" + diagnostic + "))", Self); doForward(); } */ ///////////////////////////////////////////////////////// public void created(String creator, String claw, String arg){ System.out.println("CREATED "+creator); } }