server-client.service.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { BehaviorSubject, Subject } from 'rxjs';
  2. import { ClientRequest, ConnectionAttribute, ConnectionRequest, ConnectionState, Message, OutGoingInfo, ServerRequest, State } from '../interfaces/general.interface';
  3. import { GrpcServiceMethod } from './grpc.service.method';
  4. import { BufferService } from './buffer.service';
  5. import * as dotenv from 'dotenv'
  6. import * as _ from 'lodash'
  7. dotenv.config()
  8. export class ServerClientManager {
  9. private connectionAttributes: ConnectionAttribute[] = []
  10. private grpcService: GrpcServiceMethod = new GrpcServiceMethod()
  11. private defaultServerAttribute: ServerRequest = {
  12. name: 'Default - Server',
  13. serverUrl: "localhost:3000",
  14. connectionType: 'GRPC',
  15. messageToBePublishedFromApplication: new Subject<Message>()
  16. }
  17. private defaultClientAttribute: ClientRequest = {
  18. name: 'Default - Client',
  19. targetServer: "localhost:3001",
  20. connectionType: 'GRPC',
  21. messageToBeReceivedFromRemote: new Subject<Message>()
  22. }
  23. constructor() {
  24. // logic here
  25. }
  26. public async generateConnection(request: ConnectionRequest): Promise<any> {
  27. return new Promise(async (resolve, reject) => {
  28. let initialReport: ConnectionState
  29. let reportSubject: BehaviorSubject<ConnectionState>
  30. let retransmission: BufferService
  31. // let originalRequest = JSON.parse(JSON.stringify(request))
  32. let originalRequest = _.cloneDeep(request)
  33. let database: string
  34. let response: any = { message: `Fail to complete connection generation` }
  35. let statusChain: State = 0
  36. let connectionAttribute: ConnectionAttribute
  37. if (statusChain == 0) {
  38. if (!request.server) {
  39. request.server = this.defaultServerAttribute
  40. }
  41. if (!request.client) {
  42. request.client = this.defaultClientAttribute
  43. }
  44. if (request.database) {
  45. database = request.database
  46. } else {
  47. database = request.server.name + request.client.name
  48. }
  49. /* Inject retransmission here */
  50. initialReport = { status: 'BUFFER' }
  51. reportSubject = new BehaviorSubject(initialReport)
  52. retransmission = new BufferService(request.server.messageToBePublishedFromApplication, reportSubject, database)
  53. statusChain = 1
  54. }
  55. if (statusChain == 1) {
  56. // Connection Type checking
  57. if (request.server!.connectionType != request.client!.connectionType) {
  58. console.log(`Connection Type DOES NOT MATCH!`)
  59. statusChain = 0
  60. } else {
  61. statusChain = 1
  62. }
  63. }
  64. if (statusChain == 1) {
  65. connectionAttribute = {
  66. ConnectionID: {
  67. local: request.server!.name + request.client!.name,
  68. remote: request.client!.name + request.server!.name
  69. },
  70. outGoing: {
  71. StreamID: request.server!.name,
  72. PublisherID: request.server!.name,
  73. SubscriberID: request.server!.name,
  74. MessageToBePublished: retransmission!.getMessages(),
  75. MessageToBeReceived: null
  76. },
  77. inComing: {
  78. StreamID: request.client!.name,
  79. PublisherID: request.client!.name,
  80. SubscriberID: request.client!.name,
  81. MessageToBePublished: null,
  82. MessageToBeReceived: request.client!.messageToBeReceivedFromRemote
  83. },
  84. connectionStatus: reportSubject!
  85. }
  86. statusChain = 1
  87. }
  88. if (statusChain == 1) {
  89. await this.checkConnectionAttribute(connectionAttribute!).then((res) => {
  90. if (res == true) {
  91. console.log(`Connection<${connectionAttribute.ConnectionID.local}> already exists `)
  92. }
  93. if (res == false) {
  94. this.connectionAttributes.push(connectionAttribute)
  95. console.log(`Connection ${connectionAttribute.ConnectionID.local} registered...`)
  96. response = {
  97. message: "Channel Response",
  98. requestedTo: originalRequest,
  99. data: connectionAttribute
  100. }
  101. }
  102. console.log(`There is now ${this.connectionAttributes.length} connection Attributes`)
  103. })
  104. statusChain = 1
  105. }
  106. if (statusChain == 1) {
  107. // This is default connection`
  108. if (!request.client!.connectionType) {
  109. request.client!.connectionType = 'GRPC'
  110. }
  111. // For each connection type:
  112. if (request.client!.connectionType == 'GRPC') {
  113. // this.grpcService.create(request, connectionAttribute, this.outGoingInfo)
  114. }
  115. }
  116. resolve(response);
  117. })
  118. }
  119. private async checkConnectionAttribute(connectionAttribute: ConnectionAttribute): Promise<boolean> {
  120. return new Promise((resolve) => {
  121. let result: boolean = this.connectionAttributes.some(connection =>
  122. connection.ConnectionID.local === connectionAttribute.ConnectionID.local
  123. );
  124. console.log(`Checking ${connectionAttribute.ConnectionID.local} and returns ${result}`);
  125. resolve(result);
  126. });
  127. }
  128. }