Skip to content
Open

111 #21

Show file tree
Hide file tree
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
61 changes: 61 additions & 0 deletions src/CallListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;


public class CallListener {


ServerSocket ss;
String NickName;
boolean isBusy;
String ip;
Command c;
NickCommand nc;


/*CallListener(String NickName, boolean isBusy) throws IOException{
ss=new ServerSocket(Connection.port);

this.NickName=NickName;
this.isBusy=isBusy;
}*/


Connection getConnection(Socket s) throws IOException{
Connection c=new Connection(s);
c.chatApp2015(Connection.NickName);
if(isBusy){

c.userIsBusy(Connection.NickName);
c.disconnect();
return null;

}else{
c.chatApp2015(NickName);
this.c=c.recieve();

if(this.c.type==CommandType.NICK){
nc=(NickCommand) this.c;
return c;
}else{
return null;
}
/**/
}
}


void setBusy(boolean isBusy){
this.isBusy=isBusy;
}


String getNickName(){
return NickName;
}




}
63 changes: 63 additions & 0 deletions src/CallListenerThread.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Observable;


public class CallListenerThread extends Observable implements Runnable {


CallListener cl;
Connection c;
String NickName;
volatile boolean b;
ServerSocket ss;
Socket s;


@Override
public void run() {
try{
ss=new ServerSocket(Connection.port);

while(true){

s=ss.accept();
Connection c=new Connection(s);

if(c!=null){
CommandListenerThread clt=new CommandListenerThread(c);
clt.addObserver(Form.obj);
clt.start();
c.chatApp2015(Connection.NickName);
}

}

}catch (IOException e) {
e.printStackTrace();}

}





void start(){
b=false;
Thread t=new Thread(this);
t.start();
}


void stop(){
b=true;
}


Connection getConnection(){
return this.c;
}


}
23 changes: 23 additions & 0 deletions src/Caller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;


public class Caller {


InetAddress IP;


Caller(String ip) throws UnknownHostException{
IP=InetAddress.getByName(ip);
}


Connection call() throws IOException{
Connection c=new Connection(new Socket(IP, Connection.port));
c.chatApp2015(Connection.NickName);
return c;
}
}
41 changes: 41 additions & 0 deletions src/Command.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import java.util.Scanner;


public class Command {

CommandType type;
Command(CommandType type){
this.type=type;
}


public static Command callCommand(String s){

//Scanner in=new Scanner(System.in);
if(s.contains("Accepted")) {
return new Command(CommandType.ACCEPT);
}
if(s.contains("Rejected")) {
return new Command(CommandType.REJECT);
}
if(s.contains("Disconnect")) {
return new Command(CommandType.DISCONNECT);
}
if(s.contains("ChatApp 2015")) {
return new NickCommand(CommandType.NICK, s.replaceAll("user ", ""));
}
if(s.contains("Message")) {
return new MessageCommand(CommandType.MESSAGE,s.replaceAll("Message", ""));
}
return null;
}

public CommandType getType() {
return type;
}





}
44 changes: 44 additions & 0 deletions src/CommandListenerThread.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import java.util.Observable;


public class CommandListenerThread extends Observable implements Runnable{

Connection c;
volatile boolean b;
volatile Command com;

public CommandListenerThread(Connection c) {
this.c=c;
}

@Override
public void run() {
// TODO Auto-generated method stub
this.addObserver(Form.obj);
while(b!=true){
synchronized(this){
this.com=c.recieve();
setChanged();
this.notifyObservers(com);

}
}
}

public void start() {
this.b=false;
Thread t=new Thread(this);
t.start();
}


public void stop(){
b=true;
}


Connection getConnection(){
return this.c;
}

}
4 changes: 4 additions & 0 deletions src/CommandType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

public enum CommandType {
ACCEPT, REJECT, DISCONNECT, NICK, MESSAGE;
}
96 changes: 96 additions & 0 deletions src/Connection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;


class Connection {


private Socket socket;

public final static int port=28411;
public static String NickName;

private Scanner in;
private DataOutputStream out;



public Connection(Socket socket) throws IOException{
this.socket=socket;

in=new Scanner(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
}


Command recieve(){
Command c;
String s=in.nextLine();
c=Command.callCommand(s);
return c;
}


void accept(){
try {
out.writeUTF("Accpeted");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


void reject(){
try {
out.writeUTF("Rejected");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


void disconnect(){
try {
out.writeUTF("Disconnect");
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


void chatApp2015(String NickName){
try {
out.writeUTF(new StringBuffer("ChatApp 2015").append(Connection.NickName).toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


/*void userIsBusy(String NickName){
try {
out.writeUTF(new StringBuffer("ChatApp 2015").append(NickName).append("is busy").toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}*/


void sendMessage(String message){
try {
out.writeUTF(new StringBuffer("Message").append("\n").append(message).toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
Loading