Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
P
Protocol Buffer
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tammam.alsoleman
Protocol Buffer
Commits
99843415
Commit
99843415
authored
Nov 05, 2025
by
tammam.alsoleman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
write Main & edit book.proto
parent
60107f26
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
136 additions
and
15 deletions
+136
-15
Main.java
src/main/java/org/example/Main.java
+87
-11
book.proto
src/main/proto/book.proto
+49
-4
No files found.
src/main/java/org/example/Main.java
View file @
99843415
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
src/main/proto/book.proto
View file @
99843415
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment