Commit 99843415 authored by tammam.alsoleman's avatar tammam.alsoleman

write Main & edit book.proto

parent 60107f26
package org.example; package org.example;
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or import com.google.protobuf.InvalidProtocolBufferException;
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter. import org.ds.proto.Author;
import org.ds.proto.Book;
import org.ds.proto.BookType;
import org.ds.proto.Library;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) throws InvalidProtocolBufferException {
//TIP Press <shortcut actionId="ShowIntentionActions"/> with your caret at the highlighted text byte[] msg = sender();
// to see how IntelliJ IDEA suggests fixing it. receiver(msg);
System.out.printf("Hello and welcome!"); }
for (int i = 1; i <= 5; i++) { private static void receiver(byte[] msg) throws InvalidProtocolBufferException {
//TIP Press <shortcut actionId="Debug"/> to start debugging your code. We have set one <icon src="AllIcons.Debugger.Db_set_breakpoint"/> breakpoint System.out.println("Received Data:");
// for you, but you can always add more by pressing <shortcut actionId="ToggleLineBreakpoint"/>. // Display binary data
System.out.println("i = " + i); for (int i = 0; i < msg.length; i++) {
System.out.print(msg[i] + " ");
}
System.out.println();
System.out.println("Total bytes: " + msg.length);
// Deserialize
var library = Library.parseFrom(msg);
System.out.println("Library contents:");
System.out.println(library.getBooksList());
// Display detailed book information
System.out.println("\nDetailed Book Information:");
for (Book book : library.getBooksList()) {
System.out.println("Title: " + book.getTitle());
System.out.println("Author: " + book.getAuthor().getName() + " from " + book.getAuthor().getCountry());
System.out.println("ISBN: " + book.getIsbn());
System.out.println("Year: " + book.getYear());
System.out.println("Type: " + book.getType());
System.out.println("Categories: " + book.getCategoriesList());
System.out.println("Available: " + book.getAvailable());
System.out.println("---");
}
} }
private static byte[] sender() {
// Create first book
Book book1 = Book.newBuilder()
.setTitle("Protocol Buffers in Distributed Systems")
.setAuthor(Author.newBuilder()
.setName("Ahmed Networks")
.setCountry("Egypt")
.build())
.setIsbn("978-1234567890")
.setYear(2024)
.setType(BookType.TECHNICAL)
.addCategories("Networks")
.addCategories("Programming")
.setAvailable(true)
.build();
// Create second book
Book book2 = Book.newBuilder()
.setTitle("gRPC Fundamentals and Communications")
.setAuthor(Author.newBuilder()
.setName("Mohamed Developer")
.setCountry("Saudi Arabia")
.build())
.setIsbn("978-0987654321")
.setYear(2023)
.setType(BookType.SCIENCE)
.addCategories("Communications")
.addCategories("API")
.setAvailable(true)
.build();
// Create library and add books
Library library = Library.newBuilder()
.addBooks(book1)
.addBooks(book2)
.build();
// Serialize to byte array
byte[] arrayToSend = library.toByteArray();
// Display sent data
System.out.println(" Sent Data:");
for (int i = 0; i < arrayToSend.length; i++) {
System.out.print(arrayToSend[i] + " ");
}
System.out.println();
System.out.println(" Total bytes to send: " + arrayToSend.length);
return arrayToSend;
} }
} }
\ No newline at end of file
syntax = "proto3"; syntax = "proto3";
option java_multiple_files = true; option java_multiple_files = true;
option java_package = "org.ds.proto"; option java_package = "org.ds.proto";
// enum جديد // Enum for book types
enum BookType { enum BookType {
UNKNOWN = 0; UNKNOWN = 0;
FICTION = 1; FICTION = 1;
SCIENCE = 2; SCIENCE = 2;
TECHNICAL = 3; TECHNICAL = 3;
HISTORY = 4;
} }
// رسالة متداخلة (Nested Message) // Nested message for author
message Author { message Author {
string name = 1; string name = 1;
string country = 2; string country = 2;
} }
// الرسالة الرئيسية // Main book message
message Book { message Book {
string title = 1; string title = 1;
Author author = 2; // nested message Author author = 2; // nested message
...@@ -27,7 +29,50 @@ message Book { ...@@ -27,7 +29,50 @@ message Book {
bool available = 7; bool available = 7;
} }
// مجموعة كتب // Book collection
message Library { message Library {
repeated Book books = 1; repeated Book books = 1;
} }
// Service definition for library management
service LibraryService {
// Simple RPC - Add a book
rpc AddBook (AddBookRequest) returns (AddBookResponse);
// Simple RPC - Get a book by ISBN
rpc GetBook (GetBookRequest) returns (Book);
// Server streaming RPC - Get all books
rpc GetAllBooks (GetAllBooksRequest) returns (stream Book);
// Client streaming RPC - Add multiple books
rpc AddMultipleBooks (stream Book) returns (AddBookResponse);
// Bidirectional streaming RPC - Process books
rpc ProcessBooks (stream Book) returns (stream BookProcessingResponse);
}
// Request and response messages for the service
message AddBookRequest {
Book book = 1;
}
message AddBookResponse {
bool success = 1;
string message = 2;
string book_id = 3;
}
message GetBookRequest {
string isbn = 1;
}
message GetAllBooksRequest {
// Can add filters here if needed
}
message BookProcessingResponse {
string isbn = 1;
bool processed = 2;
string status = 3;
}
\ 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