server-client.service.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import { BehaviorSubject, Subject } from 'rxjs';
  2. import { ClientRequest, ConnectionAttribute, ConnectionRequest, ConnectionState, Message, 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. // this.generateConnection({})
  25. }
  26. // create connection based on the request from the application
  27. public async generateConnection(request: ConnectionRequest): Promise<any> {
  28. return new Promise(async (resolve, reject) => {
  29. let initialReport: ConnectionState
  30. let reportSubject: BehaviorSubject<ConnectionState>
  31. let retransmission: BufferService
  32. let errorString: string
  33. let originalRequest = _.cloneDeep(request)
  34. let database: string
  35. let response: any = { message: `Fail to complete connection generation` }
  36. let statusChain: State = 1
  37. let connectionAttribute: ConnectionAttribute
  38. if (statusChain == 1) {
  39. if (!request.server) {
  40. request.server = this.defaultServerAttribute
  41. }
  42. if (!request.client) {
  43. request.client = this.defaultClientAttribute
  44. }
  45. if (request.database) {
  46. database = request.database
  47. } else {
  48. database = request.server.name + request.client.name
  49. }
  50. /* Inject retransmission here */
  51. initialReport = { status: 'BUFFER' }
  52. reportSubject = new BehaviorSubject(initialReport)
  53. retransmission = new BufferService(request.server.messageToBePublishedFromApplication, reportSubject, database)
  54. }
  55. if (statusChain == 1) {
  56. // Connection Type checking
  57. if (request.server!.connectionType != request.client!.connectionType) {
  58. statusChain = -1
  59. errorString = "Connection Type DOES NOT MATCH!"
  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. serverUrl: request.server?.serverUrl,
  75. MessageToBePublished: retransmission!.getMessages(),
  76. MessageToBeReceived: null
  77. },
  78. inComing: {
  79. StreamID: request.client!.name,
  80. PublisherID: request.client!.name,
  81. SubscriberID: request.client!.name,
  82. serverUrl: request.client?.targetServer,
  83. MessageToBePublished: null,
  84. MessageToBeReceived: request.client!.messageToBeReceivedFromRemote
  85. },
  86. connectionStatus: reportSubject!
  87. }
  88. }
  89. if (statusChain == 1) {
  90. // to prevent duplicate connection from being created
  91. await this.checkConnectionAttribute(connectionAttribute!).then((res) => {
  92. if (res == true) {
  93. console.log(`Connection<${connectionAttribute.ConnectionID.local}> already exists `)
  94. }
  95. if (res == false) {
  96. this.connectionAttributes.push(connectionAttribute)
  97. console.log(`Connection ${connectionAttribute.ConnectionID.local} registered...`)
  98. }
  99. console.log(`There is now ${this.connectionAttributes.length} connection Attributes`)
  100. })
  101. }
  102. if (statusChain == 1) {
  103. // This is default connection`
  104. if (!request.client!.connectionType) {
  105. request.client!.connectionType = 'GRPC'
  106. }
  107. // For each connection type:
  108. if (request.client!.connectionType == 'GRPC') {
  109. this.grpcService.create(connectionAttribute!, this.connectionAttributes).then(() => {
  110. // logic here
  111. }).catch(() => {
  112. errorString = `Something wrong with gRPC methods`
  113. statusChain = -1
  114. })
  115. }
  116. }
  117. if (statusChain == 1) {
  118. response = {
  119. message: "Channel Response",
  120. requestedTo: originalRequest,
  121. data: connectionAttribute!
  122. }
  123. resolve(response);
  124. } else if (statusChain == -1) {
  125. response = {
  126. message: "Channel Response Error",
  127. requestedTo: originalRequest,
  128. data: errorString! // put error string here
  129. }
  130. resolve(response);
  131. }
  132. })
  133. }
  134. private async checkConnectionAttribute(connectionAttribute: ConnectionAttribute): Promise<boolean> {
  135. return new Promise((resolve) => {
  136. let result: boolean = this.connectionAttributes.some(connection =>
  137. connection.ConnectionID.local === connectionAttribute.ConnectionID.local
  138. );
  139. resolve(result);
  140. });
  141. }
  142. }