[virtualcommons-svn] SF.net SVN: virtualcommons:[294] mentalmodels/trunk
Status: Beta
Brought to you by:
alllee
From: <see...@us...> - 2009-10-12 22:05:19
|
Revision: 294 http://virtualcommons.svn.sourceforge.net/virtualcommons/?rev=294&view=rev Author: seematalele Date: 2009-10-12 22:05:11 +0000 (Mon, 12 Oct 2009) Log Message: ----------- Created Interface PushData.java which provides mechanism for callback, when block timer is over. Able to sent blocks to the clients (Checked using Alert.Show messages in StartGame.swf). Currently the timer is set is 5 sec. (in sendBlock() method of MessageHandler class). Timer thread is created. This thread sleeps for 1 sec. sendblock checks after 1 sec if time for block is over or not. Problems: FisheryExperimentShell is still giving an error for DayByDayDecisionsC.mxml at 178 line. Need to resolve this issue Modified Paths: -------------- mentalmodels/trunk/flex/src/StartGame.mxml mentalmodels/trunk/flex/src/custom/FisheryExperimentCore.mxml mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/GameService.java mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/RoundService.java mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/StartGame.java mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/StudentService.java mentalmodels/trunk/src/main/java/edu/asu/commons/mme/utility/MessageHandler.java Added Paths: ----------- mentalmodels/trunk/src/main/java/edu/asu/commons/mme/utility/PushData.java mentalmodels/trunk/src/main/java/edu/asu/commons/mme/utility/Timer.java Modified: mentalmodels/trunk/flex/src/StartGame.mxml =================================================================== --- mentalmodels/trunk/flex/src/StartGame.mxml 2009-10-10 01:18:43 UTC (rev 293) +++ mentalmodels/trunk/flex/src/StartGame.mxml 2009-10-12 22:05:11 UTC (rev 294) @@ -28,7 +28,7 @@ <mx:VBox id="responsesContent"/> </mx:VBox> - <mx:RemoteObject id="startupService" destination="startupService" fault="faultHandler(event)"> + <mx:RemoteObject id="gameService" destination="gameService" fault="faultHandler(event)"> <mx:method name="createGame" result="gameObjectResultHandler(event)"/> <mx:method name="startGame"/> </mx:RemoteObject> @@ -55,6 +55,7 @@ import mx.messaging.events.MessageFaultEvent; import mx.messaging.FlexClient; import mx.messaging.messages.AsyncMessage; + import mx.collections.ArrayCollection; import mx.logging.*; import mx.logging.targets.*; @@ -111,15 +112,12 @@ gameObject.money = stpMoney.value; gameObject.gameCode = txtDescription.text; gameObject.timestamp = new Date(); - - startupService.createGame(gameObject); + + gameService.createGame(gameObject); /* var message:AsyncMessage = new AsyncMessage(); message.body = "got gameObject" + gameObject.description; producer.send(message); Alert.show("message is send"); */ - - - } public function start():void { @@ -127,7 +125,7 @@ /*consumer.subscribe(); Alert.show("subscribed to destination: " + consumer.destination);*/ - startupService.startGame(game); + gameService.startGame(game); //consumer.subtopic = "hello3"; } @@ -163,15 +161,23 @@ private function messageHandler(event:MessageEvent):void { //Alert.show("message from server came.."); - var block:actionscript.Block = event.message.body as actionscript.Block; + if(event.message.body as actionscript.Block) + { + var block:actionscript.Block = event.message.body as actionscript.Block; //Alert.show("String came from server is: " + block.id + " " + block.description); - if(block == null) + if(block == null) + { + Alert.show("Game is over!!"); + } + else + Alert.show("Block from server is : " + block.description); + } + else if(event.message.body as ArrayCollection) { - Alert.show("Game is over!!"); + Alert.show("something else "); } - else - Alert.show("Block from server is : " + block.description); + } ]]> Modified: mentalmodels/trunk/flex/src/custom/FisheryExperimentCore.mxml =================================================================== --- mentalmodels/trunk/flex/src/custom/FisheryExperimentCore.mxml 2009-10-10 01:18:43 UTC (rev 293) +++ mentalmodels/trunk/flex/src/custom/FisheryExperimentCore.mxml 2009-10-12 22:05:11 UTC (rev 294) @@ -57,7 +57,7 @@ </mx:HBox> - <mx:RemoteObject id="startupService" destination="startupService" fault="faultHandler(event)"> + <mx:RemoteObject id="studentService" destination="studentService" fault="faultHandler(event)"> <mx:method name="createStudent" result="studentResultHandler(event)"/> </mx:RemoteObject> <mx:RemoteObject id="answeringService" destination="answeringService" fault="faultHandler(event)"> @@ -127,7 +127,30 @@ private function messageHandler(event:MessageEvent):void { - setBlock(event.message.body as actionscript.Block); + var msg:IMessage; + + if(event.message.body as actionscript.Block) + { + setBlock(event.message.body as actionscript.Block); + } + else if(event.message.body as ArrayCollection) + { + var students:ArrayCollection = new ArrayCollection(); + students = ArrayCollection(event.message.body); + Alert.show("Student size is: " + students.length); + if(students.length !=0) + { + for(var i:int = 0; i< students.length; i++) + { + if(actionscript.Student(students.getItemAt(i)).id == studentObject.id) + { + studentObject.group = actionscript.Student(students.getItemAt(i)).group; + studentObject.studentNo = actionscript.Student(students.getItemAt(i)).studentNo; + } + } + } + Alert.show("Current Student Information is: " + "\n"+studentObject.id +"\n"+studentObject.gameCode+"\n"+studentObject.studentNo); + } } private function blockResultHandler(event:ResultEvent):void { @@ -472,7 +495,7 @@ newStudent.semester = (obj as SocioDemographicPage).getSemester(); newStudent.gameCode = (obj as SocioDemographicPage).getGameCode(); - startupService.createStudent(newStudent); + studentService.createStudent(newStudent); currentState = "wait"; btnBack.enabled = btnForward.enabled = btnReset.enabled = true; Modified: mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/GameService.java =================================================================== --- mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/GameService.java 2009-10-10 01:18:43 UTC (rev 293) +++ mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/GameService.java 2009-10-12 22:05:11 UTC (rev 294) @@ -35,12 +35,10 @@ import edu.asu.commons.mme.entity.Round; import edu.asu.commons.mme.entity.Student; import edu.asu.commons.mme.utility.MessageHandler; -import flex.messaging.MessageBroker; -import flex.messaging.messages.AsyncMessage; -import flex.messaging.util.UUIDUtils; +import edu.asu.commons.mme.utility.PushData; @Transactional -public class GameService extends Service.Base<Game, HibernateGameDao>{ +public class GameService extends Service.Base<Game, HibernateGameDao> implements PushData{ private HibernateModuleDao moduleDao; @@ -54,8 +52,14 @@ private HibernateLocationDao locationDao; private int blockSeqNo; + + private RoundService roundService; - private RoundService roundService; + MessageHandler msgHandler; + + public GameService() + { + } public GameConfig createGameConfig(GameConfig gameConfig) { @@ -131,66 +135,67 @@ public void startGame(Game game) { assignGroups(game); -/* getRoundService().setGame(game); - getRoundService().run(); -*/ + getLogger().debug("system has assigned groups. "); + pushBlock(game); + } + + + private void pushBlock(Game game) { Game newGameState = null; Block block = null; boolean running = false; getLogger().info("game start is: " + game.getId()); newGameState = getDao().find(game.getId()); - MessageHandler msgHandler = new MessageHandler(); + //FIXME: Do not know if this is a right way, I do not think because it will create refrecne to message handler for every block + msgHandler = new MessageHandler(this); msgHandler.setDestination("mme"); msgHandler.setSubtopic(getGameCode(newGameState)); - int noOfBlocks = getNumberOfBlock(game); - + //int noOfBlocks = getNumberOfBlock(game); + int i=0; try { - while (i<noOfBlocks) { - + //while (i<noOfBlocks) { i++; newGameState = getBlock(game); game = newGameState; if(newGameState == null) { - msgHandler.send(null); + getLogger().debug("game state is null..."); + msgHandler.sendBlock(null, null); } else { + getLogger().debug("game state is NOT null..."); block = newGameState.getCurrentBlock(); - msgHandler.send(block); + msgHandler.sendBlock(block,newGameState); } - if(block.getDescription().equalsIgnoreCase("Day-by-day decisions game")) - { - //start day by day decision - //getDayBydayDecisionService(). + /*if(block.getDescription().equalsIgnoreCase("Day-by-day decisions game")) + { + //start day by day decision + //getDayBydayDecisionService(). - } - else if (block.getDescription().equalsIgnoreCase("Communication")) - { - //start communication round + } + else if (block.getDescription().equalsIgnoreCase("Communication")) + { + //start communication round - } - //get the timer for the block - Integer timer = getTimerforBlock(block); - //start the timer for the block sent - - //for testing purpose 5000 is given as an argument - Thread.sleep(10000); - if(block == null) - { - running = false; - } - } - }catch (InterruptedException e) { + }*/ + //} + }catch (Exception e) { + getLogger().debug(e); } } - + public void pushNextData(Game game) { + // TODO Auto-generated method stub + pushBlock(game); + + } private int getNumberOfBlock(Game game) { // TODO Auto-generated method stub + + //game.getGameConfig() - //game.getGameConfig() return 0; } @@ -203,8 +208,6 @@ Game game = getDao().find(currentGame.getId()); try{ //getLogger().debug("In the assigngroups." + game.getId()); - - students = getStudentDao().findAllByProperty("game", game); //getLogger().debug("test student object is : " + students.size()); @@ -261,7 +264,7 @@ getLogger().debug("size of 5 : " + noOfGroupsWithSize5); */ - int groupNo = 0; + int startGroupNo = 0; //assign every student a number and group if(!students.isEmpty()) @@ -269,25 +272,25 @@ if(noOfGroupsWithSize5 > 0) { - for(Student student5:assignGroupstoStudents(5,noOfGroupsWithSize5,students,groupNo)) + for(Student student5:assignGroupstoStudents(5,noOfGroupsWithSize5,students,startGroupNo)) studentWithGroupsAssigned.add(student5); //getLogger().debug("student size after 5 is: " + students.size()); getLogger().debug("student with groups assigned: " + studentWithGroupsAssigned.size()); } - groupNo = noOfGroupsWithSize5; + startGroupNo = noOfGroupsWithSize5; if(noOfGroupsWithSize4 > 0) { - for(Student student4:assignGroupstoStudents(4,noOfGroupsWithSize4,students,groupNo)) + for(Student student4:assignGroupstoStudents(4,noOfGroupsWithSize4,students,startGroupNo)) studentWithGroupsAssigned.add(student4); //getLogger().debug("student size after 4 is: " + students.size()); getLogger().debug("student with groups assigned: " + studentWithGroupsAssigned.size()); } - groupNo = noOfGroupsWithSize5 + noOfGroupsWithSize4; + startGroupNo = noOfGroupsWithSize5 + noOfGroupsWithSize4; if(noOfGroupsWithSize3 > 0) { - for(Student student3:assignGroupstoStudents(3,noOfGroupsWithSize3,students,groupNo)) + for(Student student3:assignGroupstoStudents(3,noOfGroupsWithSize3,students,startGroupNo)) studentWithGroupsAssigned.add(student3); //getLogger().debug("student size after 3 is: " + students.size()); getLogger().debug("student with groups assigned: " + studentWithGroupsAssigned.size()); @@ -321,7 +324,7 @@ System.out.println("Students size is: " + studentWithGroupsAssigned.size()); msgBroker.routeMessageToService(msg, null);*/ - MessageHandler msgHandler = new MessageHandler(); + msgHandler = new MessageHandler(null); msgHandler.setDestination("mme"); msgHandler.setSubtopic(game.getGameCode()); /*for(Student student: studentWithGroupsAssigned) @@ -937,7 +940,7 @@ boolean flag = false; List<Block> blocks = new ArrayList<Block>(); - getLogger().debug("in iscurrentmodulefinished"); + //getLogger().debug("in iscurrentmodulefinished"); if(currentBlock == null) { flag = true; @@ -987,11 +990,11 @@ return newGame; } - public Integer getTimerforBlock(Block block) + /*public Integer getTimerforBlock(Block block) { return block.getDuration(); - } + }*/ /*public Block getCurrentBlock() { //FIXEME: game id 1 is hard coded, it should be dynamic @@ -1115,4 +1118,9 @@ public RoundService getRoundService() { return roundService; } + + + + + } Modified: mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/RoundService.java =================================================================== --- mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/RoundService.java 2009-10-10 01:18:43 UTC (rev 293) +++ mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/RoundService.java 2009-10-12 22:05:11 UTC (rev 294) @@ -775,7 +775,7 @@ boolean running = false; getLogger().info("game start is: " + game.getId()); newGameState = getGameDao().find(game.getId()); - MessageHandler msgHandler = new MessageHandler(); + MessageHandler msgHandler = new MessageHandler(null); msgHandler.setDestination("mme"); msgHandler.setSubtopic(getGameCode(newGameState)); Modified: mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/StartGame.java =================================================================== --- mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/StartGame.java 2009-10-10 01:18:43 UTC (rev 293) +++ mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/StartGame.java 2009-10-12 22:05:11 UTC (rev 294) @@ -58,7 +58,7 @@ public List<Student> assignGroups(Game game) { List<Student> students = null; - students = studentService.assignGroups(game); + // students = studentService.assignGroups(game); if(students != null) { MessageBroker msgBroker = MessageBroker.getMessageBroker("_messageBroker"); Modified: mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/StudentService.java =================================================================== --- mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/StudentService.java 2009-10-10 01:18:43 UTC (rev 293) +++ mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/StudentService.java 2009-10-12 22:05:11 UTC (rev 294) @@ -10,7 +10,6 @@ import edu.asu.commons.mme.dao.HibernateGroupDao; import edu.asu.commons.mme.dao.HibernateStudentDao; import edu.asu.commons.mme.entity.Game; -import edu.asu.commons.mme.entity.Group; import edu.asu.commons.mme.entity.Student; /** @@ -79,157 +78,6 @@ return false; } - - public List<Student> assignGroups(Game game) - { - //createGroup(1); - List<Student> studentWithGroupsAssigned = new ArrayList<Student>(); - List<Student> students = new ArrayList<Student>(); - try{ - getLogger().debug("In the assigngroups." + game.getId()); - - students = getDao().findAllByProperty("game", game); - - //getLogger().debug("test student object is : " + students.size()); - if(students.size() > 1) - { - //write an algorithm to form groups - int totalStudents = students.size(); - int noOfGroupsWithSize3=0; - int noOfGroupsWithSize4=0; - int noOfGroupsWithSize5=0; - int x; - - - //no of groups - noOfGroupsWithSize5=((int)(totalStudents/5)); - - - x= totalStudents%5; - - /* if(x==0) - { - totalGroups=groupSize5; - } - else*/ - if(x==1) - { - noOfGroupsWithSize5=noOfGroupsWithSize5 - 1; - noOfGroupsWithSize3=2; - // totalGroups=groupSize5 + groupSize3; - } - - else if(x==2) - { - noOfGroupsWithSize5=noOfGroupsWithSize5-1; - noOfGroupsWithSize4=1; - noOfGroupsWithSize3=1; - // totalGroups=groupSize5 + groupSize3 + groupSize4; - } - else if(x==3) - { - noOfGroupsWithSize3=1; - // totalGroups=groupSize3 + groupSize5; - } - - else if(x==4) - { - noOfGroupsWithSize4=1; - // totalGroups=groupSize4 + groupSize5; - } - - - //getLogger().debug("size of 4 : " + noOfGroupsWithSize4); - //getLogger().debug("size of 3 : " + noOfGroupsWithSize3); - //getLogger().debug("size of 5 : " + noOfGroupsWithSize5); - - int groupNo = 0; - //assign every student a number and group - if(!students.isEmpty()) - { - if(noOfGroupsWithSize5 > 0) - { - studentWithGroupsAssigned.addAll(assignGroupstoStudents(5,noOfGroupsWithSize5,students,groupNo)); - //getLogger().debug("student size after 5 is: " + students.size()); - //getLogger().debug("student with groups assigned: " + studentWithGroupsAssigned.size()); - } - groupNo = noOfGroupsWithSize5; - if(noOfGroupsWithSize4 > 0) - { - studentWithGroupsAssigned.addAll(assignGroupstoStudents(4,noOfGroupsWithSize4,students,groupNo)); - //getLogger().debug("student size after 4 is: " + students.size()); - //getLogger().debug("student with groups assigned: " + studentWithGroupsAssigned.size()); - } - groupNo = noOfGroupsWithSize5 + noOfGroupsWithSize4; - if(noOfGroupsWithSize3 > 0) - { - studentWithGroupsAssigned.addAll(assignGroupstoStudents(3,noOfGroupsWithSize3,students,groupNo)); - //getLogger().debug("student size after 3 is: " + students.size()); - //getLogger().debug("student with groups assigned: " + studentWithGroupsAssigned.size()); - } - } - } - getLogger().debug("Final student with groups assigned: " + studentWithGroupsAssigned.size()); - }catch(Exception e) - { - getLogger().error(e); - } - return studentWithGroupsAssigned; - - } - - private List<Student> assignGroupstoStudents(int groupSize, int noOfGroups,List<Student> students,int groupNo) { - // TODO Auto-generated method stub - Student student = new Student(); - List<Student> studentWithGroupsAssigned = new ArrayList<Student>(); - getLogger().debug("group size is : "+groupSize + " and group number is : "+ groupNo); - - for(int counter = 0; counter < noOfGroups; counter++) - { - //create group - Group grp = new Group(); - - grp.setNumber(++groupNo); - groupDao.save(grp); - - getLogger().info("Group is created with id: " + grp.getId()); - int studentNo = 1; - int j = 0; - for(int i = 0; i <= groupSize-1 ; i++) - { - //get a student from a list - student = students.get(0); - getLogger().debug("i is: " + i +"student id is : "+student.getId()); - if(student.getGroup() == null) - { - //assign student no and group to each student - getLogger().debug("group assigned is : " + grp.getId()); - student.setStudentNo(studentNo); - student.setGroup(grp); - getDao().save(student); - studentNo++; - initializeStudent(student); - studentWithGroupsAssigned.add(student); - students.remove(0); - } - j++; - } - } - return students; - } - - public Group createGroup(int groupNo) - { - Group grp = new Group(); - grp.setNumber(groupNo); - groupDao.save(grp); - getLogger().info("Group is created with id: " + grp.getId()); - return grp; - } - /*public HibernateGroupDao getGroupDao() { - return groupDao; - }*/ - public void setGroupDao(HibernateGroupDao groupDao) { this.groupDao = groupDao; } Modified: mentalmodels/trunk/src/main/java/edu/asu/commons/mme/utility/MessageHandler.java =================================================================== --- mentalmodels/trunk/src/main/java/edu/asu/commons/mme/utility/MessageHandler.java 2009-10-10 01:18:43 UTC (rev 293) +++ mentalmodels/trunk/src/main/java/edu/asu/commons/mme/utility/MessageHandler.java 2009-10-12 22:05:11 UTC (rev 294) @@ -1,17 +1,25 @@ package edu.asu.commons.mme.utility; +import org.apache.log4j.Logger; import edu.asu.commons.mme.entity.Block; +import edu.asu.commons.mme.entity.Game; +import edu.asu.commons.mme.service.GameService; import edu.asu.commons.mme.service.StartGame; import flex.messaging.MessageBroker; import flex.messaging.messages.AcknowledgeMessage; import flex.messaging.messages.AsyncMessage; import flex.messaging.util.UUIDUtils; -import org.apache.log4j.Logger; public class MessageHandler { - + private static Logger logger = Logger.getLogger(StartGame.class); + + private PushData pushData; + public MessageHandler(PushData pushData) { + // TODO Auto-generated constructor stub + this.pushData = pushData; + } private String destination; public String getDestination() { @@ -34,7 +42,7 @@ public void send(Object message) { - + MessageBroker msgBroker = MessageBroker.getMessageBroker("_messageBroker"); String clientID = UUIDUtils.createUUID(); @@ -51,28 +59,48 @@ System.out.println("Students size is: " + message); AcknowledgeMessage ack = msgBroker.routeMessageToService(msg, null); - logger.info("Ack received from client for message " + msg + "is : " + ack); + /*logger.info("Ack received from client for message " + msg + "is : " + ack);*/ } - - public void sendBlock(Block block) + + public void sendBlock(Block block, Game game) { MessageBroker msgBroker = MessageBroker.getMessageBroker("_messageBroker"); String clientID = UUIDUtils.createUUID(); AsyncMessage msg = new AsyncMessage(); msg.setDestination(destination); - //String msgDestination = game.getGameCode(); msg.setHeader("DSSubtopic", subtopic); msg.setClientId(clientID); msg.setMessageId(UUIDUtils.createUUID()); msg.setTimestamp(System.currentTimeMillis()); - msg.setBody(block); - System.out.println("Message broker is: "+ msgBroker); - //System.out.println("Students size is: " + message); + AcknowledgeMessage ack = msgBroker.routeMessageToService(msg, null); + if(block!=null) + { + //int duration = block.getDuration(); + + int duration = 5; + Timer timer = new Timer(); + Long startTime = System.currentTimeMillis()/1000; + System.out.println("Started time is : " + System.currentTimeMillis()/1000); - AcknowledgeMessage ack = msgBroker.routeMessageToService(msg, null); + while((startTime + duration) > System.currentTimeMillis()/1000) + { + + System.out.println("Still Running..." + System.currentTimeMillis()/1000); + timer.run(); + } + System.out.println("Stop timer..." + System.currentTimeMillis()/1000); + pushData.pushNextData(game); + } + else + { + System.out.println("Game is Over..." + msg); + } + + + /*AcknowledgeMessage ack = msgBroker.routeMessageToService(msg, null); logger.info("Ack received from client for message " + msg + "is : " + ack); - + */ } } Added: mentalmodels/trunk/src/main/java/edu/asu/commons/mme/utility/PushData.java =================================================================== --- mentalmodels/trunk/src/main/java/edu/asu/commons/mme/utility/PushData.java (rev 0) +++ mentalmodels/trunk/src/main/java/edu/asu/commons/mme/utility/PushData.java 2009-10-12 22:05:11 UTC (rev 294) @@ -0,0 +1,8 @@ +package edu.asu.commons.mme.utility; + +import edu.asu.commons.mme.entity.Game; + +public interface PushData { + + public void pushNextData(Game game); +} Added: mentalmodels/trunk/src/main/java/edu/asu/commons/mme/utility/Timer.java =================================================================== --- mentalmodels/trunk/src/main/java/edu/asu/commons/mme/utility/Timer.java (rev 0) +++ mentalmodels/trunk/src/main/java/edu/asu/commons/mme/utility/Timer.java 2009-10-12 22:05:11 UTC (rev 294) @@ -0,0 +1,21 @@ +package edu.asu.commons.mme.utility; + +public class Timer implements Runnable { + + public Timer() + { + + } + + public void run() { + + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |