Handled errors with notification popups
This commit is contained in:
parent
c54393c2b9
commit
124b672aeb
16 changed files with 1203 additions and 626 deletions
|
|
@ -1,6 +1,7 @@
|
|||
|
||||
package com.texttwist.server.components;
|
||||
import com.sun.org.apache.xpath.internal.operations.Bool;
|
||||
import com.texttwist.server.tasks.SendInvitations;
|
||||
import jdk.nashorn.internal.parser.JSONParser;
|
||||
import models.Message;
|
||||
import org.json.simple.JsonObject;
|
||||
|
|
@ -13,11 +14,12 @@ import java.net.ServerSocket;
|
|||
import java.net.Socket;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.SelectionKey;
|
||||
import java.nio.channels.Selector;
|
||||
import java.nio.channels.ServerSocketChannel;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.nio.channels.*;
|
||||
import java.util.Iterator;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import static java.nio.channels.SelectionKey.OP_ACCEPT;
|
||||
import static java.nio.channels.SelectionKey.OP_READ;
|
||||
|
|
@ -28,6 +30,7 @@ public class GameServer implements Runnable{
|
|||
protected ServerSocketChannel serverSocketChannel = null;
|
||||
protected ThreadProxy proxy;
|
||||
protected Selector selector = null;
|
||||
protected ExecutorService threadPool = Executors.newCachedThreadPool();
|
||||
|
||||
public GameServer(int port){
|
||||
this.serverPort = port;
|
||||
|
|
@ -77,9 +80,9 @@ public class GameServer implements Runnable{
|
|||
|
||||
if (line.startsWith("MESSAGE")) {
|
||||
Message msg = Message.toMessage(line);
|
||||
proxy = new ThreadProxy(msg);
|
||||
Thread t = new Thread(proxy);
|
||||
t.start();
|
||||
proxy = new ThreadProxy(msg, client);
|
||||
Future<Boolean> identifyMessage = threadPool.submit(proxy);
|
||||
identifyMessage.get();
|
||||
}
|
||||
|
||||
if (line.startsWith("CLOSE")) {
|
||||
|
|
@ -102,9 +105,13 @@ public class GameServer implements Runnable{
|
|||
break;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
e.printStackTrace();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
/* try {
|
||||
this.serverSocket = new ServerSocket(this.serverPort);
|
||||
Logger.write("Game Service is running at "+this.serverPort+" port...");
|
||||
|
|
|
|||
|
|
@ -36,18 +36,13 @@ public class NotificationServer implements INotificationServer {
|
|||
}
|
||||
}
|
||||
|
||||
public void update(String username, DefaultListModel<String> users) throws RemoteException {
|
||||
doCallbacks(username, users);
|
||||
}
|
||||
|
||||
private synchronized void doCallbacks(String username, DefaultListModel<String> users) throws RemoteException{
|
||||
public synchronized void sendInvitations(String username, DefaultListModel<String> users) throws RemoteException {
|
||||
System.out.print("Starting callbacks");
|
||||
Iterator i = clients.iterator();
|
||||
while(i.hasNext()){
|
||||
INotificationClient client = (INotificationClient) i.next();
|
||||
|
||||
//Calcola i valori da inviare e inviali
|
||||
client.sendInvite(username, users);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,16 +4,22 @@ import com.texttwist.server.tasks.CheckOnlineUsers;
|
|||
import com.texttwist.server.tasks.SendInvitations;
|
||||
import models.Message;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
/**
|
||||
* Created by loke on 18/06/2017.
|
||||
*/
|
||||
public class ThreadProxy implements Runnable {
|
||||
public class ThreadProxy implements Callable<Boolean> {
|
||||
protected ExecutorService threadPool = Executors.newCachedThreadPool();
|
||||
private Message request;
|
||||
ThreadProxy(Message request){
|
||||
private SocketChannel socketChannel;
|
||||
ThreadProxy(Message request, SocketChannel socketChannel){
|
||||
this.request = request;
|
||||
this.socketChannel = socketChannel;
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -21,52 +27,65 @@ public class ThreadProxy implements Runnable {
|
|||
return SessionsManager.getInstance().isValidToken(token);
|
||||
}
|
||||
|
||||
public void run() {
|
||||
@Override
|
||||
public Boolean call() {
|
||||
ByteBuffer buffer = ByteBuffer.allocate(1024);
|
||||
|
||||
System.out.println("Selecting right task for new thread");
|
||||
|
||||
System.out.println(request.token);
|
||||
System.out.println(request.sender);
|
||||
System.out.println(request.message);
|
||||
System.out.println(request.data);
|
||||
|
||||
|
||||
|
||||
if(isValidToken(request.token)){
|
||||
switch(request.message){
|
||||
case "START_GAME":
|
||||
Future<Boolean> onlineUsers = threadPool.submit(new CheckOnlineUsers(request.data));
|
||||
Boolean res = null;
|
||||
try {
|
||||
res = onlineUsers.get();
|
||||
SessionsManager.getInstance().printSessions();
|
||||
if(res){
|
||||
Boolean usersOnline = onlineUsers.get();
|
||||
if(usersOnline){
|
||||
Future<Boolean> sendInvitations = threadPool.submit(new SendInvitations(request.sender, request.data));
|
||||
try {
|
||||
res = sendInvitations.get();
|
||||
System.out.println(res);
|
||||
if (res) {
|
||||
System.out.println("SJSJSJSJSJ");
|
||||
Boolean invitationSended = sendInvitations.get();
|
||||
if (invitationSended) {
|
||||
|
||||
Message message = new Message("INVITES_ALL_SENDED", "", "", new DefaultListModel<String>());
|
||||
byte[] byteMessage = new String(message.toString()).getBytes();
|
||||
buffer = ByteBuffer.wrap(byteMessage);
|
||||
socketChannel.write(buffer);
|
||||
socketChannel.close();
|
||||
return true;
|
||||
} else {
|
||||
|
||||
return false;
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
|
||||
Message message = new Message("USER_NOT_ONLINE", "", "", new DefaultListModel<String>());
|
||||
byte[] byteMessage = new String(message.toString()).getBytes();
|
||||
buffer = ByteBuffer.wrap(byteMessage);
|
||||
socketChannel.write(buffer);
|
||||
socketChannel.close();
|
||||
return false;
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
default:
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
} else {
|
||||
System.out.print("TOKEN NON VALIDO");
|
||||
//RISPONDI ERRORE TOKEN NON VALIDO
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ public class SendInvitations implements Callable<Boolean> {
|
|||
private final DefaultListModel<String> users;
|
||||
private String sender;
|
||||
|
||||
public SendInvitations( String sender, DefaultListModel<String> users) {
|
||||
public SendInvitations(String sender, DefaultListModel<String> users) {
|
||||
this.users = users;
|
||||
this.sender = sender;
|
||||
}
|
||||
|
|
@ -27,7 +27,7 @@ public class SendInvitations implements Callable<Boolean> {
|
|||
public Boolean call() throws Exception {
|
||||
System.out.print("SendInvitations!");
|
||||
try {
|
||||
Server.notificationServer.update(sender, users);
|
||||
Server.notificationServer.sendInvitations(sender, users);
|
||||
|
||||
} catch (Exception e) {
|
||||
System.out.println("Eccezione" + e);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue