Created
February 12, 2018 09:07
-
-
Save korkmazkadir/ed9ee6c4f5dc3b303f048e4abca31d7a to your computer and use it in GitHub Desktop.
Chat Client Server Implementasyonu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package simplechat.common; | |
import java.rmi.RemoteException; | |
import java.util.LinkedHashMap; | |
import java.util.Map; | |
import java.util.Set; | |
/** | |
* | |
* @author Kadir Korkmaz | |
*/ | |
public class Server implements ChatServer { | |
private final Map<String, ChatClient> usernameToClientObject = new LinkedHashMap<>(); | |
public Server() { | |
} | |
@Override | |
public String[] registerClient(ChatClient client) throws RemoteException { | |
System.out.println("Login : " + client.getName()); | |
usernameToClientObject.put(client.getName(), client); | |
Set<String> currentUserSet = usernameToClientObject.keySet(); | |
String[] currentUserList = currentUserSet.toArray(new String[currentUserSet.size()]); | |
for (Map.Entry<String, ChatClient> entry : usernameToClientObject.entrySet()) { | |
ChatClient c = entry.getValue(); | |
c.notifyLogin(client.getName()); | |
} | |
return currentUserList; | |
} | |
@Override | |
public void unregisterClient(ChatClient client) throws RemoteException { | |
System.out.println("Logout : " + client.getName()); | |
usernameToClientObject.remove(client.getName()); | |
for (Map.Entry<String, ChatClient> entry : usernameToClientObject.entrySet()) { | |
ChatClient c = entry.getValue(); | |
c.notifyLogout(client.getName()); | |
} | |
} | |
@Override | |
public void sendMessage(ChatClient from, String to, String message) throws RemoteException { | |
ChatClient messageReceiver = usernameToClientObject.get(to); | |
if (messageReceiver != null) { | |
messageReceiver.notifyMessage(from.getName(), message); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment