Mon May 26, 2008 1:38 pm
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.Iterator;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
/**
*
* @author SAMI
*/
public class ServerConnector extends Thread {
private String Name;
private String password;
private int AdminPort = 9999;
private int UserPort = 9991;
private ByteBuffer ReadBuffer;
private ByteBuffer writeBuffer;
private SocketChannel SChan;
private Selector ReadSelector;
private CharsetDecoder asciiDecoder;
private JFrame loginFrame;
public void setLoginFrame(JFrame loginFrame) {
this.loginFrame = loginFrame;
}
// private String addr = "127.0.0.1";
private String hostname = "ahmed";
public ServerConnector(String Name, String password) {
this.Name = Name;
this.password = password;
ReadBuffer = ByteBuffer.allocateDirect(300);
writeBuffer = ByteBuffer.allocateDirect(300);
asciiDecoder = Charset.forName("US-ASCII").newDecoder();
}
public void ConnectAsAdmin() {
Connect(AdminPort,"a");
}
public void ConnectAsUser() {
Connect(UserPort,"u");
// System.out.println("N=" + Name + "&P=" + password+"&t="+type);
}
public void Connect(int PORT,String type) {
try {
ReadSelector = Selector.open();
InetAddress addr = InetAddress.getByName(hostname);
SChan = SocketChannel.open(new InetSocketAddress(addr, PORT));
SChan.configureBlocking(false);
SChan.register(ReadSelector, SelectionKey.OP_READ, new StringBuffer());
SendMassage("N=" + Name + "&P=" + password+"&T="+type);
} catch (Exception e) {
e.printStackTrace();
}
}
public void SendMassage(String messg) {
prepareBuffer(messg);
channelWrite(SChan);
System.out.println("message sent");
}
public void prepareBuffer(String massg) {
System.out.println("Massage=" + massg);
writeBuffer.clear();
writeBuffer.put(massg.getBytes());
writeBuffer.putChar('\n');
writeBuffer.flip();
}
public void channelWrite(SocketChannel client) {
long num = 0;
long len = writeBuffer.remaining();
while (num != len) {
try {
num += SChan.write(writeBuffer);
Thread.sleep(5);
} catch (IOException ex) {
ex.printStackTrace();
} catch (InterruptedException ex) {
}
}
writeBuffer.rewind();
}
public String ReadMassage() {
String Massage = null;
try {
ReadSelector.selectNow();
Set readkeys = ReadSelector.selectedKeys();
Iterator iter = readkeys.iterator();
while (iter.hasNext()) {
SelectionKey key = (SelectionKey) iter.next();
iter.remove();
SocketChannel client = (SocketChannel) key.channel();
ReadBuffer.clear();
long num = client.read(ReadBuffer);
if (num == -1) {
return null;
} else {
StringBuffer str = (StringBuffer) key.attachment();
ReadBuffer.flip();
String data = asciiDecoder.decode(ReadBuffer).toString();
ReadBuffer.clear();
Massage = data;
System.out.println("Massage= " + Massage);
str.append(data);
return Massage;
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
return Massage;
}
public void sendlogout()
{
SendMassage("logout&"+Name+"&"+password);
if(loginFrame==null)
System.out.println("The Frame is null");
else
loginFrame.setVisible(true);
}
public void Amout()
{
loginFrame.setVisible(true);
}
public void sendLogin(String type){
SendMassage("login:&"+Name+"&"+password+"&"+type);
}
public boolean changePassword(String oldPassword,String newPassword)
{
if(oldPassword.equals(password))
{
SendMassage("changePassword&"+Name+"&"+password+"&"+newPassword);
return true;
}
return false;
}
public void run() {
}
String getUserName()
{
return Name;
}
}
import java.io.BufferedReader;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author SAMI
*/
public class LoginIO {
private final String delim = "&";
private final String lineSeparator = (String) java.security.AccessController.doPrivileged(
new sun.security.action.GetPropertyAction("line.separator"));
BufferedReader buffRead;
BufferedWriter buffWrite;
String name;
String password;
File Accounts;
File Records;
FileWriter fr;
private String type;
public LoginIO() {
Accounts = new File("Account.txt");
Records = new File("records.txt");
// buffRead = new BufferedReader(new FileReader(Accounts));
//buffWrite = new BufferedWriter(new FileWriter(f,true));
// buffWrite = new BufferedWriter(new FileWriter(f));
// buffWrite=new BufferedWriter(new FileWriter(file));
}
public String checkUser(String Massage) {
ExtractNP(Massage);
String returns="This ID is not yet taken.";
try {
String Line = " ";
buffRead = new BufferedReader(new FileReader(Accounts));
while ((Line = buffRead.readLine()) != null) {
String[] data = Line.split("&");
System.out.println("len = " + data.length);
System.out.println("line " + Line);
System.out.println("Am here:" + data[1] + "-" + data[2]);
System.out.println("Am here:" + name + "-" + password);
if ((data[0].equals(name)) && (data[1].equals(password) && data[3].equalsIgnoreCase(type))) {
if(data[2].equalsIgnoreCase("c"))
{
returns= "User Aleardy connected esure that you disconnect or contact with admin";
System.out.println("returns "+returns);
break;
}
connectUser(name);
System.out.println("Is in file");
returns = "welcome "+name+" !";
}
// buffWrite.newLine();
}
buffRead.close();
} catch (IOException ex) {
Logger.getLogger(LoginIO.class.getName()).log(Level.SEVERE, null, ex);
}
return returns;
}
public String GetRecord(String SSID) {
// ExtractNP(Massage);
String Line = "no such record exists";
try {
buffRead = new BufferedReader(new FileReader("records.txt"));
while ((Line = buffRead.readLine()) != null) {
String[] data = Line.split("&");
if ((data[0].equals(SSID.trim()))) { //buffWrite.write("N="+name+"");
connectUser(name);
System.out.println("Is in file : " + Line);
return Line;
}
// buffWrite.newLine();
}
buffRead.close();
} catch (IOException ex) {
Logger.getLogger(LoginIO.class.getName()).log(Level.SEVERE, null, ex);
}
return "no such record exists";
}
public void ExtractNP(String Massage) {
name = Massage.substring(2, Massage.indexOf("&P"));
password = Massage.substring(Massage.indexOf("&P") + 3, Massage.indexOf("&T"));
type = Massage.substring(Massage.indexOf("&T") + 3, Massage.length() - 2);
System.out.println("Name=" + name + " Password=" + password+ " type=" + type);
}
synchronized boolean AddNewRecord(String SS, String fName, String lName, String phone) {
try {
buffWrite = new BufferedWriter(new FileWriter(Records, true));
String userData = SS + '&' + fName + '&' + lName + '&' + phone;
buffWrite.write(userData);
buffWrite.newLine();
buffWrite.flush();
buffWrite.close();
} catch (IOException ex) {
Logger.getLogger(LoginIO.class.getName()).log(Level.SEVERE, null, ex);
} finally {
}
return true;
}
synchronized String AddNewUser(String name,String password) {
String returnes = "problem cann't add";
try {
// ExtractNP(Message);
if(!canCreateAccount(name))
return name+" already exist";
buffWrite = new BufferedWriter(new FileWriter(Accounts, true));
buffWrite.write(name + '&' + password + "&N&u");
buffWrite.newLine();
//System.out.println(canCreateRecord("SN"));
buffWrite.flush();
buffWrite.close();
returnes="Account added ";
// AddNewRecord(getNewSSNo(), name, name, "1234");
} catch (IOException ex) {
Logger.getLogger(LoginIO.class.getName()).log(Level.SEVERE, null, ex);
} finally {
//makeUserAdmin();
}
return returnes;
}
public boolean canCreateAccount(String SS) {
try {
buffRead.close();
buffRead = new BufferedReader(new FileReader(Accounts));
String Line = " ";
while ((Line = buffRead.readLine()) != null) {
String[] tokens = Line.split("&");
System.out.println("OnlineUsers= " + tokens[0]);
if (tokens[0].equalsIgnoreCase(SS)) {
return false;
}
// buffWrite.newLine();
}
} catch (IOException ex) {
Logger.getLogger(LoginIO.class.getName()).log(Level.SEVERE, null, ex);
}
return true;
}
public boolean canCreateRecord(String SS) {
try {
buffRead.close();
buffRead = new BufferedReader(new FileReader(Records));
String Line = " ";
while ((Line = buffRead.readLine()) != null) {
String[] tokens = Line.split("&");
System.out.println("OnlineUsers= " + tokens[0]);
if (tokens[0].equalsIgnoreCase(SS)) {
return false;
}
// buffWrite.newLine();
}
} catch (IOException ex) {
Logger.getLogger(LoginIO.class.getName()).log(Level.SEVERE, null, ex);
}
return true;
}
public String getAllOnlineUsers() {
String OnlineUsers = "";
try {
buffRead.close();
buffRead = new BufferedReader(new FileReader(Accounts));
String Line = " ";
System.out.println("Just here");
while ((Line = buffRead.readLine()) != null) {
String[] tokens = Line.split("&");
if (tokens == null) {
continue;
}
System.out.println("LEN = " + tokens.length);
if (tokens[2].equalsIgnoreCase("C")) {
OnlineUsers += tokens[0] + "&";
System.out.println("OnlineUsers= " + tokens[0]);
}
// buffWrite.newLine();
}
} catch (IOException ex) {
Logger.getLogger(LoginIO.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("OnlineUsers= " + OnlineUsers);
return OnlineUsers;
}
/*synchronized public void makeUserAdmin(){
try {
buffRead = new BufferedReader(new FileReader(Accounts));
String fileContent="";
String Line = " ";
while ((Line = buffRead.readLine()) != null) {
String[] tokens = Line.split("&");
System.out.println("OnlineUsers= " + tokens[0]);
if (tokens[4].equalsIgnoreCase("u")) {
fileContent+=tokens[0]+delim+tokens[1]+delim+tokens[2]+delim+tokens[3]+delim+"a"+lineSeparator;
}else
fileContent+=Line+lineSeparator;
// buffWrite.newLine();
}
buffRead.close();
buffWrite = new BufferedWriter(new FileWriter(Accounts));
buffWrite.write(fileContent);
buffWrite.newLine();
buffWrite.flush();
buffWrite.close();
} catch (IOException ex) {
Logger.getLogger(LoginIO.class.getName()).log(Level.SEVERE, null, ex);
}
}*/
synchronized String ModifyRecord(String SSL, String Record) {
System.out.println("SSL=" + SSL + " Record=" + Record);
String returns = "Record is not found";
try {
buffRead = new BufferedReader(new FileReader("records.txt"));
String fileContent = "";
String Line = "";
while ((Line = buffRead.readLine()) != null) {
String[] tokens = Line.split("&");
/// System.out.println("OnlineUsers= " + tokens[0]);
if (tokens[0].equalsIgnoreCase(SSL)) {
fileContent += Record + lineSeparator;
returns = "Record is modified";
} else {
fileContent += Line + lineSeparator;
}
// buffWrite.newLine();
}
buffRead.close();
buffWrite = new BufferedWriter(new FileWriter("records.txt"));
buffWrite.write(fileContent.trim());
buffWrite.newLine();
buffWrite.flush();
buffWrite.close();
} catch (IOException ex) {
Logger.getLogger(LoginIO.class.getName()).log(Level.SEVERE, null, ex);
}
return returns;
}
synchronized String DeleteRecord(String SSL) {
System.out.println("SSL=" + SSL);
String returns = "Record is not found";
try {
buffRead = new BufferedReader(new FileReader("records.txt"));
String fileContent = "";
String Line = "";
while ((Line = buffRead.readLine()) != null) {
String[] tokens = Line.split("&");
/// System.out.println("OnlineUsers= " + tokens[0]);
if (tokens[0].equalsIgnoreCase(SSL)) {
// fileContent+=Record+lineSeparator;
returns = "Record is deleted";
} else {
fileContent += Line + lineSeparator;
}
// buffWrite.newLine();
}
buffRead.close();
buffWrite = new BufferedWriter(new FileWriter("records.txt"));
buffWrite.write(fileContent.trim());
buffWrite.newLine();
buffWrite.flush();
buffWrite.close();
} catch (IOException ex) {
Logger.getLogger(LoginIO.class.getName()).log(Level.SEVERE, null, ex);
}
return returns;
}
synchronized public void DisconnectUser(String Username) {
try {
buffRead = new BufferedReader(new FileReader(Accounts));
String fileContent = "";
String Line = "";
while ((Line = buffRead.readLine()) != null) {
String[] tokens = Line.split("&");
/// System.out.println("OnlineUsers= " + tokens[0]);
if (tokens[0].equalsIgnoreCase(Username.trim()) && tokens[2].equalsIgnoreCase("C")) {
fileContent += tokens[0] + delim + tokens[1] + delim + "N" + delim + tokens[3] + lineSeparator;
} else {
fileContent += Line + lineSeparator;
}
// buffWrite.newLine();
}
buffRead.close();
buffWrite = new BufferedWriter(new FileWriter(Accounts));
buffWrite.write(fileContent.trim());
buffWrite.newLine();
buffWrite.flush();
buffWrite.close();
} catch (IOException ex) {
Logger.getLogger(LoginIO.class.getName()).log(Level.SEVERE, null, ex);
}
}
synchronized public void connectUser(String Username) {
System.out.println("Connecting User : " + Username);
try {
buffRead = new BufferedReader(new FileReader(Accounts));
String fileContent = "";
String Line = "";
while ((Line = buffRead.readLine()) != null) {
String[] tokens = Line.split("&");
/// System.out.println("OnlineUsers= " + tokens[0]);
if (tokens[0].equalsIgnoreCase(Username.trim())) {
fileContent += tokens[0] + delim + tokens[1] + delim + "C" + delim + tokens[3] + lineSeparator;
System.out.println(tokens[0] + delim + tokens[1] + delim + "C" + delim + tokens[3] + lineSeparator);
} else {
fileContent += Line + lineSeparator;
}
// buffWrite.newLine();
}
buffRead.close();
buffWrite = new BufferedWriter(new FileWriter(Accounts));
buffWrite.write(fileContent.trim());
buffWrite.newLine();
buffWrite.flush();
buffWrite.close();
} catch (IOException ex) {
Logger.getLogger(LoginIO.class.getName()).log(Level.SEVERE, null, ex);
}
}
public String getNewSSNo() {
return new String("S" + Math.round(Math.random() * 100));
}
synchronized public void changePassword(String name, String oldPass, String newPass) {
try {
buffRead = new BufferedReader(new FileReader(Accounts));
String fileContent = "";
String Line = "";
while ((Line = buffRead.readLine()) != null) {
String[] tokens = Line.split("&");
/// System.out.println("OnlineUsers= " + tokens[0]);
if (tokens[0].equalsIgnoreCase(name.trim()) && tokens[1].equals(oldPass.trim())) {
fileContent += tokens[0] + delim + newPass + delim + tokens[2] + delim + tokens[3] + lineSeparator;
System.out.println("Password "+tokens[0] + delim + tokens[1] + delim + "C" + delim + tokens[3] + lineSeparator);
} else {
fileContent += Line + lineSeparator;
}
// buffWrite.newLine();
}
buffRead.close();
buffWrite = new BufferedWriter(new FileWriter(Accounts));
buffWrite.write(fileContent.trim());
buffWrite.newLine();
buffWrite.flush();
buffWrite.close();
} catch (IOException ex) {
Logger.getLogger(LoginIO.class.getName()).log(Level.SEVERE, null, ex);
}
}
public String GetRecord(String fName,String lName) {
// ExtractNP(Massage);
String Line = "no such record exists";
try {
buffRead = new BufferedReader(new FileReader("records.txt"));
while ((Line = buffRead.readLine()) != null) {
String[] data = Line.split("&");
if ((data[1].equalsIgnoreCase(fName.trim())&&data[2].equalsIgnoreCase(lName.trim()))) { //buffWrite.write("N="+name+"");
// connectUser(name);
System.out.println("Is in file : " + Line);
return Line;
}
// buffWrite.newLine();
}
buffRead.close();
} catch (IOException ex) {
Logger.getLogger(LoginIO.class.getName()).log(Level.SEVERE, null, ex);
}
return "no such record exists";
}
synchronized String DeleteAccount(String fname) {
// System.out.println("SSL=" + SSL);
String returns = "no such account found";
try {
buffRead = new BufferedReader(new FileReader(Accounts));
String fileContent = "";
String Line = "";
while ((Line = buffRead.readLine()) != null) {
String[] tokens = Line.split("&");
/// System.out.println("OnlineUsers= " + tokens[0]);
if (tokens[0].equalsIgnoreCase(fname)) {
// fileContent+=Record+lineSeparator;
returns = fname+"Account is deleted";
} else {
fileContent += Line + lineSeparator;
}
// buffWrite.newLine();
}
buffRead.close();
buffWrite = new BufferedWriter(new FileWriter(Accounts));
buffWrite.write(fileContent.trim());
buffWrite.newLine();
buffWrite.flush();
buffWrite.close();
} catch (IOException ex) {
Logger.getLogger(LoginIO.class.getName()).log(Level.SEVERE, null, ex);
}
return returns;
}
synchronized String Login(String fname,String Password,String type) {
String returns = "This ID is not yet taken.";
try {
// System.out.println("SSL=" + SSL);
buffRead = new BufferedReader(new FileReader(Accounts));
String Line = "";
while ((Line = buffRead.readLine()) != null) {
String[] tokens = Line.split("&");
/// System.out.println("OnlineUsers= " + tokens[0]);
if (tokens[0].equalsIgnoreCase(fname.trim())&&tokens[1].equals(Password.trim())&&
tokens[3].equalsIgnoreCase(type.trim())) {
// fileContent+=Record+lineSeparator;
returns = "Welcome "+fname+" !" ;
System.out.println(returns);
connectUser(fname);
return returns;
}
buffRead.close();
return returns;
}
} catch (IOException ex) {
Logger.getLogger(LoginIO.class.getName()).log(Level.SEVERE, null, ex);
}
return returns;
}
}
Mon Jan 21, 2013 12:57 am
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.