Advertisement
Guest User

Untitled

a guest
Dec 21st, 2014
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. package Server;
  2.  
  3. import Model.Messages.Message;
  4. import Model.Network.Token;
  5. import Model.Network.UserSession;
  6.  
  7. import java.util.*;
  8. import java.util.concurrent.*;
  9.  
  10. import Utils.*;
  11. import Config.*;
  12.  
  13. /**
  14. * Class NetworkScheduler will send Messages to the connected clients based on
  15. * what those messages are (it will pack them into json strings) and based on
  16. * if enough time has passed since the last dispatched set of messages, simply call
  17. * send(Message m) and it will be done
  18. */
  19. public class NetworkScheduler {
  20.  
  21. private GameServer gameServer;
  22. private Map<Token, BlockingQueue> messageMap;
  23. private Semaphore sem;
  24.  
  25. public NetworkScheduler() {
  26. messageMap = new HashMap<Token, BlockingQueue>();
  27. sem = new Semaphore(1000/Config.MAX_MESSAGES_TIMER);
  28. }
  29.  
  30. public void addMessage(Message message) {
  31. try {
  32. if (messageMap.containsKey(message.getReceptor())) {
  33. messageMap.get(message.getReceptor()).put(message);
  34. } else {
  35. messageMap.put(message.getReceptor(), new PriorityBlockingQueue<Message>());
  36. messageMap.get(message.getReceptor()).put(message);
  37. ExecutorService sender = new Executors.newSingleThreadExecutor();
  38. Executor scheduler = new Executors.newSingleThreadScheduledExecutor();
  39. }
  40. }
  41. catch(InterruptedException e) {
  42. Logger.logException("Adding a message has thrown Interrupted Exception");
  43. }
  44. }
  45.  
  46. public void setServer(GameServer server) {
  47. gameServer = server;
  48. }
  49.  
  50. public class SendingThread {
  51. Semaphore semaphore = null;
  52.  
  53. public SendingThread(Semaphore semaphore) {
  54. this.semaphore = semaphore;
  55. }
  56.  
  57. public void run() {
  58. while (true) {
  59. try {
  60. semaphore.acquire();
  61. Message message = MessageQueue.take();
  62. } catch (InterruptedException e) {
  63. Logger.logException("Sending thread interrupted");
  64. }
  65. }
  66. }
  67. }
  68.  
  69. public class SchedulingThread {
  70. Semaphore semaphore = null;
  71.  
  72. public SchedulingThread(Semaphore semaphore) {
  73. this.semaphore = semaphore;
  74. }
  75.  
  76. public void run() {
  77. while (true) {
  78. semaphore.drainPermits();
  79. semaphore.release(1000 / Config.MAX_MESSAGES_TIMER);
  80.  
  81. }
  82. }
  83. }
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement