server-client.service.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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.grpcService.getLocalServerStatus().subscribe({
  25. next: (notification: any) => {
  26. console.log(`Received a update from Grpc Server methods`)
  27. if (notification.connectionStatus === `ON`) {
  28. let connectionAttribute = this.connectionAttributes.find(connection => connection.outGoing.PublisherID == notification.serverName)
  29. connectionAttribute!.outGoing.connectionState = 'ON'
  30. console.log(notification.message)
  31. }
  32. },
  33. error: err => console.error(err),
  34. complete: () => { }
  35. })
  36. this.grpcService.getClientRequest().subscribe({
  37. next: (clientConnectionAttribute: ConnectionAttribute) => {
  38. console.log(`Received a request from ${clientConnectionAttribute.outGoing.serverUrl}`)
  39. let connectionAttribute = this.connectionAttributes.find(connection => connection.ConnectionID.remote == clientConnectionAttribute.ConnectionID.local)
  40. connectionAttribute!.inComing.connectionState = 'ON'
  41. // console.log(clientConnectionAttribute)
  42. console.log(`Updated!!! look down...`)
  43. console.log(this.connectionAttributes.find(connection => connection.ConnectionID.remote == clientConnectionAttribute.ConnectionID.local))
  44. },
  45. error: err => console.error(err),
  46. complete: () => { }
  47. })
  48. }
  49. public async generateConnection(request: ConnectionRequest): Promise<any> {
  50. return new Promise(async (resolve, reject) => {
  51. let initialReport: ConnectionState
  52. let reportSubject: BehaviorSubject<ConnectionState>
  53. let retransmission: BufferService
  54. let errorString: string
  55. let originalRequest = _.cloneDeep(request)
  56. let database: string
  57. let response: any = { message: `Fail to complete connection generation` }
  58. let statusChain: State = 1
  59. let connectionAttribute: ConnectionAttribute
  60. if (statusChain == 1) {
  61. if (!request.server) {
  62. request.server = this.defaultServerAttribute
  63. }
  64. if (!request.client) {
  65. request.client = this.defaultClientAttribute
  66. }
  67. if (request.database) {
  68. database = request.database
  69. } else {
  70. database = request.server.name + request.client.name
  71. }
  72. /* Inject retransmission here */
  73. initialReport = { status: 'BUFFER' }
  74. reportSubject = new BehaviorSubject(initialReport)
  75. retransmission = new BufferService(request.server.messageToBePublishedFromApplication, reportSubject, database)
  76. }
  77. if (statusChain == 1) {
  78. // Connection Type checking
  79. if (request.server!.connectionType != request.client!.connectionType) {
  80. // console.log(`Connection Type DOES NOT MATCH!`)
  81. statusChain = -1
  82. errorString = "Connection Type DOES NOT MATCH!"
  83. } else {
  84. statusChain = 1
  85. }
  86. }
  87. if (statusChain == 1) {
  88. connectionAttribute = {
  89. ConnectionID: {
  90. local: request.server!.name + request.client!.name,
  91. remote: request.client!.name + request.server!.name
  92. },
  93. outGoing: {
  94. StreamID: request.server!.name,
  95. PublisherID: request.server!.name,
  96. SubscriberID: request.server!.name,
  97. serverUrl: request.server?.serverUrl,
  98. connectionState: `OFF`,
  99. MessageToBePublished: retransmission!.getMessages(),
  100. MessageToBeReceived: null
  101. },
  102. inComing: {
  103. StreamID: request.client!.name,
  104. PublisherID: request.client!.name,
  105. SubscriberID: request.client!.name,
  106. serverUrl: request.client?.targetServer,
  107. connectionState: `OFF`,
  108. MessageToBePublished: null,
  109. MessageToBeReceived: request.client!.messageToBeReceivedFromRemote
  110. },
  111. connectionStatus: reportSubject!
  112. }
  113. }
  114. if (statusChain == 1) {
  115. await this.checkConnectionAttribute(connectionAttribute!).then((res) => {
  116. if (res == true) {
  117. console.log(`Connection<${connectionAttribute.ConnectionID.local}> already exists `)
  118. }
  119. if (res == false) {
  120. this.connectionAttributes.push(connectionAttribute)
  121. console.log(`Connection ${connectionAttribute.ConnectionID.local} registered...`)
  122. }
  123. console.log(`There is now ${this.connectionAttributes.length} connection Attributes`)
  124. })
  125. }
  126. if (statusChain == 1) {
  127. // This is default connection`
  128. if (!request.client!.connectionType) {
  129. request.client!.connectionType = 'GRPC'
  130. }
  131. // For each connection type:
  132. if (request.client!.connectionType == 'GRPC') {
  133. this.grpcService.create(connectionAttribute!).then(() => {
  134. // logic here
  135. }).catch(() => {
  136. errorString = `Something wrong with gRPC methods`
  137. statusChain = -1
  138. })
  139. }
  140. }
  141. if (statusChain == 1) {
  142. response = {
  143. message: "Channel Response",
  144. requestedTo: originalRequest,
  145. data: connectionAttribute!
  146. }
  147. resolve(response);
  148. } else if (statusChain == -1) {
  149. response = {
  150. message: "Channel Response Error",
  151. requestedTo: originalRequest,
  152. data: errorString! // put error string here
  153. }
  154. resolve(response);
  155. }
  156. })
  157. }
  158. private async checkConnectionAttribute(connectionAttribute: ConnectionAttribute): Promise<boolean> {
  159. return new Promise((resolve) => {
  160. let result: boolean = this.connectionAttributes.some(connection =>
  161. connection.ConnectionID.local === connectionAttribute.ConnectionID.local
  162. );
  163. // console.log(`Checking ${connectionAttribute.ConnectionID.local} and returns ${result}`);
  164. resolve(result);
  165. });
  166. }
  167. }