Commit 5c8dc9c8 authored by areej.mohammad's avatar areej.mohammad

initial

parents
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
\ No newline at end of file
# Default ignored files
/shelf/
/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>protobuf_example</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- مكتبة Protobuf -->
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>4.28.2</version>
</dependency>
<!-- مكتبة مساعدة لحل مشاكل Accessors -->
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java-util</artifactId>
<version>3.25.3</version>
</dependency>
<!-- مكتبات gRPC -->
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty-shaded</artifactId>
<version>1.63.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>1.63.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>1.63.0</version>
</dependency>
<!-- javax.annotation لدعم Java 17+ -->
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.7.0</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.6.1</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.25.3:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.63.0:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
package org.example;
// --- هذه هي السطور المهمة التي تحل المشكلة ---
import com.example.book.Book;
import com.google.protobuf.InvalidProtocolBufferException;
import java.util.Arrays;
import java.nio.charset.StandardCharsets;
// --- نهاية السطور المهمة ---
public class Main {
public static void main(String[] args) {
// --- الخطوة 1: إنشاء كائن (Instance) من رسالة Book ---
// 1a. بناء الرسالة المتداخلة (Nested Message) أولاً
Book.Publisher publisher = Book.Publisher.newBuilder()
.setName("O'Reilly")
.setLocation("USA")
.build();
// 1b. بناء الرسالة الرئيسية (Book)
Book myBook = Book.newBuilder()
.setTitle("gRPC: The Definitive Guide")
.setAuthor("Ray")
.setIsbn("123-456-789")
.setPublicationYear(2025)
.setGenre(Book.Genre.SCIENCE) // استخدام الـ Enum
.setPublisher(publisher) // استخدام الـ Nested Message
.build();
System.out.println("--- 1. الكائن الأصلي (قبل الترميز) ---");
System.out.println(myBook);
// --- الخطوة 2: ترميز الكائن إلى مصفوفة بايتات (Serialization) ---
byte[] bookAsBytes = myBook.toByteArray();
System.out.println("\n--- 2. عملية الترميز (Encode) ---");
System.out.println("تم الترميز إلى " + bookAsBytes.length + " بايت");
System.out.println("شكل البايتات: " + Arrays.toString(bookAsBytes));
// --- الخطوة 3: فك ترميز البايتات رجوعاً إلى كائن (Deserialization) ---
try {
// نستخدم parseFrom لتحويل المصفوفة إلى كائن Book
Book decodedBook = Book.parseFrom(bookAsBytes);
System.out.println("\n--- 3. الكائن (بعد فك الترميز) ---");
System.out.println(decodedBook);
// --- 4. التحقق من أن البيانات متطابقة ---
System.out.println("\n--- 4. التحقق من البيانات ---");
System.out.println("هل الكائن الأصلي يساوي الكائن المترجم؟ " + myBook.equals(decodedBook));
System.out.println("اسم الناشر (من الكائن المترجم): " + decodedBook.getPublisher().getName());
} catch (InvalidProtocolBufferException e) {
System.err.println("حدث خطأ أثناء فك الترميز: " + e.getMessage());
}
// --- الخطوة 4: (الطلب الجديد) المقارنة مع JSON ---
// (الطلب الثالث - مقارنة الحجم)
String bookAsJson = """
{
"title": "gRPC: The Definitive Guide",
"author": "Ray",
"isbn": "123-456-789",
"publication_year": 2025,
"genre": "SCIENCE",
"publisher": {
"name": "O'Reilly",
"location": "USA"
}
}
""";
int jsonSize = bookAsJson.getBytes(StandardCharsets.UTF_8).length;
int protoSize = bookAsBytes.length; // الحجم من الخطوة 2
System.out.println("\n--- 4. مقارنة حجم Protobuf مع JSON ---");
System.out.println("حجم البيانات كـ Protobuf: " + protoSize + " bytes");
System.out.println("حجم البيانات كـ JSON: " + jsonSize + " bytes");
double saving = 100.0 * (jsonSize - protoSize) / jsonSize;
System.out.println("\nالنتيجة:");
System.out.println("Protobuf أصغر بـ " + (jsonSize - protoSize) + " بايت.");
System.out.printf("أي أن Protobuf يوفر حوالي %.1f%% من حجم البيانات مقارنة بـ JSON.\n", saving);
}
}
\ No newline at end of file
syntax = "proto3";
option java_multiple_files = true;
option java_package = "com.example.book";
service BookService {
// RPC function to fetch a book using its ISBN
rpc GetBook(GetBookRequest) returns (Book) {}
}
message GetBookRequest {
string isbn = 1;
}
message Book {
enum Genre {
GENRE_UNSPECIFIED = 0;
FICTION = 1;
NON_FICTION = 2;
SCIENCE = 3;
HISTORY = 4;
}
message Publisher {
string name = 1;
string location = 2;
}
string title = 1;
string author = 2;
string isbn = 3;
int32 publication_year = 4;
Genre genre = 5;
Publisher publisher = 6;
}
\ No newline at end of file
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