-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchExampleServer.java
166 lines (119 loc) · 5.42 KB
/
SearchExampleServer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
*
*/
import java.util.Random;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import io.github.ppissias.xsrpcj.example.simple.SearchMessages.PersonNotificationRequest;
import io.github.ppissias.xsrpcj.example.simple.SearchMessages.PersonNotificationResponse;
import io.github.ppissias.xsrpcj.example.simple.SearchMessages.PersonNotificationResponse.responseEnum;
import io.github.ppissias.xsrpcj.example.simple.SearchMessages.SearchPersonRequest;
import io.github.ppissias.xsrpcj.example.simple.SearchMessages.SearchPersonResponse;
import io.github.ppissias.xsrpcj.example.simple.comms.RemoteCommunicationsException;
import io.github.ppissias.xsrpcj.example.simple.server.PersonsNotifyServerCallback;
import io.github.ppissias.xsrpcj.example.simple.server.PersonsServer;
import io.github.ppissias.xsrpcj.example.simple.server.PersonsServerService;
/**
* @author Petros Pissias
*
*/
public class SearchExampleServer {
private final Logger logger = Logger.getLogger(SearchExampleServer.class);
private final Random random = new Random();
public class ServerCallbackReplySender extends Thread {
private final Random random = new Random();
private final int hMany;
private final String name;
private final PersonsNotifyServerCallback callback;
public ServerCallbackReplySender(String name, int hMany, PersonsNotifyServerCallback callback) {
this.hMany = hMany;
this.name = name;
this.callback = callback;
}
@Override
public void run() {
for (int i=1;i<=hMany;i++) {
try {
Thread.sleep(random.nextInt(5000));
} catch (InterruptedException e1) {
}
io.github.ppissias.xsrpcj.example.simple.SearchMessages.SearchPersonResponse.Builder cbResponseBuilder = SearchPersonResponse.newBuilder();
io.github.ppissias.xsrpcj.example.simple.SearchMessages.SearchPersonResponse.Person.Builder
personBuilder = SearchPersonResponse.Person.newBuilder();
buildRandomPerson(name, personBuilder);
cbResponseBuilder.setResponse(personBuilder.build());
try {
callback.notifyCallback(cbResponseBuilder.build());
} catch (RemoteCommunicationsException e) {
logger.error("comms error",e);
}
}
}
//builds random person
private void buildRandomPerson(String fname, io.github.ppissias.xsrpcj.example.simple.SearchMessages.SearchPersonResponse.Person.Builder personBuilder) {
String[] lastNames = {"Smith","Jones","Parsons","Jackson","Anderson","Stamatiou","Lalakis","Tailor","Pirina","Glassy"};
personBuilder.setFirstName(fname);
personBuilder.setLastName(lastNames[random.nextInt(10)]);
String telephone = "0049-"+random.nextInt(10)+random.nextInt(10)+random.nextInt(10)+random.nextInt(10)+
random.nextInt(10)+random.nextInt(10)+random.nextInt(10)+"-"+random.nextInt(10)+
random.nextInt(10)+random.nextInt(10);
personBuilder.setTelephone(telephone);
}
}
/**
* @param args
* @throws RemoteCommunicationsException
*/
public static void main(String[] args) throws RemoteCommunicationsException {
//configure logging
try {
PropertyConfigurator.configure("resources/log4j.properties");
}catch (Exception e) {
e.printStackTrace();
}
//start the server
new SearchExampleServer().startServer();
}
//defines the service implementation and starts the server
private void startServer() throws RemoteCommunicationsException {
/**
* This is the service implementation.
* It replies a random Person on the search service
* and starts a thread that replies 20 times asynchronously every 5 seconds on the notify service
*/
PersonsServerService serviceImplementation = new PersonsServerService() {
@Override
public SearchPersonResponse search(SearchPersonRequest request) {
io.github.ppissias.xsrpcj.example.simple.SearchMessages.SearchPersonResponse.Builder responseBuilder = SearchPersonResponse.newBuilder();
io.github.ppissias.xsrpcj.example.simple.SearchMessages.SearchPersonResponse.Person.Builder
personBuilder = SearchPersonResponse.Person.newBuilder();
buildRandomPerson(request.getName(), personBuilder);
try {
Thread.sleep(random.nextInt(2000));
} catch (InterruptedException e1) {
}
responseBuilder.setResponse(personBuilder.build());
return responseBuilder.build();
}
@Override
public PersonNotificationResponse notify(
PersonNotificationRequest request,
PersonsNotifyServerCallback callback) {
new ServerCallbackReplySender(request.getName(), 20, callback).start();
return PersonNotificationResponse.newBuilder().setResponse(responseEnum.ACCEPTED).build();
}
};
//start on default port
new PersonsServer(serviceImplementation).start();
}
//builds random person, based on a first name
private void buildRandomPerson(String fname, io.github.ppissias.xsrpcj.example.simple.SearchMessages.SearchPersonResponse.Person.Builder personBuilder) {
String[] lastNames = {"Smith","Jones","Parsons","Jackson","Anderson","Stamatiou","Lalakis","Tailor","Pirina","Glassy"};
personBuilder.setFirstName(fname);
personBuilder.setLastName(lastNames[random.nextInt(10)]);
String telephone = "0049-"+random.nextInt(10)+random.nextInt(10)+random.nextInt(10)+random.nextInt(10)+
random.nextInt(10)+random.nextInt(10)+random.nextInt(10)+"-"+random.nextInt(10)+
random.nextInt(10)+random.nextInt(10);
personBuilder.setTelephone(telephone);
}
}