[virtualcommons-svn] SF.net SVN: virtualcommons:[310] irrigation/trunk/src/main/java/edu/asu/ commo
Status: Beta
Brought to you by:
alllee
From: <al...@us...> - 2009-10-20 09:58:38
|
Revision: 310 http://virtualcommons.svn.sourceforge.net/virtualcommons/?rev=310&view=rev Author: alllee Date: 2009-10-20 09:58:30 +0000 (Tue, 20 Oct 2009) Log Message: ----------- irrigation game panel is almost complete. It's ugly as hell though, layout needs a lot of work. Modified Paths: -------------- irrigation/trunk/src/main/java/edu/asu/commons/irrigation/client/CanalPanel.java irrigation/trunk/src/main/java/edu/asu/commons/irrigation/client/ClientDataModel.java irrigation/trunk/src/main/java/edu/asu/commons/irrigation/client/ExperimentGameWindow.java irrigation/trunk/src/main/java/edu/asu/commons/irrigation/client/IrrigationClient.java irrigation/trunk/src/main/java/edu/asu/commons/irrigation/client/IrrigationGamePanel.java irrigation/trunk/src/main/java/edu/asu/commons/irrigation/client/IrrigationGameWindow.java irrigation/trunk/src/main/java/edu/asu/commons/irrigation/client/MiddleScorePanel.java irrigation/trunk/src/main/java/edu/asu/commons/irrigation/client/ScoreBoxPanel.java irrigation/trunk/src/main/java/edu/asu/commons/irrigation/conf/RoundConfiguration.java irrigation/trunk/src/main/java/edu/asu/commons/irrigation/server/ClientData.java irrigation/trunk/src/main/java/edu/asu/commons/irrigation/server/GroupDataModel.java irrigation/trunk/src/main/java/edu/asu/commons/irrigation/server/IrrigationServer.java Modified: irrigation/trunk/src/main/java/edu/asu/commons/irrigation/client/CanalPanel.java =================================================================== --- irrigation/trunk/src/main/java/edu/asu/commons/irrigation/client/CanalPanel.java 2009-10-20 06:07:13 UTC (rev 309) +++ irrigation/trunk/src/main/java/edu/asu/commons/irrigation/client/CanalPanel.java 2009-10-20 09:58:30 UTC (rev 310) @@ -1,9 +1,9 @@ package edu.asu.commons.irrigation.client; import java.awt.Color; +import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; -import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; @@ -73,8 +73,7 @@ * @return void */ private void initialize() { - this.setSize(1098,150); - this.setLayout(new GridBagLayout()); + this.setPreferredSize(new Dimension(1098,150)); this.setBackground(Color.white); //initializing the constructor for Gates for(int i=0;i<numberofGates ;i++){ Modified: irrigation/trunk/src/main/java/edu/asu/commons/irrigation/client/ClientDataModel.java =================================================================== --- irrigation/trunk/src/main/java/edu/asu/commons/irrigation/client/ClientDataModel.java 2009-10-20 06:07:13 UTC (rev 309) +++ irrigation/trunk/src/main/java/edu/asu/commons/irrigation/client/ClientDataModel.java 2009-10-20 09:58:30 UTC (rev 310) @@ -1,6 +1,8 @@ package edu.asu.commons.irrigation.client; import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; import java.util.List; import java.util.Map; @@ -98,6 +100,17 @@ public Map<Identifier, ClientData> getClientDataMap() { return groupDataModel.getClientDataMap(); } + + public List<ClientData> getClientDataSortedByPriority() { + ArrayList<ClientData> clientDataList = new ArrayList<ClientData>(getGroupDataModel().getClientDataMap().values()); + // sort by position. + Collections.sort(clientDataList, new Comparator<ClientData>() { + public int compare(ClientData a, ClientData b) { + return new Integer(a.getPriority()).compareTo(b.getPriority()); + } + }); + return clientDataList; + } } Modified: irrigation/trunk/src/main/java/edu/asu/commons/irrigation/client/ExperimentGameWindow.java =================================================================== --- irrigation/trunk/src/main/java/edu/asu/commons/irrigation/client/ExperimentGameWindow.java 2009-10-20 06:07:13 UTC (rev 309) +++ irrigation/trunk/src/main/java/edu/asu/commons/irrigation/client/ExperimentGameWindow.java 2009-10-20 09:58:30 UTC (rev 310) @@ -383,7 +383,7 @@ Runnable runnable = new Runnable() { public void run() { addCenterComponent(irrigationGamePanel); - requestFocusInWindow(); + irrigationGamePanel.startRound(); } }; SwingUtilities.invokeLater(runnable); Modified: irrigation/trunk/src/main/java/edu/asu/commons/irrigation/client/IrrigationClient.java =================================================================== --- irrigation/trunk/src/main/java/edu/asu/commons/irrigation/client/IrrigationClient.java 2009-10-20 06:07:13 UTC (rev 309) +++ irrigation/trunk/src/main/java/edu/asu/commons/irrigation/client/IrrigationClient.java 2009-10-20 09:58:30 UTC (rev 310) @@ -137,9 +137,8 @@ transmit(openGateEvent); } - public void closeGate(String fileNo) { + public void closeGate() { CloseGateEvent closeGateEvent = new CloseGateEvent(getId()); - closeGateEvent.setFileNumber(fileNo); transmit(closeGateEvent); } Modified: irrigation/trunk/src/main/java/edu/asu/commons/irrigation/client/IrrigationGamePanel.java =================================================================== --- irrigation/trunk/src/main/java/edu/asu/commons/irrigation/client/IrrigationGamePanel.java 2009-10-20 06:07:13 UTC (rev 309) +++ irrigation/trunk/src/main/java/edu/asu/commons/irrigation/client/IrrigationGamePanel.java 2009-10-20 09:58:30 UTC (rev 310) @@ -4,9 +4,14 @@ import java.awt.Color; import java.awt.Dimension; import java.awt.Font; +import java.awt.GridLayout; import java.awt.Rectangle; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; import java.lang.reflect.InvocationTargetException; +import javax.swing.BoxLayout; +import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JProgressBar; @@ -39,7 +44,7 @@ //this contains the upstream and downstream Panel private JPanel jPanelUpStreamWindow = null; private JPanel jPanelDownStreamWindow = null; - private JPanel downloadScreenPanel = null; + private JPanel mainIrrigationPanel = null; private ScoreBoxPanel scoreBoxPanel; @@ -47,26 +52,44 @@ public Dimension screenSize; - public JProgressBar timeRemainingProgressBar = null; + public JProgressBar timeRemainingProgressBar; - private JLabel timeRemainingLabel = null; + private JLabel timeRemainingLabel; private JTextField timeRemainingTextField; - private JLabel priorityjLabel = null; + private JButton gateSwitchButton; - private JLabel totalAvailableFlowCapacityLabel = null; + private JLabel irrigationCapacityLabel; - private JLabel maximumAvailableFlowCapacityLabel = null; + // private JLabel maximumAvailableFlowCapacityLabel = null; private ClientDataModel clientDataModel; // @jve:decl-index=0: private MiddleWindowPanel middleWindowPanel; - private JLabel dashBoardLabel = null; + // private JLabel dashBoardLabel = null; - private JLabel scoreBoardLabel = null; + // private JLabel scoreBoardLabel = null; + private JLabel totalTokensEarnedLabel; + + private JLabel tokensEarnedLabel; + + private JLabel tokensNotInvestedLabel; + + private JLabel waterUsedLabel; + + private JTextField waterUsedTextField; + + private JTextField tokensNotInvestedTextField; + + private JTextField tokensEarnedTextField; + + private JTextField totalTokensEarnedTextField; + + private boolean open; + public IrrigationGamePanel(Dimension screenSize, IrrigationClient client) { setName("Irrigation Game Panel"); this.screenSize = screenSize; @@ -78,68 +101,181 @@ * @return */ private void initGuiComponents() { - timeRemainingLabel = new JLabel(); - timeRemainingLabel.setBounds(new Rectangle(469, 39, 146, 23)); + + + JPanel topmostPanel = new JPanel(); + topmostPanel.setLayout(new BoxLayout(topmostPanel, BoxLayout.X_AXIS)); + topmostPanel.add(getIrrigationCapacityLabel()); + + JPanel timeRemainingPanel = new JPanel(); + timeRemainingPanel.setLayout(new BoxLayout(timeRemainingPanel, BoxLayout.Y_AXIS)); + timeRemainingPanel.add(getTimeRemainingProgressBar()); + timeRemainingPanel.add(getTimeRemainingPanel()); + + topmostPanel.add(timeRemainingPanel); + + + setLayout(new BorderLayout()); + add(topmostPanel, BorderLayout.PAGE_START); + add(getUpperPanel(), BorderLayout.CENTER); + add(getBottomPanel(), BorderLayout.PAGE_END); + } + + private JPanel getBottomPanel() { + JPanel panel = new JPanel(); + panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); + panel.add(getGateSwitchButton()); + + JPanel bottomPanel = new JPanel(); + bottomPanel.setLayout(new GridLayout(4, 2)); + bottomPanel.add(getWaterUsedLabel()); + bottomPanel.add(getWaterUsedTextField()); + bottomPanel.add(getTokensNotInvestedLabel()); + bottomPanel.add(getTokensNotInvestedTextField()); + bottomPanel.add(getTokensEarnedLabel()); + bottomPanel.add(getTokensEarnedTextField()); + bottomPanel.add(getTotalTokensEarnedLabel()); + bottomPanel.add(getTotalTokensEarnedTextField()); + + panel.add(bottomPanel); + return panel; + } + + private JTextField getWaterUsedTextField() { + if (waterUsedTextField == null) { + waterUsedTextField = new JTextField(); + waterUsedTextField.setEditable(false); + } + return waterUsedTextField; + } + + private JLabel getWaterUsedLabel() { + if (waterUsedLabel == null) { + waterUsedLabel = new JLabel("Water units used: "); + } + return waterUsedLabel; + } + + private JTextField getTokensNotInvestedTextField() { + if (tokensNotInvestedTextField == null) { + tokensNotInvestedTextField = new JTextField(); + tokensNotInvestedTextField.setEditable(false); + } + return tokensNotInvestedTextField; + } + private JLabel getTokensNotInvestedLabel() { + if (tokensNotInvestedLabel == null) { + tokensNotInvestedLabel = new JLabel("Tokens not invested: "); + } + return tokensNotInvestedLabel; + } + + private JTextField getTokensEarnedTextField() { + if (tokensEarnedTextField == null) { + tokensEarnedTextField = new JTextField(); + tokensEarnedTextField.setEditable(false); + } + return tokensEarnedTextField; + } + private JLabel getTokensEarnedLabel() { + if (tokensEarnedLabel == null) { + tokensEarnedLabel = new JLabel("Tokens earned by crop production: "); + } + return tokensEarnedLabel; + } + + private JTextField getTotalTokensEarnedTextField() { + if (totalTokensEarnedTextField == null) { + totalTokensEarnedTextField = new JTextField(); + totalTokensEarnedTextField.setEditable(false); + } + return totalTokensEarnedTextField; + + } + + private JLabel getTotalTokensEarnedLabel() { + if (totalTokensEarnedLabel == null) { + totalTokensEarnedLabel = new JLabel("Total tokens earned this round: "); + } + return totalTokensEarnedLabel; + } + + private JPanel getTimeRemainingPanel() { + timeRemainingLabel = new JLabel("Time left: "); timeRemainingLabel.setHorizontalAlignment(SwingConstants.CENTER); timeRemainingLabel.setHorizontalTextPosition(SwingConstants.CENTER); - timeRemainingLabel.setText("Time Left"); - timeRemainingTextField = new JTextField(); + timeRemainingTextField = new JTextField("50 sec"); timeRemainingTextField.setEditable(false); - // timeRemainingTextField.setBounds(new Rectangle(620, 39, 61); - timeRemainingTextField.setText("50 sec"); timeRemainingTextField.setBackground(Color.white); timeRemainingTextField.setFont(new Font("serif", Font.BOLD, 14)); timeRemainingTextField.setForeground(new Color(102, 204, 255)); timeRemainingTextField.setHorizontalAlignment(SwingConstants.CENTER); - setLayout(new BorderLayout()); - add(getDownloadScreenPanel(), BorderLayout.CENTER); + JPanel panel = new JPanel(); + panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS)); + panel.add(timeRemainingLabel); + panel.add(timeRemainingTextField); + return panel; } - private JPanel getDownloadScreenPanel() { - if(downloadScreenPanel == null){ - scoreBoardLabel = new JLabel(); - scoreBoardLabel.setBounds(new Rectangle(582,225+100+35,530,20)); - scoreBoardLabel.setHorizontalAlignment(SwingConstants.CENTER); - scoreBoardLabel.setText("SUMMARY SCOREBOARD FOR YOU AND OTHER PLAYERS"); - dashBoardLabel = new JLabel(); - dashBoardLabel.setBounds(new Rectangle(13,225+100+35,530,20)); - dashBoardLabel.setHorizontalAlignment(SwingConstants.CENTER); - dashBoardLabel.setText("YOUR DASHBOARD"); - maximumAvailableFlowCapacityLabel = new JLabel(); - maximumAvailableFlowCapacityLabel.setBounds(new Rectangle(200, 18, 55, 17)); - maximumAvailableFlowCapacityLabel.setText(""); - totalAvailableFlowCapacityLabel = new JLabel(); - totalAvailableFlowCapacityLabel.setBounds(new Rectangle(19, 17, 170, 16)); - totalAvailableFlowCapacityLabel.setText("Irrigation capacity: "); - priorityjLabel = new JLabel(); - priorityjLabel.setBounds(new Rectangle(780, 16, 44, 16)); - priorityjLabel.setText(""); - downloadScreenPanel = new JPanel(); - downloadScreenPanel.setLayout(null); - downloadScreenPanel.setName("downloadScreenPanel"); - downloadScreenPanel.setBackground(Color.white); - downloadScreenPanel.add(getUpperPanel(),null); - downloadScreenPanel.add(getTimeRemainingProgressBar(), null); - downloadScreenPanel.add(timeRemainingLabel, null); - downloadScreenPanel.add(timeRemainingTextField,null); - - downloadScreenPanel.add(totalAvailableFlowCapacityLabel, null); - downloadScreenPanel.add(maximumAvailableFlowCapacityLabel, null); - downloadScreenPanel.add(dashBoardLabel, null); - downloadScreenPanel.add(scoreBoardLabel, null); + private JLabel getIrrigationCapacityLabel() { + if (irrigationCapacityLabel == null) { + irrigationCapacityLabel = new JLabel("Irrigation capacity: "); } - return downloadScreenPanel; + return irrigationCapacityLabel; } + // private JPanel getMainIrrigationPanel() { + // if (mainIrrigationPanel == null) { + // irrigationCapacityLabel = new JLabel(); + // irrigationCapacityLabel.setText("Irrigation capacity: "); + // mainIrrigationPanel = new JPanel(); + // + // + // mainIrrigationPanel.setName("downloadScreenPanel"); + // mainIrrigationPanel.setBackground(Color.white); + // mainIrrigationPanel.add(getUpperPanel(),null); + // mainIrrigationPanel.add(timeRemainingLabel, null); + // mainIrrigationPanel.add(timeRemainingTextField,null); + // + // mainIrrigationPanel.add(irrigationCapacityLabel, null); + // mainIrrigationPanel.add(maximumAvailableFlowCapacityLabel, null); + // mainIrrigationPanel.add(dashBoardLabel, null); + // mainIrrigationPanel.add(scoreBoardLabel, null); + // } + // return mainIrrigationPanel; + // } + private JPanel getUpperPanel() { if (upperPanel == null) { upperPanel = new JPanel(); upperPanel.setLayout(new BorderLayout()); + upperPanel.add(getScoreBoxPanel(), BorderLayout.PAGE_END); } return upperPanel; } + + private JButton getGateSwitchButton() { + if (gateSwitchButton == null) { + gateSwitchButton = new JButton("Open Gate"); + gateSwitchButton.setPreferredSize(new Dimension(60, 60)); + gateSwitchButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent event) { + open = !open; + gateSwitchButton.setText( open ? "Close gate" : "Open gate"); + if (open) { + client.openGate(); + } + else { + client.closeGate(); + } + } + }); + } + return gateSwitchButton; + } + private JPanel getCanalPanel() { canalPanel = new CanalPanel(clientDataModel); return canalPanel; @@ -185,9 +321,7 @@ private ScoreBoxPanel getScoreBoxPanel() { if(scoreBoxPanel == null){ - scoreBoxPanel = new ScoreBoxPanel(client); - - return scoreBoxPanel; + scoreBoxPanel = new ScoreBoxPanel(); } return scoreBoxPanel; } @@ -201,7 +335,6 @@ private JProgressBar getTimeRemainingProgressBar() { if (timeRemainingProgressBar == null) { timeRemainingProgressBar = new JProgressBar(0, 50); - timeRemainingProgressBar.setBounds(new Rectangle(360, 15, 370, 17)); timeRemainingProgressBar.setBackground(Color.white); timeRemainingProgressBar.setForeground(new Color(51, 153, 255)); } @@ -212,35 +345,32 @@ * Shoudl be invoked every second throughout the experiment, from a ClientUpdateEvent sent by the server. */ public void updateClientStatus(final ClientDataModel clientDataModel) { - ////////////new code//////////////////////////////////////////////////////////// Runnable createGuiRunnable = new Runnable(){ public void run() { + int timeLeft = clientDataModel.getTimeLeft(); + int irrigationCapacity = clientDataModel.getGroupDataModel().getMaximumAvailableFlowCapacity(); + timeRemainingTextField.setText( timeLeft + " sec"); + timeRemainingProgressBar.setValue( timeLeft ); + irrigationCapacityLabel.setText( + String.format("Irrigation capacity: %d cubic feet per second (cfps)", + irrigationCapacity)); for(final ClientData clientData : clientDataModel.getClientDataMap().values()){ - timeRemainingTextField.setText( clientDataModel.getTimeLeft() +" second(s)" ); - timeRemainingProgressBar.setValue( (clientDataModel.getTimeLeft() / 1000) ); - maximumAvailableFlowCapacityLabel.setText( - clientData.getGroupDataModel().getMaximumAvailableFlowCapacity() +" cubic feet per second (cfps)" - ); - middleWindowPanel.update(clientData); - if (clientDataModel.getPriority() == clientData.getPriority()) { - irrigationGameWindow.update(clientData); - //per parameter score panel - scoreBoxPanel.update(clientData); - } - else{ - scoreBoxPanel.update(clientData); - } - if(clientData.isGateOpen()){ + if (clientData.isGateOpen()) { canalPanel.openGates(clientData.getPriority()); - } else if(clientData.isPaused()){ canalPanel.closeGates(clientData.getPriority()); } - if(clientData.isGateClosed()){ + else if(clientData.isGateClosed()){ canalPanel.closeGates(clientData.getPriority()); } } + ClientData clientData = clientDataModel.getClientData(); + waterUsedTextField.setText("" + clientData.getWaterUsed()); + tokensNotInvestedTextField.setText("" + clientData.getUninvestedTokens()); + tokensEarnedTextField.setText("" + clientData.waterToTokenFunction()); + totalTokensEarnedTextField.setText("" + clientData.getTotalTokensEarned()); + scoreBoxPanel.update(clientDataModel); } }; @@ -293,21 +423,21 @@ //irrigation window as the value and the priority value as the key. This is handled here , because //the event contains the priority for every client, and the priority can act as a unique key for the //game window. - fillPanels(); - } - /** * fills in the panels depending on the priority of the client */ - private void fillPanels() { + public void startRound() { upperPanel.add(getCanalPanel(), BorderLayout.CENTER); - downloadScreenPanel.add(getJPanelUpStreamWindow(),null); - downloadScreenPanel.add(getJPanelDownStreamWindow(),null); + open = false; + scoreBoxPanel.initialize(clientDataModel); + revalidate(); + // mainIrrigationPanel.add(getJPanelUpStreamWindow(),null); + // mainIrrigationPanel.add(getJPanelDownStreamWindow(),null); //adding the in between Panel - downloadScreenPanel.add(getJPanelMiddleWindow(),null); + // mainIrrigationPanel.add(getJPanelMiddleWindow(),null); } private JPanel getJPanelMiddleWindow() { @@ -320,9 +450,8 @@ return middleWindowPanel; } - public ClientDataModel getClientGameState() { + public ClientDataModel getClientDataModel() { return clientDataModel; } } -// @jve:decl-index=0:visual-constraint="10,-43" Modified: irrigation/trunk/src/main/java/edu/asu/commons/irrigation/client/IrrigationGameWindow.java =================================================================== --- irrigation/trunk/src/main/java/edu/asu/commons/irrigation/client/IrrigationGameWindow.java 2009-10-20 06:07:13 UTC (rev 309) +++ irrigation/trunk/src/main/java/edu/asu/commons/irrigation/client/IrrigationGameWindow.java 2009-10-20 09:58:30 UTC (rev 310) @@ -544,7 +544,7 @@ if (moneyEarnedTextField == null) { moneyEarnedTextField = new JTextField(); moneyEarnedTextField.setText(new Integer(mainIrrigationWindow - .getClientGameState().getClientData().getTotalTokensEarned()) + .getClientDataModel().getClientData().getTotalTokensEarned()) .toString()); moneyEarnedTextField.setBounds(new Rectangle(161, 17, 86, 21)); moneyEarnedTextField.setFont(new Font("Dialog", Font.PLAIN, 14)); @@ -635,8 +635,9 @@ * clientData.getAvailableBandwidth()).toString()); } */ - fileDownloadedProgressBar.setValue((int) clientData.getPercentFileDownload()); +// fileDownloadedProgressBar.setValue((int) clientData.getPercentFileDownload()); // checks whether the file has been downlaoded + /* if (clientData.isFileDownloaded()) { moneyEarnedTextField.setText(new Integer(clientData.getTotalTokensEarned()) Modified: irrigation/trunk/src/main/java/edu/asu/commons/irrigation/client/MiddleScorePanel.java =================================================================== --- irrigation/trunk/src/main/java/edu/asu/commons/irrigation/client/MiddleScorePanel.java 2009-10-20 06:07:13 UTC (rev 309) +++ irrigation/trunk/src/main/java/edu/asu/commons/irrigation/client/MiddleScorePanel.java 2009-10-20 09:58:30 UTC (rev 310) @@ -148,7 +148,7 @@ else currentBandwidthText.setText(new Double(clientData.getAvailableFlowCapacity()).toString()); - filesDownloadedText.setText(new Integer(clientData.getCropsGrown()).toString()); + filesDownloadedText.setText(new Integer(clientData.getWaterUsed()).toString()); tokensText.setText(new Integer(clientData.getTotalTokensEarned()).toString()); if(clientData.isGateOpen() && clientData.getAvailableFlowCapacity() > 0) { Modified: irrigation/trunk/src/main/java/edu/asu/commons/irrigation/client/ScoreBoxPanel.java =================================================================== --- irrigation/trunk/src/main/java/edu/asu/commons/irrigation/client/ScoreBoxPanel.java 2009-10-20 06:07:13 UTC (rev 309) +++ irrigation/trunk/src/main/java/edu/asu/commons/irrigation/client/ScoreBoxPanel.java 2009-10-20 09:58:30 UTC (rev 310) @@ -1,17 +1,19 @@ package edu.asu.commons.irrigation.client; -import java.awt.Dimension; import java.awt.GridLayout; -import java.util.LinkedHashMap; -import java.util.Map; +import java.util.ArrayList; +import java.util.List; +import javax.swing.JLabel; import javax.swing.JPanel; import edu.asu.commons.irrigation.server.ClientData; +import edu.asu.commons.irrigation.server.GroupDataModel; /** * $Id$ - * + * + * Panel displaying data for all participants. * * @author <a href='mailto:All...@as...'>Allen Lee</a> * @version $Rev$ @@ -19,42 +21,54 @@ public class ScoreBoxPanel extends JPanel { private static final long serialVersionUID = 4724679308719540371L; - //creating a Linked Hash Map for the activity Panel Screens - //Map<Integer, ActivitySummaryPanel>activitySummaryPanelMap = new LinkedHashMap<Integer, ActivitySummaryPanel>(); - Map<Integer, SummaryPanel>activitySummaryPanelMap = new LinkedHashMap<Integer, SummaryPanel>(); - - IrrigationClient client; - int NUMBER_PARAMETERS = 3; + private List<JLabel> availableWaterLabels = new ArrayList<JLabel>(); - public ScoreBoxPanel(IrrigationClient client){ - super(); - this.client = client; - initialize(); + private List<JLabel> waterUsedLabels = new ArrayList<JLabel>(); + + private void clear() { + availableWaterLabels.clear(); + waterUsedLabels.clear(); } - - private void initialize() { - // TODO Auto-generated method stub - GridLayout gridLayout = new GridLayout(); - gridLayout.setColumns(2); - gridLayout.setRows(2); - this.setSize(new Dimension(530,326)); - this.setLayout(gridLayout); - - //add the new activity panel that gives information per Parameter - for(int i=0;i<NUMBER_PARAMETERS;i++){ - //get the activity panels per parameter - SummaryPanel activitySummaryPanelNew = new SummaryPanel(i,client); - activitySummaryPanelMap.put(new Integer(i), activitySummaryPanelNew); - this.add(activitySummaryPanelNew); + + public void initialize(ClientDataModel clientDataModel) { + clear(); + // number of clients + 1 for the labels + GroupDataModel groupDataModel = clientDataModel.getGroupDataModel(); + int columns = groupDataModel.size() + 1; + GridLayout gridLayout = new GridLayout(3, columns); + setLayout(gridLayout); + + add(new JLabel("Position")); + List<ClientData> clientDataList = clientDataModel.getClientDataSortedByPriority(); + for (ClientData clientData : clientDataList) { + add(new JLabel(clientData.getPriorityAsString())); } + // available water per second + add(new JLabel("Available water per second")); + for (ClientData clientData: clientDataList) { + JLabel availableWaterLabel = new JLabel("" + clientData.getAvailableFlowCapacity()); + availableWaterLabels.add(availableWaterLabel); + add(availableWaterLabel); + } + // water used + add(new JLabel("Water used")); + for (ClientData clientData : clientDataList) { + JLabel waterUsedLabel = new JLabel("" + clientData.getWaterUsed()); + waterUsedLabels.add(waterUsedLabel); + add(waterUsedLabel); + } } - public void update(ClientData clientData){ - //update specific panel if activity Summary panel is being used, else update all the panels. - //activitySummaryPanelMap.get(new Integer(clientData.getPriority())).update(clientData); - for(int i=0;i<NUMBER_PARAMETERS;i++){ - activitySummaryPanelMap.get(new Integer(i)).update(clientData); + public void update(ClientDataModel clientDataModel) { + List<ClientData> clientDataList = clientDataModel.getClientDataSortedByPriority(); + for (int index = 0; index < clientDataList.size(); index++) { + ClientData clientData = clientDataList.get(index); + JLabel availableWaterLabel = availableWaterLabels.get(index); + availableWaterLabel.setText("" + clientData.getAvailableFlowCapacity()); + JLabel waterUsedLabel = waterUsedLabels.get(index); + waterUsedLabel.setText("" + clientData.getWaterUsed()); } + } } Modified: irrigation/trunk/src/main/java/edu/asu/commons/irrigation/conf/RoundConfiguration.java =================================================================== --- irrigation/trunk/src/main/java/edu/asu/commons/irrigation/conf/RoundConfiguration.java 2009-10-20 06:07:13 UTC (rev 309) +++ irrigation/trunk/src/main/java/edu/asu/commons/irrigation/conf/RoundConfiguration.java 2009-10-20 09:58:30 UTC (rev 310) @@ -63,10 +63,6 @@ return getIntProperty("max-infrastructure-efficiency", 100); } - public double getMaximumIndividualFlowCapacity() { - return getDoubleProperty("max-individual-flow-capacity", 20.0d); - } - public boolean isPracticeRound() { return getBooleanProperty("practice-round"); } Modified: irrigation/trunk/src/main/java/edu/asu/commons/irrigation/server/ClientData.java =================================================================== --- irrigation/trunk/src/main/java/edu/asu/commons/irrigation/server/ClientData.java 2009-10-20 06:07:13 UTC (rev 309) +++ irrigation/trunk/src/main/java/edu/asu/commons/irrigation/server/ClientData.java 2009-10-20 09:58:30 UTC (rev 310) @@ -30,27 +30,10 @@ private GroupDataModel groupDataModel; - private String fileNumber; - - private List<String> filesDownloadedList = new ArrayList<String>(); - - private boolean fileDownloaded = false; + private int waterUsed = 0; - private int fileSize; + private int availableFlowCapacity; - private double percentFileDownloaded = 0; - - private double downloadedFileSize = 0; - - private int cropsGrown = 0; - - // maximum available bandwidth - private double maximumIndividualFlowCapacity; - /** - * current download speed - */ - private double availableFlowCapacity = 25.0d; - private int investedTokens; private int assignedNumber; @@ -59,8 +42,6 @@ private int totalTokens; - private int tokensCollectedLastRound; - private boolean paused = false; private boolean gateOpen = false; @@ -80,29 +61,15 @@ public boolean isGateOpen(){ return gateOpen; } - - // cap on the size of the entire stream (e.g,. 50 kbps) - public double getMaximumAvailableBandwidth(){ - return groupDataModel.getMaximumAvailableFlowCapacity(); - } - // cap on the size of the stream that can be delivered to the client (e.g., 25 kbps) - public void setMaximumIndividualFlowCapacity(double deliveryBandwidth){ - this.maximumIndividualFlowCapacity = deliveryBandwidth; - } - - public double getMaximumIndividualFlowCapacity(){ - return maximumIndividualFlowCapacity; - } - /** * The actual bandwidth allocated to this client. */ - public void setAvailableFlowCapacity(double availableFlowCapacity){ + public void setAvailableFlowCapacity(int availableFlowCapacity) { this.availableFlowCapacity = availableFlowCapacity; } - public double getAvailableFlowCapacity(){ + public int getAvailableFlowCapacity(){ return availableFlowCapacity; } @@ -120,23 +87,7 @@ } return "Position not found"; } - - /** - * set and get the current file_no from the client.The corresponding file sizes and their numbers - * are listed down in the configuration file. Thus I can get their sizes. - * - */ - public void setFileNumber(String fileNumber){ - this.fileNumber = fileNumber; - } - public String getFileNumber() { - return fileNumber; - } - - public double getPercentFileDownload(){ - return percentFileDownloaded; - } /** * here the parameters isPaused, isStopped and isStartDownload would change. * The server every time would check the clients queue. It would be a queue of clients requests. @@ -149,7 +100,7 @@ paused = false; } - public void setPaused(){ + public void pause(){ paused = true; gateOpen = false; } @@ -197,17 +148,15 @@ * clearing the clientData at the end of each round * */ - public void reset() { - resetFileInformation(); + public void endRound() { closeGate(); investedTokens = 0; //adding number of files to be downloaded = 0 per round - cropsGrown = 0; - filesDownloadedList.clear(); + waterUsed = 0; } public void resetAllTokens() { - reset(); + endRound(); totalTokens = 0; } @@ -223,9 +172,6 @@ return totalTokens; } - public int getTokensCollectedLastRound() { - return tokensCollectedLastRound; - } public RoundConfiguration getRoundConfiguration(){ return roundConfiguration; @@ -234,21 +180,11 @@ * This would initialize the clientData before the start of each download. * */ - public void init(double availableFlowCapacity) { - resetFileInformation(); - maximumIndividualFlowCapacity = getRoundConfiguration().getMaximumIndividualFlowCapacity(); - //currentBandwidth = totalContributedBandwidth; - this.availableFlowCapacity = availableFlowCapacity; + public void init(int availableFlowCapacity) { + closeGate(); + setAvailableFlowCapacity(availableFlowCapacity); } - private void resetFileInformation() { - fileSize = 0; - fileNumber = ""; - downloadedFileSize = 0; - percentFileDownloaded = 0; - closeGate(); - } - public void award() { totalTokens += getTotalTokensEarned(); } @@ -258,102 +194,71 @@ * @return */ public int getTotalTokensEarned(){ - return calculateTokensEarned() + (getRoundConfiguration().getMaximumInvestedTokens() - investedTokens); + return waterToTokenFunction() + getUninvestedTokens(); } /** - * complex functions can be defined here + * Reward function table correlating water usage to tokens earned. + * * @param value * @return */ - - private int calculateTokensEarned() { - switch (cropsGrown) { - case 0: case 1: + public int waterToTokenFunction() { + if (waterUsed < 150) { return 0; - case 2: - return 6; - case 3: - return 22; - case 4: - return 28; - case 5: - return 30; - default: - throw new IllegalStateException("Should be a number between 0-5, was " + cropsGrown + " instead."); } -// double alpha,b,a,num,deno; -// /** -// * This would be taken by a proper configuration file -// */ -// alpha = getRoundConfiguration().getAlphaAward(); -// a = getRoundConfiguration().getA_award(); -// b = getRoundConfiguration().getB_award(); -// num = alpha * Math.pow((double)filesDownloaded,b); -// deno = Math.pow(a,b)+Math.pow(filesDownloaded,b); -// -// /** -// * until u get the final equation, you can go ahead with -// * these values.. -// */ -// return (int)(Math.round(num/deno)); - //return no_fileDownloaded; + else if (waterUsed < 200) { + return 1; + } + else if (waterUsed < 250) { + return 4; + } + else if (waterUsed < 300) { + return 10; + } + else if (waterUsed < 350) { + return 15; + } + else if (waterUsed < 400) { + return 18; + } + else if (waterUsed < 500) { + return 19; + } + else if (waterUsed < 550) { + return 20; + } + else if (waterUsed < 650) { + return 19; + } + else if (waterUsed < 700) { + return 18; + } + else if (waterUsed < 750) { + return 15; + } + else if (waterUsed < 800) { + return 10; + } + else if (waterUsed < 850) { + return 4; + } + else if (waterUsed < 900) { + return 1; + } + else { + return 0; + } } - /** - * calculates the percent of the file downloaded - * - */ - /** - *Here check whether the percentageFile > 100, then init the file download - *else simply increment the filesize. If we increment the file size and then - *check whether the filesize > 100 and then invoke completedownload, that information - *is not send to the client, as the sending file progress events are in the same loop - *as processDownload function - */ - public void allocateFlowCapacity(double availableBandwidth) { - assert gateOpen; - fileDownloaded = false; - incrementFileDownloadSize(); + + public void collectWater() { + waterUsed += availableFlowCapacity; } - private void completeFileDownload() { - cropsGrown++; - fileDownloaded = true; - filesDownloadedList.add(fileNumber); - groupDataModel.getEventChannel().handle(new FileDownloadedEvent(id, fileNumber)); - resetFileInformation(); - //init(availableBandwidth); + public int getWaterUsed(){ + return waterUsed; } - - /** - * This would increament the percentage of the file downloaded - * by Bi - * - */ - public void incrementFileDownloadSize(){ - downloadedFileSize += availableFlowCapacity; - percentFileDownloaded = (downloadedFileSize/fileSize)*100; - if (percentFileDownloaded >= 100) { - //filesDownloaded++; - percentFileDownloaded = 100; - completeFileDownload(); - } - - } - public int getCropsGrown(){ - return cropsGrown; - } - - public boolean isFileDownloaded() { - return fileDownloaded; - } - - public List<String> getDownloadListArray() { - return filesDownloadedList; - } - - } Modified: irrigation/trunk/src/main/java/edu/asu/commons/irrigation/server/GroupDataModel.java =================================================================== --- irrigation/trunk/src/main/java/edu/asu/commons/irrigation/server/GroupDataModel.java 2009-10-20 06:07:13 UTC (rev 309) +++ irrigation/trunk/src/main/java/edu/asu/commons/irrigation/server/GroupDataModel.java 2009-10-20 09:58:30 UTC (rev 310) @@ -70,6 +70,10 @@ clients.put(clientData.getId(), clientData); clientData.setAssignedNumber(clients.size()); } + + public int size() { + return clients.size(); + } public void removeClient(Identifier id) { clients.remove(id); @@ -99,14 +103,13 @@ return serverDataModel.getRoundConfiguration(); } - //get totalContributed tokens for this round public int getTotalContributedTokens() { return totalContributedTokens; } public void calculateTotalFlowCapacity() { // for practice round and first round, initialize to initial infrastructure efficiency - //setting the total contributed Bandwidth = 0 , so thatfor every round, + //setting the total contributed Bandwidth = 0 , so that for every round, // fresh totalContributed tokens are calculated totalContributedTokens = 0; for(ClientData clientData : getClientDataMap().values()) { @@ -194,16 +197,17 @@ } public void allocateFlowCapacity(ClientData clientData) { - if (currentlyAvailableFlowCapacity >= clientData.getMaximumIndividualFlowCapacity()) { - currentlyAvailableFlowCapacity -= clientData.getMaximumIndividualFlowCapacity(); -// setAvailableBandwidth(clientData.getTotalContributedBandwidth() - clientData.getDeliveryBandwidth()); - clientData.setAvailableFlowCapacity(clientData.getMaximumIndividualFlowCapacity()); + RoundConfiguration roundConfiguration = getRoundConfiguration(); + int maximumClientFlowCapacity = roundConfiguration.getMaximumClientFlowCapacity(); + if (currentlyAvailableFlowCapacity >= maximumClientFlowCapacity) { + currentlyAvailableFlowCapacity -= maximumClientFlowCapacity; + clientData.setAvailableFlowCapacity(maximumClientFlowCapacity); } else { clientData.setAvailableFlowCapacity(currentlyAvailableFlowCapacity); currentlyAvailableFlowCapacity = 0; } - clientData.allocateFlowCapacity(currentlyAvailableFlowCapacity); + clientData.collectWater(); } Modified: irrigation/trunk/src/main/java/edu/asu/commons/irrigation/server/IrrigationServer.java =================================================================== --- irrigation/trunk/src/main/java/edu/asu/commons/irrigation/server/IrrigationServer.java 2009-10-20 06:07:13 UTC (rev 309) +++ irrigation/trunk/src/main/java/edu/asu/commons/irrigation/server/IrrigationServer.java 2009-10-20 09:58:30 UTC (rev 310) @@ -259,7 +259,7 @@ }); addEventProcessor(new EventTypeProcessor<PauseRequest>(PauseRequest.class) { public void handle(PauseRequest event) { - clients.get(event.getId()).setPaused(); + clients.get(event.getId()).pause(); } }); } @@ -414,7 +414,7 @@ clientData.resetAllTokens(); } else { - clientData.reset(); + clientData.endRound(); } } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |