Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import org.jboss.netty.buffer.ChannelBuffer;
- import org.jboss.netty.buffer.ChannelBuffers;
- import org.jboss.netty.channel.*;
- class JServerHandler extends SimpleChannelUpstreamHandler {
- @Override
- public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent event) {
- }
- @Override
- public void messageReceived(ChannelHandlerContext ctx, MessageEvent event) {
- // New Client Connected
- if(ctx.getAttachment() == null|| (ctx.getAttachment() instanceof User) != true) {
- ChannelBuffer buffer = (ChannelBuffer) event.getMessage();
- String[] loginInfo = getLogin(buffer);
- User u = getAuthentication(loginInfo);
- if(u == null) {
- // Was Not Found in Database
- ChannelBuffer denial = ChannelBuffers.buffer(8);
- byte[] denied = "false".getBytes();
- denial.writeBytes(denied);
- event.getChannel().write(denial);
- } else {
- // Was Found in Database
- ctx.setAttachment(u);
- byte[] username = u.getUsername().getBytes();
- byte[] password = u.getPassword().getBytes();
- int flag = u.getFlag();
- ChannelBuffer acceptance = ChannelBuffers.buffer(8);
- acceptance.writeBytes(username);
- acceptance.writeBytes(password);
- acceptance.writeInt(flag);
- event.getChannel().write(acceptance);
- }
- } else {
- String command = parseCommand((ChannelBuffer)event.getMessage());
- }
- }
- /**
- * Will return an Array with Login Information, extracted from a {@code ChannelBuffer}.
- * @param buffer The Buffer to read for information.
- * @return An Array with Username (index 0) and Password (index 1).
- */
- public String[] getLogin(ChannelBuffer buffer) {
- String[] loginInfo = new String[2];
- byte[] bytes = new byte[buffer.readableBytes()];
- for(int i = 0; i < buffer.readableBytes(); i++) {
- bytes[i] = buffer.readByte();
- }
- String s = new String(bytes);
- StringBuilder username = new StringBuilder();
- int specialchar = 0;
- for(int i = 0; i < s.length(); i++) {
- if(s.charAt(i) != '@') {
- username.append(s.charAt(i));
- } else {
- specialchar = i;
- break;
- }
- }
- String password = s.substring(specialchar, s.length());
- loginInfo[0] = username.toString();
- loginInfo[1] = password;
- return loginInfo;
- }
- /**
- * Will create and return a User Object if the information matches an entry
- * in the database and null if nothing matches.
- * @param loginInfo The array with login information. First index MUST ALWAYS
- * be the username, and the second index MUST ALWAYS be the password.
- * @return Will return a {@code User} Object if successful or null if not.
- */
- public User getAuthentication(String[] loginInfo) {
- if(loginInfo != null) {
- return new User(loginInfo[0],loginInfo[1],0);
- }
- return null;
- }
- /**
- * Will Parse out the command gotten from a {@code ChannelBuffer}.
- * @param buffer The Buffer to read for information.
- * @return Will return a string with the command.
- */
- public String parseCommand(ChannelBuffer buffer) {
- byte[] bytes = new byte[buffer.readableBytes()];
- for(int i = 0; i < buffer.readableBytes(); i++) {
- bytes[i] = buffer.readByte();
- }
- String s = new String(bytes);
- return s;
- }
- /**
- * Will fetch Map Cycle files from the specified Server.
- * @param u User who requests Map Cycle Files.
- * @param server Which Server to Fetch from.
- * @return Returns an Array with one or two Map Cycle Files.
- */
- public File[] fetchMapCycleFiles(User u, String server) {
- /*
- * Check through the database for files, check permissions etc.
- * If the user does not have the permission to fetch the map cycle
- * files, null should be returned.
- */
- File[] files = new File[2];
- return null;
- }
- }
Add Comment
Please, Sign In to add comment