package org.simpleim.server.server;

import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.simpleim.common.message.*;
import org.simpleim.server.database.DataBase;
import org.simpleim.server.util.AccountGenerator;

import com.lambdaworks.crypto.SCryptUtil;

public class ServerHandler extends ChannelHandlerAdapter {

    private static final Logger logger = Logger.getLogger(ServerHandler.class.getName());
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            boolean closeNow = true;
            Response response = null;
            String id;
            String password;
            if(msg instanceof NewAccountRequest) {
                    closeNow = true;
                    id=AccountGenerator.nextId();
                    password=AccountGenerator.generatePassword();
                    response = new NewAccountOkResponse()
                                            .setId(id)
                                            .setPassword(password);
                    String hashedPassword = SCryptUtil.scrypt(password, 1 << 15, 8, 1);
                    DataBase.InsertNumberRow(id, hashedPassword);
            } else {
                    closeNow = true;
                    response = new FailureResponse();
            }
            ChannelFuture f = ctx.write(response);
            if(closeNow)
                    f.addListener(ChannelFutureListener.CLOSE);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
            ctx.flush();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
            // Close the connection when an exception is raised.
            logger.log(Level.WARNING, "Unexpected exception from downstream.", cause);
            ctx.close();
    }
}
@@ -316,9 +316,179 @@
 age.
-*
+FailureResponse;%0Aimport org.simpleim.common.message.NewAccountOkResponse;%0Aimport org.simpleim.common.message.NewAccountRequest;%0Aimport org.simpleim.common.message.Response
 ;%0Aim
@@ -685,20 +685,17 @@
 pter %7B%0A%0A
-    
+%09
 private 
@@ -772,20 +772,17 @@
 ame());%0A
-    
+%09
 @Overrid
@@ -775,36 +775,33 @@
 ());%0A%09@Override%0A
-    
+%09
 public void chan
@@ -866,28 +866,18 @@
 ption %7B%0A
-            
+%09%09
 boolean 
@@ -893,28 +893,18 @@
 = true;%0A
-            
+%09%09
 Response
@@ -925,43 +925,23 @@
 ll;%0A
-            String id;%0A            
+%09%09String id;%0A%09%09
 Stri
@@ -953,28 +953,18 @@
 ssword;%0A
-            
+%09%09
 if(msg i
@@ -998,65 +998,31 @@
 ) %7B%0A
-                    closeNow = true;%0A                    
+%09%09%09closeNow = true;%0A%09%09%09
 id=A
@@ -1047,36 +1047,19 @@
 xtId();%0A
-                    
+%09%09%09
 password
@@ -1096,36 +1096,19 @@
 word();%0A
-                    
+%09%09%09
 response
@@ -1141,107 +1141,31 @@
 e()%0A
-                                            .setId(id)%0A                                            
+%09%09%09%09%09%09.setId(id)%0A%09%09%09%09%09%09
 .set
@@ -1184,36 +1184,19 @@
 sword);%0A
-                    
+%09%09%09
 String h
@@ -1255,36 +1255,19 @@
  8, 1);%0A
-                    
+%09%09%09
 DataBase
@@ -1308,86 +1308,42 @@
 d);%0A
-            %7D else %7B%0A                    closeNow = true;%0A                    
+%09%09%7D else %7B%0A%09%09%09closeNow = true;%0A%09%09%09
 resp
@@ -1376,34 +1376,14 @@
 ();%0A
-            %7D%0A            
+%09%09%7D%0A%09%09
 Chan
@@ -1417,28 +1417,18 @@
 ponse);%0A
-            
+%09%09
 if(close
@@ -1436,28 +1436,11 @@
 ow)%0A
-                    
+%09%09%09
 f.ad
@@ -1479,27 +1479,21 @@
 CLOSE);%0A
-    %7D%0A%0A    
+%09%7D%0A%0A%09
 @Overrid
@@ -1486,36 +1486,33 @@
 %0A%09%7D%0A%0A%09@Override%0A
-    
+%09
 public void chan
@@ -1573,28 +1573,18 @@
 ption %7B%0A
-            
+%09%09
 ctx.flus
@@ -1592,19 +1592,13 @@
 ();%0A
-    %7D%0A%0A    
+%09%7D%0A%0A%09
 @Ove
@@ -1603,20 +1603,17 @@
 verride%0A
-    
+%09
 public v
@@ -1678,28 +1678,18 @@
 ause) %7B%0A
-            
+%09%09
 // Close
@@ -1733,28 +1733,18 @@
 raised.%0A
-            
+%09%09
 logger.l
@@ -1814,20 +1814,10 @@
 e);%0A
-            
+%09%09
 ctx.
@@ -1829,11 +1829,9 @@
 ();%0A
-    
+%09
 %7D%0A%7D
+%0A

Revert "Signed-off-by: rogetfan <huxuehan1991@gmail.com>"