Skip to content
Open
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
1 change: 1 addition & 0 deletions MYCHAT
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

11 changes: 11 additions & 0 deletions chatapp.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
99 changes: 99 additions & 0 deletions src/CallListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;

public class CallListener {
public static final int port = 28411;
public String localNick;
public String localAddress;
private boolean busy;
private SocketAddress listenAddress,remoteAdress;
private ServerSocket serverSocket;
public String remoteNick;
public Socket socket;
public CallListener() throws IOException {
this("without name");
}

public CallListener(String localNick) {
this.localNick = localNick;
this.serverSocket = null;

}

public CallListener(String localNick, String localAdress)throws IOException{
try {
serverSocket = new ServerSocket(28411);
} catch (IOException e) {
e.printStackTrace();
}
if (localAdress != null)
serverSocket.bind(new InetSocketAddress(localAdress, 28411));
this.localNick=localNick;
this.listenAddress =new InetSocketAddress(localAdress,port);
}

public Connection getConnection() throws IOException {
socket = serverSocket.accept();
return new Connection(socket, localNick);
}

public SocketAddress getListenAddress() {
return listenAddress;
}

public void setListenAddress(SocketAddress listenAddress) {
this.listenAddress = listenAddress;
}

public String getLocalNick() {
return localNick;
}

public void setLocalNick(String localNick) {
this.localNick = localNick;
}

public String getLocalAddress() {
return localAddress;
}

public void setLocalAddress(String localAddress) {
this.localAddress = localAddress;
}

public SocketAddress getRemoteAdress() {
return remoteAdress;
}

public void setRemoteAdress(SocketAddress remoteAdress) {
this.remoteAdress = remoteAdress;
}

public ServerSocket getServerSocket() {
return serverSocket;
}

public void setServerSocket(ServerSocket serverSocket) {
this.serverSocket = serverSocket;
}

public String getRemoteNick() {
return remoteNick;
}

public void setRemoteNick(String remoteNick) {
this.remoteNick = remoteNick;
}

public boolean isBusy() {
return busy;
}

public void setBusy(boolean busy) {
this.busy = busy;
}
}

88 changes: 88 additions & 0 deletions src/CallListenerThread.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import java.io.IOException;
import java.net.SocketAddress;
import java.util.Observable;
import java.util.concurrent.TimeUnit;

public class CallListenerThread extends Observable implements Runnable {
private CallListener callListener;
private Caller.CallStatus callStatus;
private Connection connection;
private volatile boolean isOpen;
// TODO: Add lastEvent;

public CallListenerThread() throws IOException {
callListener = new CallListener();
}

public CallListenerThread(String localNick) throws IOException {
callListener = new CallListener(localNick);
}

public CallListenerThread(String localNick, String localIp) throws IOException {
callListener = new CallListener(localNick, localIp);
}

public Caller.CallStatus getCallStatus() {
return callStatus;
}

public SocketAddress getListenAddress() throws IOException {
return callListener.getListenAddress();
}

public String getLocalNick() {
return callListener.getLocalNick();
}

public SocketAddress getRemoteAddress() throws IOException {
return callListener.getRemoteAdress();
}

public Connection getConnection() {
return connection;
}


public boolean isBusy() {
return callListener.isBusy();
}

@Override
public void run() {
while (true) {
try {
connection = callListener.getConnection();
if (connection == null) {
callStatus = Caller.CallStatus.valueOf("BUSY");
} else {
callStatus = Caller.CallStatus.valueOf("OK");
}
} catch (IOException e) {
System.out.println("SmthWrong");
}
setChanged();
notifyObservers();
}

}

public void setBusy(boolean busy) {
callListener.setBusy(busy);
}


public void setLocalNick(String localNick) {
callListener.setLocalNick(localNick);
}

public void start() {
this.isOpen = true;
Thread t = new Thread(this);
t.start();
};

public void stop() {
this.isOpen = false;
}

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

public class Caller {

private String nick;
private int localport;
private InetAddress ip;
private String NickCaller;
public static enum CallStatus{OK, NOT_ACCESSIBLE, BUSY, REJECTED, NO_SERVICE};
private CallStatus status;
private SocketAddress address;

public Socket getSocket() {
return socket;
}

public void setSocket(Socket socket) {
this.socket = socket;
}

private Socket socket;
public Caller(){
this("unnamed", new InetSocketAddress("127.0.0.1", 465));
}

public Caller(String localNick){
this(localNick, new InetSocketAddress("127.0.0.1",465));
}

public Caller(String localNick, SocketAddress remoteAddress){
this.nick=localNick;
this.address=remoteAddress;
}

public Caller(String localNick, String ip){
this(localNick, new InetSocketAddress(ip, 465));
}

public String getNick() {
return nick;
}

public void setNick(String nick) {
this.nick = nick;
}

public InetAddress getIp() {
return ip;
}

public void setIp(InetAddress ip) {
this.ip = ip;
}

public int getLocalport() {
return localport;
}

public void setLocalport(int localport) {
this.localport = localport;
}

public String getNickCaller() {
return NickCaller;
}

public void setNickCaller(String nickCaller) {
NickCaller = nickCaller;
}

public CallStatus getStatus() {
return status;
}

public void setStatus(CallStatus status) {
this.status = status;
}

public SocketAddress getAddress() {
return address;
}

public void setAddress(SocketAddress address) {
this.address = address;
}

public Connection call() throws InterruptedException {
String ip = address.toString(); // "/ip:port"
try {
Socket s = new Socket();
s.connect(address, 1000);
return new Connection(s, nick);
} catch (IOException e) {
System.out.println("Not connected");
return null;
}
}


}
Loading