Commit c22f0c08 authored by Gerson Sunyé's avatar Gerson Sunyé
Browse files

Add TypeScript proxy example to not-alone-player

- Specify maven-site-plugin version in module not-alone-serevr to avoid execution errors
- Add a Game interface and a proxy to not-alone-player
- Correct TS compilation option to correctly generate JS code
- Add a Main class to not-alone-player
- Add a Player interface and a proxy to not-alone-server
parent eeec6916
......@@ -51,6 +51,7 @@
<sourceHighlighter>coderay</sourceHighlighter>
-->
<attributes>
<sourceHighlighter>coderay</sourceHighlighter>
<icons>font</icons>
<pagenums/>
<toc/>
......
= Conception détaillée
//:source-highlighter: rouge
== Game Server
[source, java]
----
class Klass {
String field;
}
----
[source, ocl]
----
context Klass
inv:
field->select()
----
.GameSever
[plantuml]
....
......
{
"name": "not-alone-web",
"name": "not-alone-player",
"version": "0.0.0",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "not-alone-web",
"version": "0.0.0",
"dependencies": {
"@creditkarma/thrift-client": "^0.16.1",
......
{
"name": "not-alone-web",
"name": "not-alone-player",
"version": "0.0.0",
"main": "dist/main.js",
"scripts": {
"prebuild": "tslint -c tslint.json -p tsconfig.json --fix",
"build": "tsc",
"prestart": "npm run build",
"start": "node .",
"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/\""
},
......
export interface GameInterface {
createGame(numberOfPlayers: number): Promise<number>;
join(gameId: number): Promise<number>;
}
import { ThriftServer } from './thrift/thrift-server';
/**
* Port used for local Thrift server.
*/
const LOCAL_PORT = 8090;
class Main {
private server: ThriftServer;
constructor() {
console.log('Main class');
this.server = new ThriftServer(LOCAL_PORT);
}
}
const main: Main = new Main();
import { Client } from 'src/codegen/common/GameService';
import { GameInterface } from '../core/game';
import { CoreOptions } from 'request';
import { createHttpClient, ICreateHttpClientOptions } from '@creditkarma/thrift-client';
import { IJoinRequest, JoinRequest } from '../codegen/common/JoinRequest';
export class GameProxy implements GameInterface {
private thriftClient: Client<CoreOptions>;
constructor(options: ICreateHttpClientOptions) {
this.thriftClient = createHttpClient(Client, options);
}
createGame(numberOfPlayers: number): Promise<number> {
return this.thriftClient.createGame(numberOfPlayers);
}
join(gameId: number): Promise<number> {
const request: IJoinRequest = new JoinRequest();
request.name = 'me';
return this.thriftClient.join(gameId, request);
}
}
......@@ -5,12 +5,11 @@ import * as express from 'express';
import { createThriftServer } from '@creditkarma/thrift-server-express';
const handler: ClientServiceHandler<express.Request> = new ClientServiceHandler<express.Request>();
const PORT = 8090;
export class ThriftServer {
private app: express.Application;
constructor() {
constructor(port: number) {
this.app = createThriftServer({
path: '/thrift',
thriftOptions: {
......@@ -18,8 +17,8 @@ export class ThriftServer {
handler: new GameService.Processor(handler),
},
});
this.app.listen(PORT, () => {
console.log(`Express server listening on port: ${PORT}`);
this.app.listen(port, () => {
console.log(`Thrift Express server listening on port: ${port}`);
});
}
}
......
......@@ -2,14 +2,15 @@
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"outDir": "./dist/",
"sourceMap": true,
"declaration": false,
"module": "ES6",
"module": "CommonJS",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "ES5",
"strict": true,
"target": "ES2017",
"typeRoots": [
"node_modules/@types"
],
......
......@@ -52,6 +52,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.9.1</version>
</plugin>
</plugins>
</build>
......
package fr.univnantes.alma;
import fr.univnantes.alma.thrift.GameServerService;
import fr.univnantes.alma.thrift.GameService;
import fr.univnantes.alma.handler.GameServiceHandler;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocolFactory;
......@@ -30,7 +30,7 @@ public class NotAloneApplication {
@Bean
public ServletRegistrationBean gameServer(TProtocolFactory protocolFactory, GameServiceHandler handler) {
TServlet tServlet = new TServlet(new GameServerService.Processor<GameServiceHandler>(handler), protocolFactory);
TServlet tServlet = new TServlet(new GameService.Processor<GameServiceHandler>(handler), protocolFactory);
return new ServletRegistrationBean(tServlet, "/api");
}
......
package fr.univnantes.alma.player;
public interface PlayerInterface {
void startGame(int gameId);
}
package fr.univnantes.alma.player;
import fr.univnantes.alma.thrift.PlayerService.Client;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
import java.net.SocketAddress;
public class PlayerProxy implements PlayerInterface {
private Client client;
public PlayerProxy(String addr, int port) throws TTransportException {
TTransport transport = new TSocket(addr, port);
TProtocol protocol = new TBinaryProtocol(transport);
client = new Client(protocol);
}
@Override
public void startGame(int gameId) {
try {
client.startGame(gameId);
} catch (TException e) {
e.printStackTrace();
}
}
}
This diff is collapsed.
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