Commit 6bfb7ac4 authored by tammam.alsoleman's avatar tammam.alsoleman

Last update

parent 484f833c
import sys import sys
import os import os
# إضافة مسار المجلد generated إلى Python path
sys.path.append(os.path.join(os.path.dirname(__file__), 'generated')) sys.path.append(os.path.join(os.path.dirname(__file__), 'generated'))
...@@ -102,7 +101,7 @@ def main(): ...@@ -102,7 +101,7 @@ def main():
client.get_history() client.get_history()
elif choice == "3": elif choice == "3":
print("👋 Goodbye!") print("Goodbye!")
break break
else: else:
......
...@@ -3,7 +3,6 @@ const protoLoader = require('@grpc/proto-loader'); ...@@ -3,7 +3,6 @@ const protoLoader = require('@grpc/proto-loader');
const path = require('path'); const path = require('path');
const fs = require('fs').promises; const fs = require('fs').promises;
// تحميل بروتوكول gRPC
const PROTO_PATH = path.join(__dirname, '../proto/calculator.proto'); const PROTO_PATH = path.join(__dirname, '../proto/calculator.proto');
const packageDefinition = protoLoader.loadSync(PROTO_PATH, { const packageDefinition = protoLoader.loadSync(PROTO_PATH, {
keepCase: true, keepCase: true,
...@@ -14,11 +13,10 @@ const packageDefinition = protoLoader.loadSync(PROTO_PATH, { ...@@ -14,11 +13,10 @@ const packageDefinition = protoLoader.loadSync(PROTO_PATH, {
}); });
const calculatorProto = grpc.loadPackageDefinition(packageDefinition).calculator; const calculatorProto = grpc.loadPackageDefinition(packageDefinition).calculator;
// تخزين العمليات
let operations = []; let operations = [];
class MultiplicationService { class MultiplicationService {
// دالة الضرب
multiply(call, callback) { multiply(call, callback) {
const { a, b } = call.request; const { a, b } = call.request;
const result = a * b; const result = a * b;
...@@ -32,10 +30,8 @@ class MultiplicationService { ...@@ -32,10 +30,8 @@ class MultiplicationService {
timestamp timestamp
}; };
// حفظ العملية في الذاكرة
operations.push(operation); operations.push(operation);
// حفظ في ملف السجل
this.logToFile(operation); this.logToFile(operation);
console.log(` Multiplication operation: ${a} × ${b} = ${result}`); console.log(` Multiplication operation: ${a} × ${b} = ${result}`);
...@@ -46,7 +42,6 @@ class MultiplicationService { ...@@ -46,7 +42,6 @@ class MultiplicationService {
}); });
} }
// دالة جلب السجل (Streaming)
getOperationHistory(call) { getOperationHistory(call) {
console.log(' Sending operation history...'); console.log(' Sending operation history...');
...@@ -55,12 +50,10 @@ class MultiplicationService { ...@@ -55,12 +50,10 @@ class MultiplicationService {
operations: operations operations: operations
}; };
// إرسال السجل
call.write(history); call.write(history);
call.end(); call.end();
} }
// دالة حفظ في ملف
async logToFile(operation) { async logToFile(operation) {
try { try {
const logEntry = `[${operation.timestamp}] ${operation.type}: ${operation.a} × ${operation.b} = ${operation.result}\n`; const logEntry = `[${operation.timestamp}] ${operation.type}: ${operation.a} × ${operation.b} = ${operation.result}\n`;
...@@ -75,13 +68,11 @@ function main() { ...@@ -75,13 +68,11 @@ function main() {
const server = new grpc.Server(); const server = new grpc.Server();
const multiplicationService = new MultiplicationService(); const multiplicationService = new MultiplicationService();
// تسجيل الخدمة
server.addService(calculatorProto.Calculator.service, { server.addService(calculatorProto.Calculator.service, {
Multiply: multiplicationService.multiply.bind(multiplicationService), Multiply: multiplicationService.multiply.bind(multiplicationService),
GetOperationHistory: multiplicationService.getOperationHistory.bind(multiplicationService) GetOperationHistory: multiplicationService.getOperationHistory.bind(multiplicationService)
}); });
// بدء الخادم
server.bindAsync( server.bindAsync(
'0.0.0.0:50053', '0.0.0.0:50053',
grpc.ServerCredentials.createInsecure(), grpc.ServerCredentials.createInsecure(),
...@@ -97,7 +88,6 @@ function main() { ...@@ -97,7 +88,6 @@ function main() {
); );
} }
// معالجة إغلاق التطبيق بشكل أنيق
process.on('SIGINT', () => { process.on('SIGINT', () => {
console.log('\n Shutting down Multiplication Service...'); console.log('\n Shutting down Multiplication Service...');
process.exit(0); process.exit(0);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment