Author: xor
Date: 2008-11-11 01:24:25 +0000 (Tue, 11 Nov 2008)
New Revision: 23475
Modified:
trunk/plugins/WoT/introduction/IntroductionPuzzle.java
trunk/plugins/WoT/introduction/IntroductionServer.java
Log:
Implement some more of the introduction code.
Modified: trunk/plugins/WoT/introduction/IntroductionPuzzle.java
===================================================================
--- trunk/plugins/WoT/introduction/IntroductionPuzzle.java 2008-11-10
18:50:52 UTC (rev 23474)
+++ trunk/plugins/WoT/introduction/IntroductionPuzzle.java 2008-11-11
01:24:25 UTC (rev 23475)
@@ -5,25 +5,28 @@
*/
package plugins.WoT.introduction;
+import java.net.MalformedURLException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
import com.db4o.query.Query;
+import freenet.crypt.SHA1;
+import freenet.keys.FreenetURI;
+
import plugins.WoT.Identity;
+import plugins.WoT.OwnIdentity;
+import plugins.WoT.WoT;
public class IntroductionPuzzle {
- enum PuzzleType {
- Image,
- Audio
- };
+ public static final String INTRODUCTION_CONTEXT = "introduction";
+ public static final int MINIMAL_SOLUTION_LENGTH = 5;
- private final PuzzleType mType;
-
private final String mMimeType;
- private final String mFilename;
-
private final byte[] mData;
private final String mSolution;
@@ -32,6 +35,11 @@
private final long mValidUntilTime;
+ private final Date mDateOfInsertion;
+
+ private final int mIndex;
+
+
/* FIXME: wire this in */
/**
* Get a list of fields which the database should create an index on.
@@ -45,14 +53,17 @@
* @param newType
* @param newData
*/
- public IntroductionPuzzle(Identity newInserter, PuzzleType newType,
String newMimeType, String newFilename, byte[] newData, long newValidUntilTime)
{
+ public IntroductionPuzzle(Identity newInserter, String newMimeType,
String newFilename, byte[] newData, long myValidUntilTime, Date
myDateOfInsertion, int myIndex) {
+ assert( newInserter != null && newMimeType != null &&
!newMimeType.equals("") && newFilename!=null && !newFilename.equals("") &&
+ newData!=null && newData.length!=0 &&
myValidUntilTime > System.currentTimeMillis() && myDateOfInsertion != null &&
+ myDateOfInsertion.getTime() <
System.currentTimeMillis() && myIndex >= 0);
mInserter = newInserter;
- mType = newType;
mMimeType = newMimeType;
- mFilename = newFilename;
mData = newData;
mSolution = null;
- mValidUntilTime = newValidUntilTime;
+ mDateOfInsertion = myDateOfInsertion;
+ mValidUntilTime = myValidUntilTime;
+ mIndex = myIndex;
}
/**
@@ -60,29 +71,57 @@
* @param newType
* @param newData
*/
- public IntroductionPuzzle(Identity newInserter, PuzzleType newType,
String newMimeType, String newFilename, byte[] newData, String newSolution) {
+ public IntroductionPuzzle(Identity newInserter, String newMimeType,
byte[] newData, String newSolution, int myIndex) {
+ assert( newInserter != null && newMimeType != null &&
!newMimeType.equals("") && newData!=null && newData.length!=0 &&
+ newSolution!=null &&
newSolution.length()>=MINIMAL_SOLUTION_LENGTH && myIndex >= 0);
mInserter = newInserter;
- mType = newType;
mMimeType = newMimeType;
- mFilename = newFilename;
mData = newData;
mSolution = newSolution;
- mValidUntilTime = System.currentTimeMillis() +
IntroductionServer.PUZZLE_INVALID_AFTER_DAYS * 24 * 60 * 60 * 1000;
+ mDateOfInsertion = new Date(); /* FIXME: get it in UTC */
+ mValidUntilTime = System.currentTimeMillis() +
IntroductionServer.PUZZLE_INVALID_AFTER_DAYS * 24 * 60 * 60 * 1000; /* FIXME:
get it in UTC */
+ mIndex = myIndex;
}
- public PuzzleType getPuzzleType() {
- return mType;
- }
-
public String getMimeType() {
return mMimeType;
}
- public String getFilename() {
- /* FIXME: include date etc. */
- return mFilename;
+ /**
+ * Get the URI at which to insert this puzzle.
+ * [EMAIL PROTECTED]/WoT/introduction/yyyy-MM-dd|#.xml
+ */
+ public FreenetURI getURI() throws MalformedURLException {
+ assert(mSolution != null); /* This function should only be
needed by the introduction server, not by clients. */
+
+ /* FIXME: I did not really understand the javadoc of
FreenetURI. Please verify that the following code actually creates an URI
+ * which looks like the one I specified in the javadoc above
this function. Thanks. */
+ String dayOfInsertion = new
SimpleDateFormat("yyyy-MM-dd").format(mDateOfInsertion);
+ FreenetURI baseURI =
((OwnIdentity)mInserter).getInsertURI().setKeyType("KSK");
+ baseURI = baseURI.setDocName(WoT.WOT_CONTEXT + "/" +
INTRODUCTION_CONTEXT);
+ return baseURI.setMetaString(new String[] {dayOfInsertion + "|"
+ mIndex + ".xml"} );
}
+
+ /**
+ * Get the URI at which to look for a solution of this puzzle (if
someone solved it)
+ */
+ public FreenetURI getSolutionURI() {
+ return getSolutionURI(mSolution);
+ }
+
+ /**
+ * Get the URI at which to insert the solution of this puzzle.
+ */
+ public FreenetURI getSolutionURI(String guessOfSolution) {
+ String dayOfInsertion = new
SimpleDateFormat("yyyy-MM-dd").format(mDateOfInsertion);
+ return new FreenetURI("KSK", INTRODUCTION_CONTEXT + "|" +
+
mInserter.getId() + "|" +
+ dayOfInsertion
+ "|" +
+ mIndex + "|" +
+
guessOfSolution); /* FIXME: hash the solution!! */
+ }
+
public byte[] getPuzzle() {
return mData;
}
@@ -99,11 +138,14 @@
return mInserter;
}
+ public Date getDateOfInsertion() {
+ return mDateOfInsertion;
+ }
+
public long getValidUntilTime() {
return mValidUntilTime;
}
-
public void store(ObjectContainer db) {
db.store(this);
db.commit();
Modified: trunk/plugins/WoT/introduction/IntroductionServer.java
===================================================================
--- trunk/plugins/WoT/introduction/IntroductionServer.java 2008-11-10
18:50:52 UTC (rev 23474)
+++ trunk/plugins/WoT/introduction/IntroductionServer.java 2008-11-11
01:24:25 UTC (rev 23475)
@@ -41,7 +41,6 @@
private static final long THREAD_PERIOD = 30 * 60 * 1000;
private static final short PUZZLES_COUNT = 5;
public static final long PUZZLE_INVALID_AFTER_DAYS = 3;
- private static final String INTRODUCTION_CONTEXT = "introduction";
private Thread mThread;
@@ -91,9 +90,10 @@
while(identities.hasNext()) {
OwnIdentity identity = identities.next();
- if(identity.hasContext("introduction")) {
+
if(identity.hasContext(IntroductionPuzzle.INTRODUCTION_CONTEXT)) {
try {
managePuzzles(identity);
+ downloadSolutions(identity);
} catch (Exception e) {
Logger.error(this, "Puzzle
insert failed: " + e.getMessage(), e);
}
@@ -135,7 +135,7 @@
}
private void insertNewPuzzle(OwnIdentity identity) throws IOException,
InsertException {
- Bucket tempB = mTBF.makeBucket(10 * 1024);
+ Bucket tempB = mTBF.makeBucket(10 * 1024); /* TODO: set to a
reasonable value */
OutputStream os = tempB.getOutputStream();
try {
@@ -145,13 +145,12 @@
tempB.setReadOnly();
ClientMetadata cmd = new
ClientMetadata(p.getMimeType());
- FreenetURI uri = new FreenetURI("KSK", p.getFilename());
- InsertBlock ib = new InsertBlock(tempB, cmd, uri);
+ InsertBlock ib = new InsertBlock(tempB, cmd,
p.getURI());
Logger.debug(this, "Started insert puzzle from '" +
identity.getNickName() + "'");
/* FIXME: use nonblocking insert */
- mClient.insert(ib, true, p.getFilename());
+ mClient.insert(ib, true, p.getURI().getMetaString());
db.store(p);
db.commit();
@@ -161,4 +160,8 @@
Logger.debug(this, "Successful insert of puzzle for '" +
identity.getNickName() + "'");
}
+
+ private void downloadSolutions(OwnIdentity identity) {
+
+ }
}
_______________________________________________
cvs mailing list
[email protected]
http://emu.freenetproject.org/cgi-bin/mailman/listinfo/cvs