Guest User

Untitled

a guest
Sep 4th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.40 KB | None | 0 0
  1. import java.io.File;
  2. import org.jboss.netty.buffer.ChannelBuffer;
  3. import org.jboss.netty.buffer.ChannelBuffers;
  4. import org.jboss.netty.channel.*;
  5.  
  6. class JServerHandler extends SimpleChannelUpstreamHandler {
  7.    
  8.     @Override
  9.     public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent event) {
  10.        
  11.     }
  12.    
  13.     @Override
  14.     public void messageReceived(ChannelHandlerContext ctx, MessageEvent event) {
  15.         // New Client Connected
  16.         if(ctx.getAttachment() == null|| (ctx.getAttachment() instanceof User) != true) {
  17.             ChannelBuffer buffer = (ChannelBuffer) event.getMessage();
  18.             String[] loginInfo = getLogin(buffer);
  19.             User u = getAuthentication(loginInfo);
  20.             if(u == null) {
  21.                 // Was Not Found in Database
  22.                 ChannelBuffer denial = ChannelBuffers.buffer(8);
  23.                 byte[] denied = "false".getBytes();
  24.                 denial.writeBytes(denied);
  25.                 event.getChannel().write(denial);
  26.             } else {
  27.                 // Was Found in Database
  28.                 ctx.setAttachment(u);
  29.                 byte[] username = u.getUsername().getBytes();
  30.                 byte[] password = u.getPassword().getBytes();
  31.                 int flag = u.getFlag();
  32.                 ChannelBuffer acceptance = ChannelBuffers.buffer(8);
  33.                 acceptance.writeBytes(username);
  34.                 acceptance.writeBytes(password);
  35.                 acceptance.writeInt(flag);
  36.                 event.getChannel().write(acceptance);
  37.             }
  38.         } else {
  39.             String command = parseCommand((ChannelBuffer)event.getMessage());
  40.            
  41.         }
  42.     }
  43.    
  44.    
  45.    
  46.     /**
  47.      * Will return an Array with Login Information, extracted from a {@code ChannelBuffer}.
  48.      * @param buffer The Buffer to read for information.
  49.      * @return An Array with Username (index 0) and Password (index 1).
  50.      */
  51.     public String[] getLogin(ChannelBuffer buffer) {
  52.         String[] loginInfo = new String[2];
  53.         byte[] bytes = new byte[buffer.readableBytes()];
  54.         for(int i = 0; i < buffer.readableBytes(); i++) {
  55.             bytes[i] = buffer.readByte();
  56.         }
  57.         String s = new String(bytes);
  58.         StringBuilder username = new StringBuilder();
  59.         int specialchar = 0;
  60.         for(int i = 0; i < s.length(); i++) {
  61.             if(s.charAt(i) != '@') {
  62.                 username.append(s.charAt(i));
  63.             } else {
  64.                 specialchar = i;
  65.                 break;
  66.             }
  67.         }
  68.         String password = s.substring(specialchar, s.length());
  69.         loginInfo[0] = username.toString();
  70.         loginInfo[1] = password;
  71.         return loginInfo;
  72.     }    
  73.    
  74.     /**
  75.      * Will create and return a User Object if the information matches an entry
  76.      * in the database and null if nothing matches.
  77.      * @param loginInfo The array with login information. First index MUST ALWAYS
  78.      * be the username, and the second index MUST ALWAYS be the password.
  79.      * @return Will return a {@code User} Object if successful or null if not.
  80.      */
  81.     public User getAuthentication(String[] loginInfo) {
  82.         if(loginInfo != null) {
  83.             return new User(loginInfo[0],loginInfo[1],0);
  84.         }
  85.        
  86.         return null;
  87.     }
  88.    
  89.     /**
  90.      * Will Parse out the command gotten from a {@code ChannelBuffer}.
  91.      * @param buffer The Buffer to read for information.
  92.      * @return Will return a string with the command.
  93.      */
  94.     public String parseCommand(ChannelBuffer buffer) {
  95.         byte[] bytes = new byte[buffer.readableBytes()];
  96.         for(int i = 0; i < buffer.readableBytes(); i++) {
  97.             bytes[i] = buffer.readByte();
  98.         }
  99.         String s = new String(bytes);
  100.        
  101.         return s;
  102.     }
  103.    
  104.     /**
  105.      * Will fetch Map Cycle files from the specified Server.
  106.      * @param u User who requests Map Cycle Files.
  107.      * @param server Which Server to Fetch from.
  108.      * @return Returns an Array with one or two Map Cycle Files.
  109.      */
  110.     public File[] fetchMapCycleFiles(User u, String server) {
  111.         /*
  112.          * Check through the database for files, check permissions etc.
  113.          * If the user does not have the permission to fetch the map cycle
  114.          * files, null should be returned.
  115.          */
  116.         File[] files = new File[2];
  117.        
  118.         return null;
  119.     }
  120. }
Add Comment
Please, Sign In to add comment