Commit 93412246 authored by Natanael BOUTEILLER's avatar Natanael BOUTEILLER
Browse files

Liaison TypeScript (client) et serveur (java).

parent 4a6d798a
......@@ -13,5 +13,7 @@ service GameServerService {
i32 join(i32 gameId, JoinRequest request) throws (1:common.GameNotFound e)
void startGame(i32 gameId) throws (1:common.GameNotFound e)
list<string> getJoueurs(i32 gameId) throws (1:common.GameNotFound e)
}
......@@ -8,7 +8,7 @@
"prestart": "npm run build",
"start": "node ./dist/main.js",
"test": "echo \"Error: no test specified\" && exit 1",
"codegen": "\"./node_modules/.bin/thrift-typescript\" --target thrift-server --sourceDir \"../not-alone-core/src/main/thrift/\" --outDir \"./src/codegen/\""
"codegen": "thrift-typescript --target thrift-server --sourceDir \"../not-alone-core/src/main/thrift/\" --outDir \"./src/codegen/\""
},
"private": true,
"devDependencies": {
......
import { IJoinRequest } from 'src/codegen/common';
import { IHandler } from '../codegen/common/GameServerService';
import { PlayerService } from '../player/player-service';
import { GameProxy } from '../thrift/game-proxy';
const CONFIG = {
hostName: 'localhost',
port: 8045,
};
export class ClientServiceHandler<Context> implements IHandler<Context> {
private service: PlayerService;
constructor() {
this.service = new PlayerService();
this.service = new PlayerService(new GameProxy(CONFIG));
}
public createGame(numberOfPlayers: number, context?: Context): number | Promise<number> {
throw new Error('Method not implemented.');
return this.service.createGame(numberOfPlayers);
}
public join(gameId: number, request: IJoinRequest, context?: Context): number | Promise<number> {
throw new Error('Method not implemented.');
return this.service.join(gameId, request);
}
public startGame(gameId: number): void | Promise<void> {
......@@ -24,7 +31,7 @@ export class ClientServiceHandler<Context> implements IHandler<Context> {
public getJoueurs(gameId: number): string[] | Promise<string[]> {
throw new Error('Method not implemented');
return this.service.getJoueurs(gameId);
}
......
import { ThriftServer } from './thrift/thrift-server';
import { ThriftClient } from './thrift/thrift-client';
/**
* Port used for local Thrift server.
......@@ -8,8 +9,10 @@ const LOCAL_PORT = 8090;
class Main {
private server: ThriftServer;
private client: ThriftClient;
constructor() {
console.log('Main class');
this.client = new ThriftClient();
this.server = new ThriftServer(LOCAL_PORT);
}
}
......
import { IJoinRequest } from '../codegen/common/JoinRequest';
import { GameProxy } from '../thrift/game-proxy';
export class PlayerService {
private proxy: GameProxy;
constructor(gameProxy: GameProxy) {
this.proxy = gameProxy;
}
startGame(gameId: number): Promise<void> {
return this.proxy.startGame(gameId);
}
startGame(gameId: number): void {
console.log('Start game', gameId);
join(gameId: number, request: IJoinRequest): Promise<number> {
return this.proxy.join(gameId);
}
getJoueurs(gameId: number): Promise<string[]> {
return this.proxy.getJoueurs(gameId);
}
createGame(numberOfPlayers: number): Promise<number> {
return this.proxy.createGame(numberOfPlayers);
}
}
import { Client } from 'src/codegen/common/GameServerService';
import { Client } from '../codegen/common/GameServerService';
import { GameInterface } from '../core/game';
import { CoreOptions } from 'request';
import { createHttpClient, ICreateHttpClientOptions } from '@creditkarma/thrift-client';
......@@ -20,4 +20,12 @@ export class GameProxy implements GameInterface {
request.name = 'me';
return this.thriftClient.join(gameId, request);
}
getJoueurs(gameId: number): Promise<string[]> {
return this.thriftClient.getJoueurs(gameId);
}
startGame(gameId: number): Promise<void> {
return this.thriftClient.startGame(gameId);
}
}
......@@ -6,11 +6,22 @@ import { Client } from '../codegen/common/GameServerService';
const CONFIG = {
hostName: 'localhost',
port: 8045,
port: 8080, // 8080 port serveur java
};
const thriftClient: Client<CoreOptions> = createHttpClient(Client, CONFIG);
const id: Promise<Number> = thriftClient.createGame(8);
const id: Promise<number> = thriftClient.createGame(5);
export class ThriftClient {}
export class ThriftClient {
private idGame = 0;
constructor() {
id.then(data => {
this.idGame = data;
console.log(data);
}).catch((error) => {
console.log(error);
});
}
}
......@@ -32,6 +32,6 @@ public class NotAloneApplication {
public ServletRegistrationBean gameServer(TProtocolFactory protocolFactory, GameServiceHandler handler) {
TServlet tServlet = new TServlet(new GameServerService.Processor<GameServiceHandler>(handler), protocolFactory);
return new ServletRegistrationBean(tServlet, "/api");
return new ServletRegistrationBean(tServlet, "/thrift");
}
}
......@@ -22,6 +22,8 @@ public interface GameService {
*/
int join(int gameId, GameJoinRequest request);
void startGame(int gameId);
List<String> getJoueurs(int gameId);
}
......@@ -36,6 +36,12 @@ public class GameController implements GameService {
return game.join(request);
}
@Override
public void startGame(int gameId) {
//TODO
System.out.println("starting game");
}
@Override
public List<String> getJoueurs(int gameId) {
Game game = games.get(gameId);
......
......@@ -32,6 +32,11 @@ public class GameServiceHandler implements GameServerService.Iface {
return service.join(gameId, new GameJoinRequest(name));
}
@Override
public void startGame(int gameId) throws GameNotFound, TException {
service.startGame(gameId);
}
@Override
public List<String> getJoueurs(int gameId) throws TException{
return service.getJoueurs(gameId);
......
package fr.univnantes.alma;
import fr.univnantes.alma.thrift.GameService;
import fr.univnantes.alma.thrift.GameServerService;
import fr.univnantes.alma.thrift.JoinRequest;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TProtocol;
......@@ -24,7 +24,7 @@ class NotAloneApplicationTest {
@LocalServerPort
protected int port;
protected GameService.Iface client;
protected GameServerService.Iface client;
@BeforeEach
public void setUp() throws Exception {
......@@ -32,7 +32,7 @@ class NotAloneApplicationTest {
TProtocol protocol = protocolFactory.getProtocol(transport);
client = new GameService.Client(protocol);
client = new GameServerService.Client(protocol);
}
@Test
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment