- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnection.java
More file actions
Latest commit
144 lines (116 loc) · 4.61 KB
/
Copy pathConnection.java
File metadata and controls
144 lines (116 loc) · 4.61 KB
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
/**
* Created by sameerraghuram on 4/22/17.
*/
importcom.google.gson.Gson;
importcom.google.gson.GsonBuilder;
importjava.io.IOException;
importjava.util.concurrent.Callable;
importjava.util.function.Consumer;
/**
* TODO: Write a relaible function to send JSON messages
* TODO: Write a Server (Done)
* TODO: Figure out how GSON works
*/
publicclassConnection {
Stringbroker;
ClientListenerclientListener;
Callbackscallbacks;
publicConnection(Stringbroker){
this.broker = broker;
clientListener = newClientListener();
callbacks = Callbacks.getInstance();
Threadlistener = newThread(clientListener);
listener.start();
}
/**
* Sends a request to the Broker to subscribe the client
* to a message queue residing on the default exchange.
*
* @param queueName: ID of the message queue on the broker
* @param callback: Function to be called everytime a new message arrives.
*/
publicvoidsubscribe(StringqueueName, Consumer<String> callback){
// Construct the request here
// Step 1: Make the GSON object
Gsongson = newGsonBuilder().setPrettyPrinting().create();
// Step 2: Create the request JSON string
SubscribeMessagesubscribeMessage = newSubscribeMessage(queueName);
Stringmessage = gson.toJson(subscribeMessage);
// Step 3: Register callback to queue-id
this.callbacks.put(queueName, callback);
// Step 4: Send message
try {
Messengermessenger = newMessenger(this.broker, 12344);
messenger.sendMessage(message);
} catch (IOExceptione) {
e.printStackTrace();
}
}
/**
* Sends a request to the Broker to subscribe the client
* to a message queue residing on the exchange given by exchangeName.
*
* @param exchangeName: Identity of the exchange
* @param queueName: ID of the message queue on the broker
* @param callback: Function to be called everytime a new message arrives.
*/
publicvoidsubscribe(StringexchangeName, StringqueueName, Consumer<String> callback){
// Step 1: Make the GSON object
Gsongson = newGsonBuilder().setPrettyPrinting().create();
// Step 2: Create the request JSON string
SubscribeMessagesubscribeMessage = newSubscribeMessage(exchangeName, queueName);
Stringmessage = gson.toJson(subscribeMessage);
// Step 3: Register callback to queue-id
this.callbacks.put(exchangeName + "." + queueName, callback);
// TODO: Step 4: Send message
try {
Messengermessenger = newMessenger(this.broker, 12344);
messenger.sendMessage(message);
} catch (IOExceptione) {
e.printStackTrace();
}
}
publicvoidcreateQueue(StringexchangeName, StringqueueName){
// Step 1: Make the GSON object
Gsongson = newGsonBuilder().setPrettyPrinting().create();
// Step 2: Create the request JSON string
CreateQueueMessagecreateQueueMessage = newCreateQueueMessage(exchangeName, queueName);
Stringmessage = gson.toJson(createQueueMessage);
// Step 3: Send message
try {
Messengermessenger = newMessenger(this.broker, 12344);
messenger.sendMessage(message);
} catch (IOExceptione) {
e.printStackTrace();
}
}
publicvoidcreateQueue(StringqueueName){
// Step 1: Make the GSON object
Gsongson = newGsonBuilder().setPrettyPrinting().create();
// Step 2: Create the request JSON string
CreateQueueMessagecreateQueueMessage = newCreateQueueMessage(queueName);
Stringmessage = gson.toJson(createQueueMessage);
// Step 3: Send message
try {
Messengermessenger = newMessenger(this.broker, 12344);
messenger.sendMessage(message);
//System.out.println(message);
} catch (Exceptione) {
e.printStackTrace();
}
}
publicvoidpublish(StringqueueName, Stringmessage){
// Step 1: Make the GSON object
Gsongson = newGsonBuilder().setPrettyPrinting().create();
// Step 2: Create the request JSON string
PublishMessagepublishMessage = newPublishMessage(queueName, message);
message = gson.toJson(publishMessage);
// TODO: Step 4: Send message
try {
Messengermessenger = newMessenger(this.broker, 12344);
messenger.sendMessage(message);
} catch (IOExceptione) {
e.printStackTrace();
}
}
}