Skip to content

Commit

Permalink
feat(tcp): implements first draft of tcp parser
Browse files Browse the repository at this point in the history
The TCP parser pulls information from the socket connection and has a hard codeded method of TCP,
though this may change to protocol instead of method. Protocol currently takes from the remote
connections family (an IP version). The call point must be stringified in the case of using a json
as the message pattern (as the NestJS docs show). Integration has also been built for this, using
concurrently to run the client and server servers.

fix #23
  • Loading branch information
jmcdo29 committed Apr 6, 2020
1 parent c349b77 commit bf0eb6b
Show file tree
Hide file tree
Showing 16 changed files with 6,406 additions and 0 deletions.
29 changes: 29 additions & 0 deletions integration/tcp/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "@ogma/platform-socket-io-integration",
"version": "0.0.0",
"scripts": {
"prebuild": "rimraf dist",
"build": "tsc -p tsconfig.build.json",
"start": "concurrently \"yarn start:server\" \"yarn start:client\"",
"start:client": "node dist/client/main.js",
"start:server": "node dist/server/main.js",
"visual": "node testScript"
},
"private": true,
"dependencies": {
"@nestjs/common": "^7.0.0",
"@nestjs/core": "^7.0.0",
"@nestjs/microservices": "^7.0.0",
"@nestjs/platform-express": "^7.0.0"
},
"devDependencies": {
"@nestjs/cli": "^7.1.1",
"@nestjs/testing": "^7.0.0",
"concurrently": "^5.1.0",
"jest": "^25.2.3",
"reflect-metadata": "0.1.13",
"rimraf": "^3.0.2",
"rxjs": "6.5.4",
"ts-jest": "^25.2.1"
}
}
22 changes: 22 additions & 0 deletions integration/tcp/src/client/app.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';

@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}

@Get()
getMessage() {
return this.appService.getHello();
}

@Get('error')
getError() {
return this.appService.getError();
}

@Get('skip')
getSkip() {
return this.appService.getSkip();
}
}
23 changes: 23 additions & 0 deletions integration/tcp/src/client/app.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Module } from '@nestjs/common';
import { ClientsModule, Transport } from '@nestjs/microservices';
import { OgmaModule } from '@ogma/nestjs-module';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
imports: [
ClientsModule.register([
{
name: 'CLIENT_SERVICE',
transport: Transport.TCP,
options: { port: 3001 },
},
]),
OgmaModule.forRoot({
interceptor: false,
}),
],
providers: [AppService],
controllers: [AppController],
})
export class AppModule {}
24 changes: 24 additions & 0 deletions integration/tcp/src/client/app.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Inject, Injectable, OnApplicationBootstrap } from '@nestjs/common';
import { ClientProxy } from '@nestjs/microservices';
import { Observable } from 'rxjs';

@Injectable()
export class AppService implements OnApplicationBootstrap {
constructor(@Inject('CLIENT_SERVICE') private readonly client: ClientProxy) {}

async onApplicationBootstrap() {
await this.client.connect();
}

getHello(): Observable<string> {
return this.client.send<string>({ cmd: 'message' }, '');
}

getError(): Observable<any> {
return this.client.send({ cmd: 'error' }, '');
}

getSkip(): Observable<string> {
return this.client.send({ cmd: 'skip' }, '');
}
}
12 changes: 12 additions & 0 deletions integration/tcp/src/client/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { OgmaService } from '@ogma/nestjs-module';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
const logger = app.get(OgmaService);
await app.listen(3000);
logger.log(`TCP Microservice client listening at ${await app.getUrl()}`);
}

bootstrap();
30 changes: 30 additions & 0 deletions integration/tcp/src/server/app.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {
BadRequestException,
Controller,
UseInterceptors,
} from '@nestjs/common';
import { MessagePattern } from '@nestjs/microservices';
import { OgmaInterceptor, OgmaSkip } from '@ogma/nestjs-module';
import { AppService } from './app.service';

@UseInterceptors(OgmaInterceptor)
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}

@MessagePattern({ cmd: 'message' })
getMessage() {
return this.appService.getHello();
}

@MessagePattern({ cmd: 'error' })
getError() {
throw new BadRequestException('Borked');
}

@OgmaSkip()
@MessagePattern({ cmd: 'skip' })
getSkip() {
return this.appService.getHello();
}
}
18 changes: 18 additions & 0 deletions integration/tcp/src/server/app.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Module } from '@nestjs/common';
import { OgmaModule } from '@ogma/nestjs-module';
import { TcpParser } from '@ogma/platform-tcp';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
imports: [
OgmaModule.forRoot({
interceptor: {
rpc: TcpParser,
},
}),
],
providers: [AppService],
controllers: [AppController],
})
export class AppModule {}
8 changes: 8 additions & 0 deletions integration/tcp/src/server/app.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Injectable } from '@nestjs/common';

@Injectable()
export class AppService {
getHello() {
return 'Hello World!';
}
}
21 changes: 21 additions & 0 deletions integration/tcp/src/server/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { NestFactory } from '@nestjs/core';
import { MicroserviceOptions, Transport } from '@nestjs/microservices';
import { OgmaService } from '@ogma/nestjs-module';
import { AppModule } from './app.module';

async function bootstrap() {
const app = await NestFactory.createMicroservice<MicroserviceOptions>(
AppModule,
{
transport: Transport.TCP,
options: {
port: 3001,
},
},
);
const logger = app.get(OgmaService);
await app.listenAsync();
logger.log(`Microservice server listening at port 3001`);
}

bootstrap();
17 changes: 17 additions & 0 deletions integration/tcp/testScript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const { request } = require('http');

const baseUrl = 'http://localhost:3000/';

function cb(res) {
let data = '';
res.on('data', (chunk) => (data += chunk));
res.on('end', () => console.log(data));
}

function makeCall(url) {
request(baseUrl + url, cb).end();
}

makeCall('');
makeCall('error');
makeCall('skip');
3 changes: 3 additions & 0 deletions integration/tcp/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./tsconfig.json"
}
9 changes: 9 additions & 0 deletions integration/tcp/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"composite": false,
"rootDir": "./src",
"outDir": "./dist"
},
"include": ["./src"]
}
Loading

0 comments on commit bf0eb6b

Please sign in to comment.