diff --git a/packages/server/output.json b/packages/server/output.json new file mode 100644 index 0000000..6db759b --- /dev/null +++ b/packages/server/output.json @@ -0,0 +1,17 @@ +[ + { + "index_val": 1, + "my_val": 421, + "their_val": 991 + }, + { + "index_val": 3, + "my_val": 422, + "their_val": 991 + }, + { + "index_val": 7, + "my_val": 427, + "their_val": 991 + } +] \ No newline at end of file diff --git a/packages/server/sample1.json b/packages/server/sample1.json new file mode 100644 index 0000000..68b5387 --- /dev/null +++ b/packages/server/sample1.json @@ -0,0 +1,24 @@ +{ + "r1": [ + [ + 1, + 421 + ], + [ + 2, + 422 + ], + [ + 3, + 422 + ], + [ + 4, + 423 + ], + [ + 7, + 427 + ] + ] +} \ No newline at end of file diff --git a/packages/server/sample2.json b/packages/server/sample2.json new file mode 100644 index 0000000..41ca5d1 --- /dev/null +++ b/packages/server/sample2.json @@ -0,0 +1,20 @@ +{ + "r2": [ + [ + 1, + 991 + ], + [ + 3, + 991 + ], + [ + 5, + 991 + ], + [ + 7, + 991 + ] + ] +} \ No newline at end of file diff --git a/packages/server/src/app.module.ts b/packages/server/src/app.module.ts index 63b9843..20d680b 100644 --- a/packages/server/src/app.module.ts +++ b/packages/server/src/app.module.ts @@ -10,6 +10,10 @@ import { CollectionModule } from './collection/collection.module'; import { join } from 'path'; import { ShareModule } from './share/share.module'; +import { MpcService } from './mpc/mpc.service'; +import { MpcController } from './mpc/mpc.controller'; +import { MpcModule } from './mpc/mpc.module'; + const MONGO_DB_HOST = 'mongodb-server:27017'; //matches mongo server name and port in docker-compose file const MONGO_DB_NAME = 'maindb'; const DATABASE_ULR = 'mongodb://' + MONGO_DB_HOST + '/' + MONGO_DB_NAME; @@ -23,9 +27,10 @@ const DATABASE_ULR = 'mongodb://' + MONGO_DB_HOST + '/' + MONGO_DB_NAME; }), ParticipantModule, CollectionModule, - ShareModule + ShareModule, + MpcModule, ], - controllers: [AppController], - providers: [AppService], + controllers: [AppController, MpcController], + providers: [AppService, MpcService], }) export class AppModule {} diff --git a/packages/server/src/mpc/mpc.controller.ts b/packages/server/src/mpc/mpc.controller.ts new file mode 100644 index 0000000..dd71c61 --- /dev/null +++ b/packages/server/src/mpc/mpc.controller.ts @@ -0,0 +1,18 @@ +import { Controller, Post, Body, Res } from '@nestjs/common'; +import { MpcService } from './mpc.service'; +import { Response } from 'express'; + +@Controller('mpc') +export class MpcController { + constructor(private readonly mpcService: MpcService) {} + + @Post('run') + async runMpc(@Body() body: { data1: any; data2: any }, @Res() res: Response) { + try { + const output = await this.mpcService.runMpc(body.data1, body.data2); + res.status(200).json({ output }); + } catch (error) { + res.status(500).json({ message: error }); + } + } +} diff --git a/packages/server/src/mpc/mpc.module.ts b/packages/server/src/mpc/mpc.module.ts new file mode 100644 index 0000000..142b658 --- /dev/null +++ b/packages/server/src/mpc/mpc.module.ts @@ -0,0 +1,9 @@ +import { Module } from '@nestjs/common'; +import { MpcService } from './mpc.service'; +import { MpcController } from './mpc.controller'; + +@Module({ + providers: [MpcService], + controllers: [MpcController], +}) +export class MpcModule {} diff --git a/packages/server/src/mpc/mpc.service.ts b/packages/server/src/mpc/mpc.service.ts new file mode 100644 index 0000000..7e6d42e --- /dev/null +++ b/packages/server/src/mpc/mpc.service.ts @@ -0,0 +1,46 @@ +import { Injectable } from '@nestjs/common'; +import { spawn } from 'child_process'; +import * as fs from 'fs'; +import * as path from 'path'; + +@Injectable() +export class MpcService { + async runMpc(data1: any, data2: any): Promise { + const filePath1 = path.join(process.cwd(), 'sample1.json'); + const filePath2 = path.join(process.cwd(), 'sample2.json'); + const jsonString1 = JSON.stringify({ data1 }, null, 2); + const jsonString2 = JSON.stringify({ data2 }, null, 2); + + await fs.promises.writeFile(filePath1, jsonString1); + await fs.promises.writeFile(filePath2, jsonString2); + + return new Promise((resolve, reject) => { + const command = 'mpirun'; + const args = ['-np', '3', './test_join_sail', filePath1, filePath2]; + + const mpcProcess = spawn(command, args); + + let output = ''; + + mpcProcess.stdout.on('data', (data) => { + output += data.toString(); + }); + + mpcProcess.stderr.on('data', (data) => { + console.error(`stderr: ${data}`); + }); + + mpcProcess.on('close', (code) => { + if (code === 0) { + resolve(output); + } else { + reject(`Process exited with code ${code}`); + } + }); + + mpcProcess.on('error', (error) => { + reject(`Error: ${error.message}`); + }); + }); + } +} diff --git a/packages/server/src/temp.ts b/packages/server/src/temp.ts new file mode 100644 index 0000000..c692214 --- /dev/null +++ b/packages/server/src/temp.ts @@ -0,0 +1,53 @@ +import * as fs from 'fs'; +import * as path from 'path'; +import { spawn } from 'child_process'; + +// Function to write JSON files +const writeJsonFile = async (filePath, data) => { + const jsonString = JSON.stringify(data, null, 2); + await fs.promises.writeFile(filePath, jsonString); +}; + +const runMpc = async () => { + const r1 = [[1, 421], [2, 422], [3, 422], [4, 423], [7, 427]]; + const data1 = { r1 }; + const filePath1 = path.join("./", 'sample1.json'); + const r2 = [[1, 991], [3, 991], [5, 991], [7, 991]]; + const data2 = { r2 }; + const filePath2 = path.join('./', 'sample2.json'); + + // Write the JSON files + await writeJsonFile(filePath1, data1); + await writeJsonFile(filePath2, data2); + + // Define the command and arguments + const command = 'mpirun'; + const args = ['-np', '3', '/Users/hishii/Desktop/MT/Secrecy/build/test_join_sail', filePath1, filePath2]; + // Spawn the process + const process = spawn(command, args); + + // Handle stdout + process.stdout.on('data', (data) => { + console.log(`stdout: ${data}`); + }); + + // Handle stderr + process.stderr.on('data', (data) => { + console.error(`stderr: ${data}`); + }); + + // Handle process exit + process.on('close', (code) => { + console.log(`Process exited with code ${code}`); + }); + + // Handle process errors + process.on('error', (error) => { + console.error(`Error: ${error.message}`); + }); +}; + +// Run the MPC process +runMpc().catch((error) => { + console.error(`Error running MPC process: ${error.message}`); +});