Commit 78e6e1be authored by toaster's avatar toaster
Browse files

token store operations

git-svn-id: https://subversion.umiacs.umd.edu/ace/trunk@30 f1b3a171-7291-4a19-a512-95ad0ad9394a
parent 72742f77
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
* Copyright (c) 2007-2010, University of Maryland
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of conditions
* and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions
* and the following disclaimer in the documentation and/or other materials provided with the
* distribution.
*
* Neither the name of the University of Maryland nor the names of its contributors may be used to
* endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ACE Components were written in the ADAPT Project at the University of
* Maryland Institute for Advanced Computer Study.
*/
package edu.umiacs.ace.ims.api;
......@@ -9,6 +34,8 @@ import edu.umiacs.ace.ims.ws.TokenResponse;
import edu.umiacs.ace.token.AceToken;
import edu.umiacs.ace.token.AceTokenBuilder;
import edu.umiacs.ace.util.HashValue;
import java.util.List;
import org.apache.log4j.Logger;
/**
*
......@@ -16,6 +43,7 @@ import edu.umiacs.ace.util.HashValue;
*/
public class IMSUtil {
private static final Logger LOG = Logger.getLogger(IMSUtil.class);
private static ThreadLocal<AceTokenBuilder> tokenBuilder = new ThreadLocal<AceTokenBuilder>()
{
......@@ -26,6 +54,72 @@ public class IMSUtil {
};
public static String formatProof(TokenResponse resp)
{
StringBuilder proof = new StringBuilder();
for (ProofElement pe : resp.getProofElements())
{
proof.append(formatHashLevel(pe));
proof.append('\n');
}
return proof.toString();
}
public static String formatHashLevel(ProofElement pe)
{
int inheritIdx = pe.getIndex();
List<String> hashes = pe.getHashes();
// LOG.trace("received "+ inheritIdx + " " + hashes.toString());
StringBuilder level = new StringBuilder();
int i = 0;
for (; i < hashes.size(); i++)
{
if (i == inheritIdx)
{
level.append("X:");
}
level.append(hashes.get(i));
if (i != (hashes.size() -1))
level.append(':');
}
if (inheritIdx == i)
level.append(":X");
// LOG.trace("converted "+ level.toString());
return level.toString();
}
// public static TokenResponse convertToken(AceToken token) {
//
// TokenResponse response = new TokenResponse();
// response.setTimestamp()
// bldr.setDate(response.getTimestamp().toGregorianCalendar().getTime());
//
// bldr.setDigestAlgorithm(response.getDigestService());
// bldr.setRound(response.getRoundId());
// bldr.setIms(ims);
// bldr.setImsService(response.getTokenClassName());
//
// for (ProofElement pe : response.getProofElements())
// {
// bldr.startProofLevel(pe.getHashes().size() + 1);
// bldr.setLevelInheritIndex(pe.getIndex());
//
// for (String hash : pe.getHashes())
// {
// bldr.addLevelHash(HashValue.asBytes(hash));
// }
//
// }
//// return bldr.createToken();
// return response;
// }
public static AceToken convertResponse(TokenResponse response, String ims) {
AceTokenBuilder bldr = tokenBuilder.get();
......
......@@ -31,9 +31,10 @@
package edu.umiacs.ace.ims.api;
import edu.umiacs.ace.hashtree.ProofValidator;
import edu.umiacs.ace.ims.ws.ProofElement;
import edu.umiacs.ace.ims.ws.RoundSummary;
import edu.umiacs.ace.ims.ws.TokenResponse;
import edu.umiacs.ace.token.AceToken;
import edu.umiacs.ace.util.HashValue;
import edu.umiacs.util.Check;
import java.security.MessageDigest;
......@@ -56,7 +57,7 @@ import org.apache.log4j.NDC;
public class TokenValidator extends Thread
{
private Map<TokenResponse, String> requests = new HashMap<TokenResponse, String>();
private Map<AceToken, String> requests = new HashMap<AceToken, String>();
private IMSService connection;
private ValidationCallback callback;
private static final Logger print =
......@@ -85,7 +86,32 @@ public class TokenValidator extends Thread
this.start();
}
public void add(String fileHash, TokenResponse token) throws InterruptedException
// public void add(String fileHash, AceToken token) throws InterruptedException
// {
// Check.notNull("token", token);
// Check.notNull("fileHash", fileHash);
//
// lock.lockInterruptibly();
// try
// {
// if ( shutdownRequested )
// {
// throw new IllegalStateException("Process shutdown");
// }
// print.trace("Adding work: " + fileHash);
// requests.put(IMSUtil.convertToken(token), fileHash);
// if ( requests.size() >= maxQueueLength )
// {
// processCondition.signal();
// }
// }
// finally
// {
// lock.unlock();
// }
// }
public void add(String fileHash, AceToken token) throws InterruptedException
{
Check.notNull("token", token);
Check.notNull("fileHash", fileHash);
......@@ -151,6 +177,7 @@ public class TokenValidator extends Thread
private void processBatch()
{
List<Long> roundNumbers = new ArrayList<Long>();
ProofValidator pv = new ProofValidator();
Map<Long, LinkedList<WorkUnit>> proofMap = new HashMap<Long, LinkedList<WorkUnit>>();
......@@ -158,21 +185,21 @@ public class TokenValidator extends Thread
try
{
print.trace("Processing batch of size: " + requests.size());
for ( TokenResponse token : requests.keySet() )
for ( AceToken token : requests.keySet() )
{
String hash = requests.get(token);
WorkUnit unit = new WorkUnit();
unit.setHash(hash);
unit.setTokenResponse(token);
if ( !roundNumbers.contains(token.getRoundId()) )
if ( !roundNumbers.contains(token.getRound()) )
{
roundNumbers.add(token.getRoundId());
proofMap.put(token.getRoundId(), new LinkedList<WorkUnit>());
roundNumbers.add(token.getRound());
proofMap.put(token.getRound(), new LinkedList<WorkUnit>());
}
proofMap.get(token.getRoundId()).offer(unit);
proofMap.get(token.getRound()).offer(unit);
}
print.trace("batch load finished, calling clear, unlocking.");
requests.clear();
......@@ -194,12 +221,13 @@ public class TokenValidator extends Thread
for ( WorkUnit unit : proofMap.get(round) )
{
TokenResponse response = unit.getTokenResponse();
AceToken response = unit.getTokenResponse();
String localLeafHash = unit.getHash();
String imsSuppliedHash = summary.getHashValue();
String calculatedHash = calculateRoot(digest,
localLeafHash, response.getProofElements());
// String calculatedHash = calculateRoot(digest,
// localLeafHash, response.getProofElements());
String calculatedHash = HashValue.asHexString(pv.rootHash(digest, response.getProof(), HashValue.asBytes(localLeafHash)));
if ( imsSuppliedHash.equals(calculatedHash) )
{
......@@ -330,18 +358,18 @@ public class TokenValidator extends Thread
{
private String hash;
private TokenResponse tokenResponse;
private AceToken tokenResponse;
public WorkUnit()
{
}
public void setTokenResponse(TokenResponse tokenResponse)
public void setTokenResponse(AceToken tokenResponse)
{
this.tokenResponse = tokenResponse;
}
public TokenResponse getTokenResponse()
public AceToken getTokenResponse()
{
return tokenResponse;
}
......
......@@ -31,7 +31,7 @@
package edu.umiacs.ace.ims.api;
import edu.umiacs.ace.ims.ws.TokenResponse;
import edu.umiacs.ace.token.AceToken;
/**
*
......@@ -42,7 +42,7 @@ public interface ValidationCallback
public void unexpectedException(Throwable throwable);
public void validToken(TokenResponse response);
public void validToken(AceToken response);
/**
* Called if a hash did not validate. This will be called if either the localhash
......@@ -52,6 +52,6 @@ public interface ValidationCallback
* @param correctCSI IMS stored CSI
* @param calculatedCSI CSI generated from local hash and token
*/
public void invalidToken(TokenResponse response, String correctCSI,
public void invalidToken(AceToken response, String correctCSI,
String calculatedCSI);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment