Added task class to threads

This commit is contained in:
Lorenzo Iovino 2017-06-19 02:43:40 +02:00
parent eb1754e63e
commit cedaf8f009
18 changed files with 1150 additions and 379 deletions

View file

@ -2,6 +2,7 @@ package com.texttwist.client;
import com.texttwist.client.pages.AuthService;
import com.texttwist.client.pages.Home;
import com.texttwist.client.pages.MatchService;
import com.texttwist.client.pages.SessionService;
import javax.swing.*;
@ -14,6 +15,7 @@ public class App extends JFrame {
public static AuthService authService;
public static SessionService sessionService;
public static MatchService matchService;
public App(){
setPreferredSize( new Dimension( 640, 480 ));
@ -25,6 +27,7 @@ public class App extends JFrame {
//Init services
authService = new AuthService();
sessionService = new SessionService();
matchService = new MatchService();
Home home = new Home(this);
}

View file

@ -5,11 +5,19 @@ package com.texttwist.client.constants;
*/
public class Config {
private static String RMIServerURI = "localhost";
private static Integer RMIServerPort = 9999;
private static String AuthServerURI = "localhost";
private static Integer AuthServerPort = 9999;
public static String GameServerURI = "localhost";
public static Integer GameServerPort = 10000;
public static String getRMIServerAddress(){
return "rmi://".concat(RMIServerURI).concat(":").concat(RMIServerPort.toString());
public static String getAuthServerURI(){
return "rmi://".concat(AuthServerURI).concat(":").concat(AuthServerPort.toString());
}
public static String getGameServerURI(){
return "tcp://".concat(GameServerURI).concat(":").concat(GameServerPort.toString());
}
}

View file

@ -1,5 +1,6 @@
package com.texttwist.client.pages;
import com.texttwist.client.App;
import com.texttwist.client.constants.Config;
import interfaces.IAuth;
import models.Response;
@ -14,7 +15,7 @@ import java.rmi.RemoteException;
*/
public class AuthService {
protected String baseUrl = Config.getRMIServerAddress().concat("/auth");
protected String baseUrl = Config.getAuthServerURI().concat("/auth");
public AuthService(){
}
@ -29,8 +30,8 @@ public class AuthService {
return auth.register(userName, password);
}
public Response logout(String userName, String token) throws RemoteException, NotBoundException, MalformedURLException {
public Response logout(String userName) throws RemoteException, NotBoundException, MalformedURLException {
IAuth auth = (IAuth) Naming.lookup(baseUrl);
return auth.logout(userName, token);
return auth.logout(userName, App.sessionService.account.token);
}
}

View file

@ -0,0 +1,53 @@
package com.texttwist.client.pages;
import com.texttwist.client.App;
import com.texttwist.client.constants.Config;
import interfaces.IAuth;
import models.Message;
import models.Response;
import org.json.simple.JsonObject;
import javax.swing.*;
import java.io.*;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
/**
* Created by loke on 18/06/2017.
*/
public class MatchService {
public MatchService(){
}
public Response play(DefaultListModel<String> userNames) throws IOException {
InetSocketAddress socketAddress = new InetSocketAddress(Config.GameServerURI, Config.GameServerPort);
SocketChannel clientSocket = SocketChannel.open(socketAddress);
Message message = new Message("START_GAME", App.sessionService.account.token, userNames);
byte[] byteMessage = new String(message.toString()).getBytes();
ByteBuffer buffer = ByteBuffer.wrap(byteMessage);
clientSocket.write(buffer);
//Risposta dal server
/*JsonObject data = new JsonObject();
data.put("obj", out);
data.put("unavailableUsers", out);*/
clientSocket.close();
Response res = new Response("Player unavailable!",400, new JsonObject());
return res;
}
}

View file

@ -90,7 +90,7 @@ public class MatchSetup extends Page{
new Callable() {
@Override
public Object call() throws Exception {
return new MatchSetup(Page.window);
return null;
}
},null);
}

View file

@ -4,6 +4,7 @@ import com.texttwist.client.App;
import models.Response;
import javax.swing.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
@ -15,13 +16,11 @@ public class MatchSetupController {
public MatchSetupController(){}
public Response play(DefaultListModel<String> userNames) throws RemoteException, NotBoundException, MalformedURLException {
System.out.print(userNames.toString());
/* Response res = App.authService.login(userName,password);
if (res.code == 200){
App.sessionService.create(userName, res.data.get("token").toString());
}*/
try {
return App.matchService.play(userNames);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}

View file

@ -92,7 +92,7 @@ public class Menu extends Page{
new Callable<Object>() {
@Override
public Object call() throws Exception {
menuController.logout(App.sessionService.account.userName,App.sessionService.account.token);
menuController.logout(App.sessionService.account.userName);
return new Home(Page.window);
}
},

View file

@ -15,8 +15,8 @@ public class MenuController {
public MenuController(){
}
public Response logout(String userName, String token) throws RemoteException, NotBoundException, MalformedURLException {
Response res = App.authService.logout(userName,token);
public Response logout(String userName) throws RemoteException, NotBoundException, MalformedURLException {
Response res = App.authService.logout(userName);
if (res.code == 200){
App.sessionService.remove();
}

View file

@ -24,8 +24,10 @@ public class TTSearchBar extends TTContainer{
@Override
public Object call() throws Exception {
String username = ctx.getText();
ctx.setText("");
list.addElement(username);
if(!username.equals("")) {
ctx.setText("");
list.addElement(username);
}
return null;
}
};
@ -34,7 +36,7 @@ public class TTSearchBar extends TTContainer{
public TTSearchBar(Point position,
Dimension dimension,
String placeholer,
String placeholder,
TTContainer parent) throws Exception {
super(position, dimension, Palette.inputBox_backgroundColor, -1, parent);
@ -47,7 +49,7 @@ public class TTSearchBar extends TTContainer{
TTLabel playerFinder_flavourText = new TTLabel(
new Point(20,40),
new Dimension(350,50),
"<html>Player to invite</html>",
"<html>Add player</html>",
new Font(Palette.inputBox_font.getFontName(), Font.ITALIC, 18),
null,
parent);
@ -55,9 +57,10 @@ public class TTSearchBar extends TTContainer{
TTInputField usernameField = new TTInputField(
new Point(20,80),
new Dimension(250,45),
"Username",
placeholder,
parent);
/* TTScrollList userList = new TTScrollList(
/*TTScrollList userList = new TTScrollList(
new Point(20,120),
new Dimension(250,95),
matchedUsers,
@ -75,7 +78,7 @@ public class TTSearchBar extends TTContainer{
TTLabel playerToSendInvite_flavourText = new TTLabel(
new Point(305,40),
new Dimension(350,50),
"Double-Click on user for remove",
"Double-Click on item for remove",
new Font(Palette.inputBox_font.getFontName(), Font.ITALIC, 18),
null,
parent);
@ -93,6 +96,7 @@ public class TTSearchBar extends TTContainer{
super.mouseClicked(evt);
JList thisList = (JList)evt.getSource();
if (evt.getClickCount() == 2) {
// Double-click detected
int index = thisList.locationToIndex(evt.getPoint());
list.remove(index);
@ -111,7 +115,6 @@ public class TTSearchBar extends TTContainer{
e1.printStackTrace();
}
}
//Every time i press a key, execute a search of users
}
});