Added method to handle incoming objects
(not finished as the methods to further process these "Events" have not been implemented yet)
This commit is contained in:
		@@ -1,13 +1,19 @@
 | 
				
			|||||||
package envoy.server;
 | 
					package envoy.server;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.io.ByteArrayInputStream;
 | 
					import java.io.ByteArrayInputStream;
 | 
				
			||||||
 | 
					import java.io.ByteArrayOutputStream;
 | 
				
			||||||
import java.io.IOException;
 | 
					import java.io.IOException;
 | 
				
			||||||
import java.io.ObjectInputStream;
 | 
					import java.io.ObjectInputStream;
 | 
				
			||||||
 | 
					import java.io.ObjectOutputStream;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import com.jenkov.nioserver.IMessageProcessor;
 | 
					import com.jenkov.nioserver.IMessageProcessor;
 | 
				
			||||||
import com.jenkov.nioserver.Message;
 | 
					import com.jenkov.nioserver.Message;
 | 
				
			||||||
import com.jenkov.nioserver.WriteProxy;
 | 
					import com.jenkov.nioserver.WriteProxy;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import envoy.data.LoginCredentials;
 | 
				
			||||||
 | 
					import envoy.data.User;
 | 
				
			||||||
 | 
					import envoy.event.Event;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Project: <strong>envoy-server-standalone</strong><br>
 | 
					 * Project: <strong>envoy-server-standalone</strong><br>
 | 
				
			||||||
 * File: <strong>ObjectMessageProcessor.java</strong><br>
 | 
					 * File: <strong>ObjectMessageProcessor.java</strong><br>
 | 
				
			||||||
@@ -18,14 +24,63 @@ import com.jenkov.nioserver.WriteProxy;
 | 
				
			|||||||
 */
 | 
					 */
 | 
				
			||||||
public class ObjectMessageProcessor implements IMessageProcessor {
 | 
					public class ObjectMessageProcessor implements IMessageProcessor {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						private long currentUserID = 0;// temporary
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	@Override
 | 
						@Override
 | 
				
			||||||
	public void process(Message message, WriteProxy writeProxy) {
 | 
						public void process(Message message, WriteProxy writeProxy) {
 | 
				
			||||||
		try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(message.sharedArray, message.offset + 4, message.length - 4))) {
 | 
							try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(message.sharedArray, message.offset + 4, message.length - 4))) {
 | 
				
			||||||
			Object obj = in.readObject();
 | 
								Object obj = in.readObject();
 | 
				
			||||||
			// TODO: Process pipeline
 | 
					
 | 
				
			||||||
			System.out.println("Read object: " + obj.toString());
 | 
								System.out.println("Read object: " + obj.toString());
 | 
				
			||||||
 | 
								handleObject(message, writeProxy, obj);
 | 
				
			||||||
		} catch (IOException | ClassNotFoundException e) {
 | 
							} catch (IOException | ClassNotFoundException e) {
 | 
				
			||||||
			e.printStackTrace();
 | 
								e.printStackTrace();
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * This method splits incoming objects into the different objects that are
 | 
				
			||||||
 | 
						 * relevant to the server and guides them to their predestined spot.
 | 
				
			||||||
 | 
						 * 
 | 
				
			||||||
 | 
						 * @param request    the {@link Message} in which the objects are saved
 | 
				
			||||||
 | 
						 * @param writeProxy the writeProxy to define the resulting socket for
 | 
				
			||||||
 | 
						 * @param obj        the object that has been read out of the {@link Message}
 | 
				
			||||||
 | 
						 * @throws IllegalArgumentException if the object given is neither an
 | 
				
			||||||
 | 
						 *                                  {@link envoy.data.Message}, an {@link Event}
 | 
				
			||||||
 | 
						 *                                  nor a {@link LoginCredentials}
 | 
				
			||||||
 | 
						 * @since Envoy Server Standalone v0.1-alpha
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						private void handleObject(Message request, WriteProxy writeProxy, Object obj) throws IllegalArgumentException {
 | 
				
			||||||
 | 
							boolean	responseToSameSocket	= false;
 | 
				
			||||||
 | 
							Object	usage;
 | 
				
			||||||
 | 
							if (obj instanceof envoy.data.Message) {// if object is Message
 | 
				
			||||||
 | 
								envoy.data.Message cast = (envoy.data.Message) obj;
 | 
				
			||||||
 | 
								usage = cast.getClass();
 | 
				
			||||||
 | 
							} else if (obj instanceof Event) {// if object is Event
 | 
				
			||||||
 | 
								usage = (Event<?>) obj;
 | 
				
			||||||
 | 
							} else if (obj instanceof LoginCredentials) {// if object is LoginCredential
 | 
				
			||||||
 | 
								responseToSameSocket = true;
 | 
				
			||||||
 | 
								LoginCredentials cast = (LoginCredentials) obj;
 | 
				
			||||||
 | 
								usage = new User(currentUserID++, cast.getName());
 | 
				
			||||||
 | 
							} else throw new IllegalArgumentException();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							Message response = writeProxy.getMessage();
 | 
				
			||||||
 | 
							if (responseToSameSocket) {
 | 
				
			||||||
 | 
								response.socketId = request.socketId;
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								response.socketId = -0;// TODO temporary.Needs to be replaced
 | 
				
			||||||
 | 
								return;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							// Serialize object to byte array
 | 
				
			||||||
 | 
							ByteArrayOutputStream baos = new ByteArrayOutputStream();
 | 
				
			||||||
 | 
							try (ObjectOutputStream oout = new ObjectOutputStream(baos)) {
 | 
				
			||||||
 | 
								oout.writeObject(usage);
 | 
				
			||||||
 | 
							} catch (IOException e) {
 | 
				
			||||||
 | 
								e.printStackTrace();
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							byte[] objBytes = baos.toByteArray();
 | 
				
			||||||
 | 
							response.writeToMessage(objBytes);
 | 
				
			||||||
 | 
							writeProxy.enqueue(response);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user