Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Conversation.java #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 30 additions & 57 deletions RPIAN Vartalap Server/src/rpian/vartalap/server/Conversation.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,24 @@
import java.io.PrintWriter;
import java.net.Socket;

public class Conversation extends Thread {
public class Conversation extends Thread
{

Socket soc;
static int LOGIN_RETRY_COUNTER = 3; // TODO: move this to configuration file or DB

public Conversation(Socket soc) {
public Conversation(Socket soc)
{
this.soc = soc;
}

private boolean authenticateUserPassword(String strArgument) {

Thread t = Thread.currentThread();

boolean flagAuth = false;

for (String a : RPIANVartalapServer.userArrayList) {
System.out.println(t.getName() + " authenticateUserPassword(): Loop: String a :" + a);
// TODO: Move authentication from array to DB
if (strArgument.equals(a)) {
flagAuth = true;
break;
}
} // User authentication complete..

return flagAuth;
}


@Override
public void run() {
Thread t = Thread.currentThread();
System.out.println(t.getName() + " Signing On");
public void run()
{
System.out.println(this.getName() + " Signing On");

try {
try
{
BufferedReader nis = new BufferedReader(
new InputStreamReader(
soc.getInputStream()
Expand All @@ -53,45 +38,33 @@ public void run() {
), true
);
RPIANVartalapServer.nosArrayList.add(nos);
boolean flagAuth = false;
int retryCounter = 0;
String strInput = null;

while (retryCounter < LOGIN_RETRY_COUNTER) {
strInput = nis.readLine();
flagAuth = authenticateUserPassword(strInput);
if (flagAuth == true) {
break;
} else {
nos.println("AuthFailed-Retry");
}
retryCounter++; // increment retry counter on user authentication failure
}// while login retry block ends

if (flagAuth == true) {
// perform chat conversation with Authorized user
nos.println("Authenticated");
// perform chat conversation with Authorized user
nos.println("Authenticated");
String strInput = nis.readLine();
while (!strInput.equals("End"))
{
System.out.println("Server recieved = "+strInput);
RPIANVartalapServer.q.enqueue(strInput);
strInput = nis.readLine();
while (!strInput.equals("End")) {
RPIANVartalapServer.q.enqueue(strInput);
strInput = nis.readLine();
} // until the user closes the conversation with an "End"
} else {
nos.println("Failed to Authenticate");
strInput = "End"; // Forcefully terminating the chat with client connection
}// flag false ends here
} // until the user closes the conversation with an "End"

if ("End".equals(strInput)) {
if ("End".equals(strInput))
{
nos.println(strInput);
// if u dont send "End" to respective client, its chat window wont terminate
RPIANVartalapServer.nosArrayList.remove(nos);
System.out.println(t.getName() + " Chat client terminated");
System.out.println(this.getName() + " Chat client terminated");
}
} catch (Exception e) {
System.out.println(t.getName() + " Exception occured\nClient Seems to have abruptly closed the connection\n" + e);
} finally {
}
catch (Exception e)
{
System.out.println(this.getName() + " Exception occured\nClient Seems to have abruptly closed the connection\n" + e);
}
finally
{
// TODO : release resources and connections
System.out.println(t.getName() + " Signing Off");
System.out.println(this.getName() + " Signing Off");
}
}// run ends
}
}