grpc1.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. import { Subject, from, interval, take } from 'rxjs';
  2. import { Message, MessageLog, ConnectionRequest } from '../interfaces/general.interface';
  3. import { GrpcServiceMethod } from '../services/grpc.service.method';
  4. import { readFileSync } from 'fs';
  5. import { ServerClientManager } from '../services/server-client.service';
  6. import mongoose from 'mongoose';
  7. // Connect to MongoDB
  8. // mongoose.connect('mongodb://localhost:27017/grpc1')
  9. // const Message = mongoose.model('Message', require('../models/message.schema'))
  10. // Subject for bidirectional communication
  11. const connectionService: ServerClientManager = new ServerClientManager()
  12. const messagesJSON: any = readFileSync('payload.json')
  13. let parsedMessages: any[] = JSON.parse(messagesJSON) // load the fake messages generated for this trial
  14. let targetserver: string = 'localhost:3001'
  15. let targetserver2: string = 'localhost:3002'
  16. let hostServer: string = 'localhost:3000'
  17. let array: any[] = [] // Used for testing
  18. let intervalToStreamOutGoingMessage: number = 15
  19. /* Simple Test: 1 to 1 */
  20. // let connectionRequest: ConnectionRequest = {
  21. // server: {
  22. // name: 'g1',
  23. // serverUrl: hostServer,
  24. // connectionType: 'GRPC',
  25. // messageToBePublishedFromApplication: new Subject<Message>()
  26. // },
  27. // client: {
  28. // name: 'g2',
  29. // targetServer: targetserver,
  30. // connectionType: 'GRPC',
  31. // messageToBeReceivedFromRemote: new Subject<Message>()
  32. // }
  33. // }
  34. // connectionService.generateConnection(connectionRequest)
  35. // let generateFakeMessagesToBePublished = stream().pipe(take(1000))
  36. // let generateFakeMessagesToBePublished = from(parsedMessages).pipe(take(5000))
  37. // generateFakeMessagesToBePublished.subscribe({
  38. // next: message => {
  39. // let payload: Message = {
  40. // id: hostServer,
  41. // message: message
  42. // }
  43. // connectionRequest.server.messageToBePublishedFromApplication.next(payload)
  44. // },
  45. // error: error => console.error(error),
  46. // complete: () => console.log(`Completed`)
  47. // })
  48. // stream().subscribe({
  49. // next: message => {
  50. // let payload = {
  51. // id: hostServer,
  52. // message: message
  53. // }
  54. // connectionRequest.server.messageToBePublishedFromApplication.next(payload)
  55. // }
  56. // })
  57. // connectionRequest.client.messageToBeReceivedFromRemote.subscribe({
  58. // next: response => {
  59. // console.log(`Received ${(response.message as MessageLog).appData.msgId} from ${connectionRequest.client.targetServer}`)
  60. // // Message.create(response)
  61. // array.push(response)
  62. // },
  63. // error: error => console.error(error),
  64. // complete: () => console.log(`Response for incoming generated. But this will never stop, and should not either.`)
  65. // })
  66. /* Complex Test: 1 to 1*/
  67. // let connectionRequest: ConnectionRequest = {
  68. // server: {
  69. // name: 'g1',
  70. // serverUrl: hostServer,
  71. // connectionType: 'GRPC',
  72. // messageToBePublishedfromApplication: new Subject<Message>()
  73. // },
  74. // client: {
  75. // name: 'g2',
  76. // targetServer: targetserver,
  77. // connectionType: 'GRPC',
  78. // messageToBeReceivedFromRemote: new Subject<Message>()
  79. // }
  80. // }
  81. // connectionService.generateConnection(connectionRequest)
  82. // setTimeout(() => {
  83. // let message = {
  84. // id: parsedMessages[10].appData.msgId,
  85. // message: parsedMessages[10] // Choose this number, because i purposely use the 11th message and change the msgPayload property to query to emulate a request
  86. // }
  87. // connectionRequest.server.messageToBePublishedfromApplication.next(message)
  88. // }, 3000)
  89. // setTimeout(() => {
  90. // let message = {
  91. // id: parsedMessages[11].appData.msgId,
  92. // message: parsedMessages[11] // Choose this number, because i purposely use the 12th message and change the msgPayload property to query to emulate a request
  93. // }
  94. // connectionRequest.server.messageToBePublishedfromApplication.next(message)
  95. // }, 4000)
  96. // Handler for the incoming Messages from the other side.
  97. // connectionRequest.client.messageToBeReceivedFromRemote.subscribe({
  98. // next: request => {
  99. // // Application logic comes here. This is where the asortment takes place, of decidiing whose messages it belongs of what it is
  100. // if ((request.message as MessageLog).appData.msgPayload == 'Query') {
  101. // generateFakeStreamResponse(request).subscribe({
  102. // next: (responseMessage: Message) => {
  103. // console.log(`Received ${(response.message as MessageLog).appData.msgId} from ${connectionRequest.client.targetServer}`)
  104. // connectionRequest.server.messageToBePublishedfromApplication.next(responseMessage)
  105. // },
  106. // error: error => console.error(error),
  107. // complete: () => {
  108. // console.log(`Stream request for ${request.id} is queued.`) // shpuld be indefinite
  109. // }
  110. // })
  111. // } else {
  112. // array.push(request)
  113. // console.log(`Received message: ${(request.message as MessageLog).appData.msgId} from ${request.id}`)
  114. // }
  115. // },
  116. // error: error => console.error(error),
  117. // complete: () => console.log(`Response for incoming generated. But this will never stop, and should not either.`)
  118. // })
  119. /* Simple Test: 1 to Many */
  120. let connectionRequest1: ConnectionRequest = {
  121. server: {
  122. name: 'G0',
  123. serverUrl: hostServer,
  124. connectionType: 'GRPC',
  125. messageToBePublishedFromApplication: new Subject<Message>()
  126. },
  127. client: {
  128. name: 'G1',
  129. targetServer: targetserver,
  130. connectionType: 'GRPC',
  131. messageToBeReceivedFromRemote: new Subject<Message>()
  132. }
  133. }
  134. let connectionRequest2: ConnectionRequest = {
  135. server: {
  136. name: 'G0',
  137. serverUrl: hostServer,
  138. connectionType: 'GRPC',
  139. messageToBePublishedFromApplication: new Subject<Message>()
  140. },
  141. client: {
  142. name: 'G2',
  143. targetServer: targetserver2,
  144. connectionType: 'GRPC',
  145. messageToBeReceivedFromRemote: new Subject<Message>()
  146. }
  147. }
  148. connectionService.generateConnection(connectionRequest1).then((res) => {
  149. // console.log(res)
  150. })
  151. connectionService.generateConnection(connectionRequest2).then((res) => {
  152. // console.log(res)
  153. })
  154. let generateFakeMessagesToBePublished = from(parsedMessages).pipe(take(50))
  155. generateFakeMessagesToBePublished.subscribe({
  156. next: message => {
  157. let payload: Message = {
  158. id: hostServer,
  159. message: message
  160. }
  161. // connectionRequest1.server!.messageToBePublishedFromApplication.next(payload)
  162. // connectionRequest2.server!.messageToBePublishedFromApplication.next(payload)
  163. },
  164. error: error => console.error(error),
  165. complete: () => console.log(`Completed`)
  166. })
  167. stream().subscribe({
  168. next: message => {
  169. let payload = {
  170. id: hostServer,
  171. message: message
  172. }
  173. connectionRequest1.server!.messageToBePublishedFromApplication.next(payload)
  174. connectionRequest2.server!.messageToBePublishedFromApplication.next(payload)
  175. }
  176. })
  177. // connectionRequest.client.messageToBeReceivedFromRemote.subscribe({
  178. // next: request => {
  179. // console.log(`Received ${(request.message as MessageLog).appData.msgId} from ${connectionRequest.client.targetServer}`)
  180. // array.push(request)
  181. // },
  182. // error: error => console.error(error),
  183. // complete: () => console.log(`Response for incoming generated. But this will never stop, and should not either.`)
  184. // })
  185. // connectionRequest2.client.messageToBeReceivedFromRemote.subscribe({
  186. // next: request => {
  187. // console.log(`Received ${(request.message as MessageLog).appData.msgId} from ${connectionRequest.client.targetServer}`)
  188. // array.push(request)
  189. // },
  190. // error: error => console.error(error),
  191. // complete: () => console.log(`Response for incoming generated. But this will never stop, and should not either.`)
  192. // })
  193. /* Complex Test: 1 to Many */
  194. // let connectionRequest: ConnectionRequest = {
  195. // server: {
  196. // name: 'g1',
  197. // serverUrl: hostServer,
  198. // connectionType: 'GRPC',
  199. // messageToBePublishedfromApplication: new Subject<Message>()
  200. // },
  201. // client: {
  202. // name: 'g2',
  203. // targetServer: targetserver,
  204. // connectionType: 'GRPC',
  205. // messageToBeReceivedFromRemote: new Subject<Message>()
  206. // }
  207. // }
  208. // let connectionRequest2: ConnectionRequest = {
  209. // server: {
  210. // name: 'g1',
  211. // serverUrl: hostServer,
  212. // connectionType: 'GRPC',
  213. // messageToBePublishedfromApplication: new Subject<Message>()
  214. // },
  215. // client: {
  216. // name: 'g3',
  217. // targetServer: targetserver2,
  218. // connectionType: 'GRPC',
  219. // messageToBeReceivedFromRemote: new Subject<Message>()
  220. // }
  221. // }
  222. // connectionService.generateConnection(connectionRequest)
  223. // connectionService.generateConnection(connectionRequest2)
  224. // setTimeout(() => {
  225. // let message = {
  226. // id: parsedMessages[10].appData.msgId,
  227. // message: parsedMessages[10] // Choose this number, because i purposely use the 11th message and change the msgPayload property to query to emulate a request
  228. // }
  229. // connectionRequest.server.messageToBePublishedfromApplication.next(message)
  230. // connectionRequest2.server.messageToBePublishedfromApplication.next(message)
  231. // }, 3000)
  232. // setTimeout(() => {
  233. // let message = {
  234. // id: parsedMessages[11].appData.msgId,
  235. // message: parsedMessages[11] // Choose this number, because i purposely use the 12th message and change the msgPayload property to query to emulate a request
  236. // }
  237. // connectionRequest.server.messageToBePublishedfromApplication.next(message)
  238. // connectionRequest2.server.messageToBePublishedfromApplication.next(message)
  239. // }, 4000)
  240. // connectionRequest.client.messageToBeReceivedFromRemote.subscribe({
  241. // next: request => {
  242. // // Application logic comes here. This is where the asortment takes place, of deciding whose messages it belongs of what it is
  243. // if ((request.message as MessageLog).appData.msgPayload == 'Query') {
  244. // generateFakeStreamResponse(request).subscribe({
  245. // next: (responseMessage: Message) => {
  246. // connectionRequest.server.messageToBePublishedfromApplication.next(responseMessage)
  247. // },
  248. // error: error => console.error(error),
  249. // complete: () => {
  250. // console.log(`Stream request for ${request.id} is queued.`) // should be indefinite
  251. // }
  252. // })
  253. // } else {
  254. // console.log(`Received ${(response.message as MessageLog).appData.msgId} from ${connectionRequest.client.targetServer}`)
  255. // array.push(request)
  256. // console.log(`Received message: ${(request.message as MessageLog).appData.msgId} from ${request.id}`)
  257. // }
  258. // },
  259. // error: error => console.error(error),
  260. // complete: () => console.log(`Response for incoming generated. But this will never stop, and should not either.`)
  261. // })
  262. // connectionRequest2.client.messageToBeReceivedFromRemote.subscribe({
  263. // next: request => {
  264. // // Application logic comes here. This is where the asortment takes place, of deciding whose messages it belongs of what it is
  265. // if ((request.message as MessageLog).appData.msgPayload == 'Query') {
  266. // generateFakeStreamResponse(request).subscribe({
  267. // next: (responseMessage: Message) => {
  268. // connectionRequest2.server.messageToBePublishedfromApplication.next(responseMessage)
  269. // },
  270. // error: error => console.error(error),
  271. // complete: () => {
  272. // console.log(`Stream request for ${request.id} is queued.`) // should be indefinite
  273. // }
  274. // })
  275. // } else {
  276. // console.log(`Received ${(response.message as MessageLog).appData.msgId} from ${connectionRequest.client.targetServer}`)
  277. // array.push(request)
  278. // console.log(`Received message: ${(request.message as MessageLog).appData.msgId} from ${request.id}`)
  279. // }
  280. // },
  281. // error: error => console.error(error),
  282. // complete: () => console.log(`Response for incoming generated. But this will never stop, and should not either.`)
  283. // })
  284. // this is just to publish an array of fake data as a Subject
  285. function stream(): Subject<any> {
  286. let result: Subject<any> = new Subject()
  287. let messages: any[] = parsedMessages
  288. let count = 0
  289. const intervalId = setInterval(() => {
  290. result.next(messages[count]);
  291. count++;
  292. if (count >= 300) {
  293. clearInterval(intervalId);
  294. result.complete();
  295. }
  296. }, intervalToStreamOutGoingMessage)
  297. return result
  298. }
  299. function generateFakeStreamResponse(request: any): Subject<any> {
  300. let res: Subject<any> = new Subject()
  301. stream().pipe(take(7)).subscribe({
  302. next: element => {
  303. let message = {
  304. id: request.id, // Caller's
  305. message: element
  306. }
  307. res.next(message)
  308. },
  309. error: error => console.error(error),
  310. complete: () => console.log(`Stream response for ${request.id} has been prepared.`)
  311. })
  312. return res
  313. }
  314. /* Checking the values by the end of the test */
  315. interval(5000).subscribe(() => {
  316. console.log(`All received data: ${array.length}`);
  317. });