From 8b056be6e23917b39cd7fcb2c803615107292fed Mon Sep 17 00:00:00 2001 From: Gabriele Civitarese Date: Thu, 4 Apr 2019 12:35:24 +0200 Subject: [PATCH] Esempietto di comunicazione ProtocolBuffer su socket --- src/main/java/researcher/Client.java | 30 ++++++++++++++++++++++++++++ src/main/java/researcher/Server.java | 24 ++++++++++++++++++++++ src/main/proto/Researcher.proto | 21 +++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 src/main/java/researcher/Client.java create mode 100644 src/main/java/researcher/Server.java create mode 100644 src/main/proto/Researcher.proto diff --git a/src/main/java/researcher/Client.java b/src/main/java/researcher/Client.java new file mode 100644 index 0000000..7d2e5f0 --- /dev/null +++ b/src/main/java/researcher/Client.java @@ -0,0 +1,30 @@ +package researcher; + +import it.ewlab.researcher.ResearcherOuterClass.Researcher; + +import java.io.IOException; +import java.net.Socket; + +public class Client { + + public static void main(String[] args) throws IOException { + + Socket s = new Socket("localhost", 9999); + + Researcher r = + Researcher.newBuilder() + .setName("Gabriele") + .setSurname("Civitarese") + .setType(Researcher.ResearcherType.POSTDOC) + .addPaper(Researcher.Paper.newBuilder().setTitle("Activity Recognition") + .setYear(2014)) + .addPaper(Researcher.Paper.newBuilder().setTitle("Activity Recognition Again") + .setYear(2015)).build(); + + r.writeTo(s.getOutputStream()); + + s.close(); + + + } +} diff --git a/src/main/java/researcher/Server.java b/src/main/java/researcher/Server.java new file mode 100644 index 0000000..da6ce56 --- /dev/null +++ b/src/main/java/researcher/Server.java @@ -0,0 +1,24 @@ +package researcher; + +import it.ewlab.researcher.ResearcherOuterClass; + +import java.io.IOException; +import java.net.ServerSocket; +import java.net.Socket; + +public class Server { + + public static void main(String[] args) throws IOException { + + + ServerSocket serverSocket = new ServerSocket(9999); + + Socket s = serverSocket.accept(); + + ResearcherOuterClass.Researcher r = ResearcherOuterClass.Researcher.parseFrom(s.getInputStream()); + + System.out.println(r); + } + + +} diff --git a/src/main/proto/Researcher.proto b/src/main/proto/Researcher.proto new file mode 100644 index 0000000..dd37f25 --- /dev/null +++ b/src/main/proto/Researcher.proto @@ -0,0 +1,21 @@ +package it.ewlab.researcher; + +message Researcher { + required string name = 1; + required string surname = 2; + + enum ResearcherType { + PHDSTUDENT = 0; + POSTDOC = 1; + ASSISTANTPROFESSOR = 2; + } + + message Paper { + required string title = 1; + required int32 year = 2; + + } + + repeated Paper paper = 3; + optional ResearcherType type = 4; +} \ No newline at end of file -- 2.18.1