Skip to content

Commit

Permalink
Pushing all I created (will delete unnecessary files later)
Browse files Browse the repository at this point in the history
  • Loading branch information
Snafkin547 committed Jul 30, 2024
1 parent 41bfc06 commit d8ba06b
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 3 deletions.
17 changes: 17 additions & 0 deletions packages/server/output.json
Original file line number Diff line number Diff line change
@@ -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
}
]
24 changes: 24 additions & 0 deletions packages/server/sample1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"r1": [
[
1,
421
],
[
2,
422
],
[
3,
422
],
[
4,
423
],
[
7,
427
]
]
}
20 changes: 20 additions & 0 deletions packages/server/sample2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"r2": [
[
1,
991
],
[
3,
991
],
[
5,
991
],
[
7,
991
]
]
}
11 changes: 8 additions & 3 deletions packages/server/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {}
18 changes: 18 additions & 0 deletions packages/server/src/mpc/mpc.controller.ts
Original file line number Diff line number Diff line change
@@ -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 });
}
}
}
9 changes: 9 additions & 0 deletions packages/server/src/mpc/mpc.module.ts
Original file line number Diff line number Diff line change
@@ -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 {}
46 changes: 46 additions & 0 deletions packages/server/src/mpc/mpc.service.ts
Original file line number Diff line number Diff line change
@@ -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<string> {
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}`);
});
});
}
}
53 changes: 53 additions & 0 deletions packages/server/src/temp.ts
Original file line number Diff line number Diff line change
@@ -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}`);
});

0 comments on commit d8ba06b

Please sign in to comment.