Package voxo.server.actions

Source Code of voxo.server.actions.UserLoginAction

package voxo.server.actions;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.SQLException;
import java.util.ArrayList;

import voxo.common.entities.Packet;
import voxo.common.entities.User;
import voxo.common.enums.EnumPacket;
import voxo.common.packets.LoginPacket;
import voxo.server.Messages;
import voxo.server.exceptions.RequestException;
import voxo.server.managers.UserManager;
import voxo.server.services.NetworkService;

public class UserLoginAction {
  public UserLoginAction(InetAddress ip, LoginPacket lp) throws SQLException, UnknownHostException, IOException, RequestException {
    // Check Login
    ArrayList<User> alu = UserManager.searchUserAbsolute(new User(null, lp.getUsername(), lp.getPwd(), null, null, null, null));
    if (alu.size() != 1) {
      NetworkService.sendPacket(ip, new Packet(EnumPacket.SERVER_Error, Messages.getString("UserLoginAction.UsernamePasswordInvalid"))); // Send notice //$NON-NLS-1$
      throw new RequestException(Messages.getString("UserLoginAction.ERR_WrongUsername")); //$NON-NLS-1$
    }

    // Get the user
    User u = alu.get(0);

    // Check if already logged
    if (u.getOnline()) {
      NetworkService.sendPacket(u.getIp(), new Packet(EnumPacket.SERVER_Error, Messages.getString("UserLoginAction.ERR_DualLoginKick"))); //$NON-NLS-1$
      new KickUserAction(u);
      throw new RequestException(Messages.getString("UserLoginAction.ERR_DualLogin"));
    }
   
    // Set online
    u.setOnline(true);
    u.setIp(ip.getHostAddress());
    UserManager.updateUserById(u);


    // Send updates to the client
    NetworkService.sendPacket(ip, new Packet(EnumPacket.SERVER_LoginAccepted, u)); // Send
                                            // responce
    new SendContactListAction(u); // Send contacts
    NetworkService.sendPacket(ip, new Packet(EnumPacket.SERVER_Notice, Messages.getString("UserLoginAction.WelcomeMsg"))); // Send notice //$NON-NLS-1$

    // Update contact list to his online members
    ArrayList<User> alcu = UserManager.getUserContactsByUser(u);
    for (User iU : alcu) {
      if (iU.getOnline() && (iU.getIp() != null)) {
        new SendContactListAction(iU);
        NetworkService.sendPacket(iU.getIp(), new Packet(EnumPacket.SERVER_Notice, String.format(Messages.getString("UserLoginAction.UserConnect"), u.getUsername()))); //$NON-NLS-1$
      }
    }
  }
}
TOP

Related Classes of voxo.server.actions.UserLoginAction

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.