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

Développement du proxy pour la communication serveur -> client, pour la synchro de chaque client.

parent 93412246
......@@ -24,6 +24,12 @@
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
......
......@@ -19,6 +19,7 @@ import javax.servlet.Servlet;
@ComponentScan
public class NotAloneApplication {
public static void main(String[] args) {
SpringApplication.run(NotAloneApplication.class, args);
}
......
package fr.univnantes.alma.common;
import fr.univnantes.alma.thrift.GameServerService;
import fr.univnantes.alma.thrift.JoinRequest;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.protocol.TProtocolFactory;
import org.apache.thrift.transport.THttpClient;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.server.LocalServerPort;
import java.util.List;
public class ClientProxy {
@Autowired
protected TProtocolFactory protocolFactory;
@LocalServerPort
protected int port;
protected GameServerService.Iface client;
public ClientProxy(String ip_port) throws TTransportException {
TTransport transport = new THttpClient(ip_port + "/api");
TProtocol protocol = protocolFactory.getProtocol(transport);
client = new GameServerService.Client(protocol);
}
int createGame(int gameId) throws TException {
return this.client.createGame(gameId);
}
int join(int gameId, JoinRequest joinRequest) throws TException {
return this.client.join(gameId, joinRequest);
}
List<String> getJoueurs(int gameId) throws TException {
return this.client.getJoueurs(gameId);
}
void startGame(int gameId) throws TException {
this.client.startGame(gameId);
}
}
package fr.univnantes.alma.common;
import fr.univnantes.alma.common.GameJoinRequest;
import org.apache.thrift.transport.TTransportException;
import java.util.List;
......@@ -20,7 +21,7 @@ public interface GameService {
* @param request
* @return
*/
int join(int gameId, GameJoinRequest request);
int join(int gameId, GameJoinRequest request) throws TTransportException;
void startGame(int gameId);
......
package fr.univnantes.alma.game;
import fr.univnantes.alma.common.ClientProxy;
import fr.univnantes.alma.common.GameJoinRequest;
import org.apache.thrift.transport.TTransportException;
import org.atlanmod.commons.log.Log;
import java.util.HashMap;
......@@ -17,6 +19,8 @@ public class Game {
*/
private final BlockingQueue<GameJoinRequest> requests;
private Map<String, Player> players;
private List<ClientProxy> clientProxies;
/**
* Counter used to increment player identifications.
*/
......@@ -30,14 +34,15 @@ public class Game {
this.expectedPlayers = new AtomicInteger(expectedPlayers);
this.requests = new ArrayBlockingQueue<GameJoinRequest>(expectedPlayers);
this.players = new HashMap<>(expectedPlayers);
this.clientProxies = new LinkedList<>();
play = new Thread(() -> this.start());
play.start();
}
public int join(GameJoinRequest request) {
public int join(GameJoinRequest request) throws TTransportException {
int id = idCounter.getAndIncrement();
requests.offer(request);
clientProxies.add(new ClientProxy("http://localhost:8080"));
return id;
}
......@@ -67,10 +72,14 @@ public class Game {
players.put(request.getName(), new Player(request.getName()));
}
public List<String> getJoueurs() {
LinkedList<String> liste = new LinkedList<>();
liste.addAll(players.keySet());
return liste;
}
}
......@@ -4,6 +4,7 @@ package fr.univnantes.alma.game;
import fr.univnantes.alma.common.GameJoinRequest;
import fr.univnantes.alma.common.GameService;
import fr.univnantes.alma.common.GameService;
import org.apache.thrift.transport.TTransportException;
import org.springframework.stereotype.Component;
import java.util.HashMap;
......@@ -31,7 +32,7 @@ public class GameController implements GameService {
}
@Override
public int join(int gameId, GameJoinRequest request) {
public int join(int gameId, GameJoinRequest request) throws TTransportException {
Game game = games.get(gameId);
return game.join(request);
}
......
......@@ -3,6 +3,7 @@ package fr.univnantes.alma.game;
public class Player {
String name;
Player(String name) {
this.name = name;
}
......
This diff is collapsed.
......@@ -25,6 +25,8 @@
"@creditkarma/thrift-typescript": "^3.7.6",
"@ngtools/webpack": "^10.1.3",
"@types/thrift": "^0.10.10",
"ansi-colors": "^4.1.1",
"ansicolor": "^1.1.93",
"core-js": "^3.6.5",
"marked": "^1.2.0",
"rxjs": "~6.6.3",
......
......@@ -2,10 +2,12 @@ import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { GameComponent } from './game/game.component';
@NgModule({
declarations: [
AppComponent
AppComponent,
GameComponent
],
imports: [
BrowserModule
......
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { GameComponent } from './game.component';
describe('GameComponent', () => {
let component: GameComponent;
let fixture: ComponentFixture<GameComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ GameComponent ]
})
.compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(GameComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
import { Component, OnInit } from '@angular/core';
import {ThriftClient} from "../../client/thrift-client";
@Component({
selector: 'app-game',
templateUrl: './game.component.html',
styleUrls: ['./game.component.css']
})
export class GameComponent implements OnInit {
private client : ThriftClient;
constructor() {
this.client = new ThriftClient();
}
ngOnInit(): void {
}
}
import { createHttpClient, HttpConnection } from '@creditkarma/thrift-client';
import * as request from 'request';
import { CoreOptions } from 'request';
import { Client } from '../codegen/fr/univnantes/alma/thrift/GameServerService';
const CONFIG = {
hostName: 'localhost',
port: 8080, // 8080 port serveur java
};
const thriftClient: Client<CoreOptions> = createHttpClient(Client, CONFIG);
const id: Promise<number> = thriftClient.createGame(5);
export class ThriftClient {
private idGame = 0;
constructor() {
id.then(data => {
this.idGame = data;
console.log(data);
}).catch((error) => {
console.log(error);
});
}
}
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