@@ -7,6 +7,7 @@ Beam is client/server and peer-to-peer compatible networking library. It provide
77* Connecting a Client
88* Creating Messages
99* Sending Messages
10+ * Exchanging Messages
1011* Queuing Messages
1112* Broadcasting Messages
1213* Secure Communication
@@ -33,12 +34,12 @@ public class ExampleHandler extends BasicHandler
3334{
3435
3536 public ExampleHandler () {
36- super (ExampleMessageType . EXAMPLE_MESSAGE );
37+ super (ExampleMessage . EXAMPLE_MESSAGE_ID );
3738 }
3839
3940 @Override
4041 public BeamMessage messageRecieved (Communicator comm , BasicMessage msg ) {
41- System . out. println (" Client sent msg : " + msg. getString (" client_message" ));
42+ System . out. println (" Client sent message : " + msg. getString (" client_message" ));
4243
4344 // response message
4445 return msg. emptyResponse (). setString (" server_response" , " example_reply_message" );
@@ -72,9 +73,10 @@ client.connect ();
7273``` java
7374public class ExampleMessage extends BasicMessage
7475{
75-
76+ public final static int EXAMPLE_MESSAGE_ID = 1000 ;
77+
7678 public ExampleMessage () {
77- super (ExampleMessageType . EXAMPLE_MESSAGE );
79+ super (EXAMPLE_MESSAGE_ID );
7880 }
7981
8082 public ExampleMessage (BeamMessage message ) {
@@ -102,6 +104,21 @@ exampleMessage = new ExampleMessage (responseMessage);
102104System . out. println (" Server response: " + exampleMessage. getString (" server_response" ));
103105```
104106
107+ ## Exchanging Messages ##
108+
109+ Exchanging a message will send a message and update the original message with the response message given. Useful when the request and response message are the same class as it avoids unnessecary casting like when sending messages.
110+
111+ To create and exchange a BasicMessage to the server from client
112+ ``` java
113+ ExampleMessage exampleMessage = new ExampleMessage ();
114+ exampleMessage. setString (" client_message" , " example_client_message" );
115+ if (client. exchangeMessage (exampleMessage)) {
116+ // output server response
117+ System . out. println (exampleMessage. getString (" server_response" ));
118+ } else {
119+ System . out. println (" Request timed out!" );
120+ }
121+ ```
105122
106123## Queuing Messages ##
107124
@@ -133,7 +150,7 @@ server.broadcast (exampleMessage);
133150Client
134151``` java
135152AES aes = new AES (" password" );
136- BeamMessage aesMessage = new AESBeamMessage (aes, ENCRYPTED_MESSAGE );
153+ BeamMessage aesMessage = new AESBeamMessage (aes, ENCRYPTED_MESSAGE_ID );
137154aesMessage. set (" secret_variable" , " secret_value" );
138155
139156// send and receive response (response is returned decrypted)
@@ -147,14 +164,14 @@ public class ExampleAESHandler extends AESBeamHandler
147164{
148165
149166 public ExampleAESHandler (AES aes ) {
150- super (aes, ENCRYPTED_MESSAGE );
167+ super (aes, ENCRYPTED_MESSAGE_ID );
151168 }
152169
153170 @Override
154171 public BeamMessage messageRecieved (Communicator comm , BeamMessage message ) {
155172 System . out. println (" Client sent secret: " + message. get (" secret_variable" ));
156173
157- BasicMessage responseMessage = new BasicMessage (ENCRYPTED_MESSAGE );
174+ BasicMessage responseMessage = new BasicMessage (ENCRYPTED_MESSAGE_ID );
158175 responseMessage. setString (" server_response" , " server_secret_value" );
159176 return responseMessage;
160177 }
@@ -169,7 +186,7 @@ Client
169186```java
170187RSA rsa = new RSA (1024 );
171188RSAConnection rsaConn = client. establishRSAConnection (rsa);
172- BeamMessage rsaMessage = new RSABeamMessage (rsaConn, ENCRYPTED_MESSAGE );
189+ BeamMessage rsaMessage = new RSABeamMessage (rsaConn, ENCRYPTED_MESSAGE_ID );
173190rsaMessage. set (" secret_variable" , " secret_value" );
174191
175192// send and receive response (response is returned decrypted)
@@ -183,14 +200,14 @@ public class ExampleRSAHandler extends RSABeamHandler
183200{
184201
185202 public ExampleRSAHandler (RSA rsa ) {
186- super (ENCRYPTED_MESSAGE );
203+ super (ENCRYPTED_MESSAGE_ID );
187204 }
188205
189206 @Override
190207 public BeamMessage messageRecieved (Communicator comm , BeamMessage message ) {
191208 System . out. println (" Client sent secret: " + message. get (" secret_variable" ));
192209
193- BasicMessage responseMessage = new BasicMessage (ENCRYPTED_MESSAGE );
210+ BasicMessage responseMessage = new BasicMessage (ENCRYPTED_MESSAGE_ID );
194211 responseMessage. setString (" server_response" , " server_secret_value" );
195212 return responseMessage;
196213 }
0 commit comments