Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Server;
- import Model.Messages.Message;
- import Model.Network.Token;
- import Model.Network.UserSession;
- import java.util.*;
- import java.util.concurrent.*;
- import Utils.*;
- import Config.*;
- /**
- * Class NetworkScheduler will send Messages to the connected clients based on
- * what those messages are (it will pack them into json strings) and based on
- * if enough time has passed since the last dispatched set of messages, simply call
- * send(Message m) and it will be done
- */
- public class NetworkScheduler {
- private GameServer gameServer;
- private Map<Token, BlockingQueue> messageMap;
- private Semaphore sem;
- public NetworkScheduler() {
- messageMap = new HashMap<Token, BlockingQueue>();
- sem = new Semaphore(1000/Config.MAX_MESSAGES_TIMER);
- }
- public void addMessage(Message message) {
- try {
- if (messageMap.containsKey(message.getReceptor())) {
- messageMap.get(message.getReceptor()).put(message);
- } else {
- messageMap.put(message.getReceptor(), new PriorityBlockingQueue<Message>());
- messageMap.get(message.getReceptor()).put(message);
- ExecutorService sender = new Executors.newSingleThreadExecutor();
- Executor scheduler = new Executors.newSingleThreadScheduledExecutor();
- }
- }
- catch(InterruptedException e) {
- Logger.logException("Adding a message has thrown Interrupted Exception");
- }
- }
- public void setServer(GameServer server) {
- gameServer = server;
- }
- public class SendingThread {
- Semaphore semaphore = null;
- public SendingThread(Semaphore semaphore) {
- this.semaphore = semaphore;
- }
- public void run() {
- while (true) {
- try {
- semaphore.acquire();
- Message message = MessageQueue.take();
- } catch (InterruptedException e) {
- Logger.logException("Sending thread interrupted");
- }
- }
- }
- }
- public class SchedulingThread {
- Semaphore semaphore = null;
- public SchedulingThread(Semaphore semaphore) {
- this.semaphore = semaphore;
- }
- public void run() {
- while (true) {
- semaphore.drainPermits();
- semaphore.release(1000 / Config.MAX_MESSAGES_TIMER);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement