[virtualcommons-svn] SF.net SVN: virtualcommons:[450] csidex/trunk/src
Status: Beta
Brought to you by:
alllee
From: <al...@us...> - 2010-02-02 20:50:05
|
Revision: 450 http://virtualcommons.svn.sourceforge.net/virtualcommons/?rev=450&view=rev Author: alllee Date: 2010-02-02 20:49:58 +0000 (Tue, 02 Feb 2010) Log Message: ----------- patching SocketIdentifier for new IP scheme used by COOR 5519 where the station identifier is part of the hostname instead of the IP address. Modified Paths: -------------- csidex/trunk/src/main/java/edu/asu/commons/net/SocketIdentifier.java csidex/trunk/src/main/java/edu/asu/commons/util/Duration.java csidex/trunk/src/test/java/edu/asu/commons/conf/ConfigurationTest.java Modified: csidex/trunk/src/main/java/edu/asu/commons/net/SocketIdentifier.java =================================================================== --- csidex/trunk/src/main/java/edu/asu/commons/net/SocketIdentifier.java 2010-02-02 20:47:51 UTC (rev 449) +++ csidex/trunk/src/main/java/edu/asu/commons/net/SocketIdentifier.java 2010-02-02 20:49:58 UTC (rev 450) @@ -122,17 +122,35 @@ if (stationNumber != null) { return stationNumber.intValue(); } - StringTokenizer tokenizer = new StringTokenizer(remoteSocketAddress.toString(), "."); - String hostname = tokenizer.nextToken(); + String remoteHostname = remoteSocketAddress.getHostName(); + StringTokenizer tokenizer = new StringTokenizer(remoteHostname, "."); + String stationId = tokenizer.nextToken(); - int startIndex = hostname.length() - 2; - String station = hostname.substring(startIndex, startIndex+2); + int startIndex = stationId.lastIndexOf("-"); + String station = stationId.substring(startIndex + 1, stationId.length()); try { stationNumber = Math.abs(Integer.valueOf(station)); stationed = true; return stationNumber; } catch (NumberFormatException e) { + // try get station from ip + return getStationFromIP(); + } + } + + private int getStationFromIP() { + if (stationNumber != null) { + return stationNumber.intValue(); + } + String remoteHost = remoteSocketAddress.toString(); + String lastIPDigits = remoteHost.substring(remoteHost.lastIndexOf(".") + 1, remoteHost.length()); + try { + stationNumber = Math.abs(Integer.parseInt(lastIPDigits)); + stationed = true; + return stationNumber; + } + catch (NumberFormatException e) { stationed = false; return id; } Modified: csidex/trunk/src/main/java/edu/asu/commons/util/Duration.java =================================================================== --- csidex/trunk/src/main/java/edu/asu/commons/util/Duration.java 2010-02-02 20:47:51 UTC (rev 449) +++ csidex/trunk/src/main/java/edu/asu/commons/util/Duration.java 2010-02-02 20:49:58 UTC (rev 450) @@ -59,11 +59,13 @@ } public static long toMillis(int seconds) { +// return TimeUnit.MILLISECONDS.convert(seconds, TimeUnit.SECONDS); return seconds * 1000L; } public static int toSeconds(long milliseconds) { - return (int) (milliseconds / 1000); +// return TimeUnit.SECONDS.convert(milliseconds, TimeUnit.MILLISECONDS); + return (int) (milliseconds / 1000L); } public long getDelta() { Modified: csidex/trunk/src/test/java/edu/asu/commons/conf/ConfigurationTest.java =================================================================== --- csidex/trunk/src/test/java/edu/asu/commons/conf/ConfigurationTest.java 2010-02-02 20:47:51 UTC (rev 449) +++ csidex/trunk/src/test/java/edu/asu/commons/conf/ConfigurationTest.java 2010-02-02 20:49:58 UTC (rev 450) @@ -1,9 +1,16 @@ package edu.asu.commons.conf; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + import org.junit.Before; import org.junit.Test; +import edu.asu.commons.util.Duration; +import static org.junit.Assert.*; public class ConfigurationTest { @@ -15,9 +22,338 @@ } @Test + public void testConfigurationAssistant() { + assertEquals(0.0d, assistant.getDoubleProperty("foo", 0.0d), 0.0d); + } + + @Test public void testDefaultConfigurationDirectory() { + + } + + @Test + public void testTemplate() { + ServerConfiguration serverConfiguration = new ServerConfiguration(); + + } + + public static class ServerConfiguration + extends ExperimentConfiguration.Base<RoundConfiguration> { + + private static final long serialVersionUID = 7867208205942476733L; + + private final static String CONFIGURATION_FILE_NAME = "irrigation.xml"; + + private final static String DEFAULT_LOG_FILE_DESTINATION = "irrigation.log"; + + public ServerConfiguration() { + super(); + } + + public ServerConfiguration(String configurationDirectory) { + super(configurationDirectory); + } + + public String getLogFileDestination() { + return assistant.getStringProperty("log", DEFAULT_LOG_FILE_DESTINATION); + } + + public String getPersistenceDirectory() { + return assistant.getStringProperty("save-dir", "data"); + } + + public boolean shouldUpdateFacilitator() { + return assistant.getBooleanProperty("update-facilitator"); + } + + @Override + protected RoundConfiguration createRoundConfiguration(String roundConfigurationFile) { + return new RoundConfiguration(roundConfigurationFile); + } + + @Override + protected String getServerConfigurationFilename() { + return CONFIGURATION_FILE_NAME; + } + + private final static String[] PRIORITY_STRINGS = { "A", "B", "C", "D", "E" }; + + public String toPriorityString(int clientPriority) { + // bounds check + if (clientPriority >= 0 && clientPriority < PRIORITY_STRINGS.length) { + return PRIORITY_STRINGS[clientPriority]; + } + return "Position not found"; + } + + public boolean isUndisruptedFlowRequired(){ + return assistant.getBooleanProperty("undisrupted-flow-required", false); + } + + public String getUndisruptedFlowInstructions() { + return assistant.getProperty("undisrupted-flow-instructions", ""); + } + + public double getShowUpPayment() { + return assistant.getDoubleProperty("showup-payment", 5.0d); + } + + public String getInitialInstructions() { + String initialInstructions = assistant.getProperty("initial-instructions", ""); + if (initialInstructions.contains("%d")) { + return String.format(initialInstructions, getChatDuration()); + } + return initialInstructions; + } + + public String getWelcomeInstructions() { + return assistant.getProperty("welcome-instructions"); + } + + public Map<String, String> getQuizAnswers() { + Properties properties = assistant.getProperties(); + Map<String, String> answers = new HashMap<String, String>(); + for (int i = 1; properties.containsKey("q" + i); i++) { + String key = "q" + i; + String answer = properties.getProperty(key); + answers.put(key, answer); + String quizExplanationKey = "explanation" + i; + String quizExplanation = properties.getProperty(quizExplanationKey); + answers.put(quizExplanationKey, quizExplanation); + String descriptiveAnswerKey = "a" + i; + answers.put(descriptiveAnswerKey, properties.getProperty(descriptiveAnswerKey)); + } + return answers; + } + + public String getQuizQuestion(int pageNumber) { + return assistant.getProperty("general-instructionsq" + pageNumber); + } + + public String getQuizPage(int pageNumber) { + return assistant.getProperty("quiz-page"+pageNumber); + } + + public String getWaterCollectedToTokensTable() { + return assistant.getProperty("water-collected-to-tokens-table"); + } + + public String getFinalInstructions() { + return assistant.getProperty("final-instructions", "<b>The experiment is now over. Thanks for participating!</b>"); + } + + public String getInvestmentInstructions() { + return assistant.getProperty("investment-instructions"); + } + + public int getNumberOfQuizPages() { + return assistant.getIntProperty("question-pages", 2); + } + + public int getChatDuration() { + return assistant.getIntProperty("chat-duration", 60); + } + + public String getChatInstructions() { + String chatInstructions = assistant.getProperty("chat-instructions", ""); + if (chatInstructions.contains("%d")) { + return String.format(chatInstructions, getChatDuration()); + } + return chatInstructions; + } + + public String getGameScreenshotInstructions() { + return assistant.getProperty("game-screenshot-instructions"); + } + + } + public static class RoundConfiguration extends ExperimentRoundParameters.Base<ServerConfiguration> { + + private static final long serialVersionUID = -5053624886508752562L; + + private static final float DEFAULT_DOLLARS_PER_TOKEN = .05f; + + public static int getTokensEarned(int waterCollected) { + if (waterCollected < 150) { + return 0; + } + else if (waterCollected < 200) { + return 1; + } + else if (waterCollected < 250) { + return 4; + } + else if (waterCollected < 300) { + return 10; + } + else if (waterCollected < 350) { + return 15; + } + else if (waterCollected < 400) { + return 18; + } + else if (waterCollected < 500) { + return 19; + } + else if (waterCollected < 550) { + return 20; + } + else if (waterCollected < 650) { + return 19; + } + else if (waterCollected < 700) { + return 18; + } + else if (waterCollected < 750) { + return 15; + } + else if (waterCollected < 800) { + return 10; + } + else if (waterCollected < 850) { + return 4; + } + else if (waterCollected < 900) { + return 1; + } + else { + return 0; + } + } + + public RoundConfiguration(String resource) { + super(resource); + } + + public int getMaximumClientFlowCapacity() { + return getIntProperty("max-client-flow-capacity", 25); + } + + public int getInitialInfrastructureEfficiency() { + return getIntProperty("initial-infrastructure-efficiency", 75); + } + + public int getInfrastructureDegradationFactor() { + return getIntProperty("infrastructure-degradation-factor", 25); + } + + public int getWaterSupplyCapacity() { + return getIntProperty("max-canal-flow-capacity", 30); + } + + public int getMaximumTokenInvestment() { + return getIntProperty("max-token-investment", 10); + } + + /** + * returns maximum number of tokens that could have been contributed + * @return + */ + public int getMaximumTotalInvestedTokens() { + return getMaximumTokenInvestment() * getClientsPerGroup(); + } + + public int getMaximumInfrastructureEfficiency() { + return getIntProperty("max-infrastructure-efficiency", 100); + } + + public boolean isPracticeRound() { + return getBooleanProperty("practice-round"); + } + + public String getPracticeRoundPaymentInstructions() { + return getProperty("practice-round-payment-instructions", + "This is a practice round so the earnings mentioned are only for illustrative purposes and <b>will not count towards your actual earnings</b>."); + } + + public int getClientsPerGroup() { + return getIntProperty("clients-per-group", 5); + } + + /** + * Returns the dollars/token exchange rate. $1 = 1, 50 cents = $.50, 1 penny per token = .01, etc. + * + * FIXME: this should be a ServerConfiguration parameter unless we change it so + * the client keeps track of total dollars earned per round instead of total tokens earned per round. + * + * @return + */ + public double getDollarsPerToken() { + return getDoubleProperty("dollars-per-token", DEFAULT_DOLLARS_PER_TOKEN); + } + + /** + * for debugging purposes + */ + public void report() { + getProperties().list(System.err); + } + + public boolean shouldResetInfrastructureEfficiency() { + return isFirstRound() || getBooleanProperty("reset-infrastructure-efficiency", false); + } + + public String getInstructions() { + return getStringProperty("instructions", + "<b>No instructions available for this round.</b>"); + } + + public boolean shouldDisplayGroupTokens() { + return getBooleanProperty("display-group-tokens"); + } + + public boolean isQuizEnabled() { + return getBooleanProperty("quiz"); + } + + public String getQuizPage() { + return getStringProperty("quiz-page"); + } + + public Map<String, String> getQuizAnswers() { + if (isQuizEnabled()) { + Properties properties = getProperties(); + Map<String, String> answers = new HashMap<String, String>(); + for (int i = 1; properties.containsKey("q" + i); i++) { + String key = "q" + i; + String answer = properties.getProperty(key); + answers.put(key, answer); + } + return answers; + } + return Collections.emptyMap(); + } + + /** + * Returns true if the current round should have a communication session for + * getChatDuration() seconds before the round begins. + * + * @return + */ + public boolean isChatEnabledBeforeRound() { + return getBooleanProperty("chat-enabled-before-round", true); + } + + + /** + * Returns the duration of the round in seconds. Set to default of 50 seconds per round. + */ + @Override + public Duration getRoundDuration() { + return Duration.create(getRoundDurationInSeconds()); + } + + public int getRoundDurationInSeconds() { + return getIntProperty("round-duration", 50); + } + + public boolean shouldRandomizeGroup() { + return getBooleanProperty("randomize-groups", false); + } + } - + + } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |