Commit 78a53180 authored by mohammad.salama's avatar mohammad.salama

No GUI , READY FOR ONE

parent c7021a1e
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
......@@ -4,16 +4,12 @@
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="71bec43c-ef6e-431f-a77c-2b7f124f5fea" name="Changes" comment="">
<change afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/encodings.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/pom.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/ChatClient.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/IServerChat.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/org/example/Main.java" afterDir="false" />
<list default="true" id="71bec43c-ef6e-431f-a77c-2b7f124f5fea" name="Changes" comment="printing Done">
<change afterPath="$PROJECT_DIR$/../ChatServer/src/main/java/IClientChat.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../ChatServer/.idea/vcs.xml" beforeDir="false" afterPath="$PROJECT_DIR$/../ChatServer/.idea/vcs.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../ChatServer/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/../ChatServer/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../ChatServer/src/main/java/IServerChat.java" beforeDir="false" afterPath="$PROJECT_DIR$/../ChatServer/src/main/java/IServerChat.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../ChatServer/src/main/java/ServerChatImp.java" beforeDir="false" afterPath="$PROJECT_DIR$/../ChatServer/src/main/java/ServerChatImp.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
......@@ -29,7 +25,7 @@
</option>
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$/.." />
</component>
<component name="MarkdownSettingsMigration">
<option name="stateVersion" value="1" />
......@@ -47,9 +43,37 @@
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"SHARE_PROJECT_CONFIGURATION_FILES": "true",
"last_opened_file_path": "D:/HIAST/FIY/FS/Distributed Systems/Lab/3/HW/ChatClient"
"last_opened_file_path": "D:/HIAST/FIY/FS/Distributed Systems/Lab/3/HW/ChatServer"
}
}]]></component>
<component name="RunManager">
<configuration name="Client1" type="Application" factoryName="Application">
<option name="MAIN_CLASS_NAME" value="ChatClient" />
<module name="ChatClient" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="Client2" type="Application" factoryName="Application">
<option name="MAIN_CLASS_NAME" value="ChatClient" />
<module name="ChatClient" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="Client3" type="Application" factoryName="Application">
<option name="MAIN_CLASS_NAME" value="ChatClient" />
<module name="ChatClient" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<list>
<item itemvalue="Application.Client1" />
<item itemvalue="Application.Client2" />
<item itemvalue="Application.Client3" />
</list>
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
......@@ -59,6 +83,14 @@
<option name="presentableId" value="Default" />
<updated>1699584495345</updated>
</task>
<task id="LOCAL-00001" summary="printing Done">
<created>1699587951299</created>
<option name="number" value="00001" />
<option name="presentableId" value="LOCAL-00001" />
<option name="project" value="LOCAL" />
<updated>1699587951299</updated>
</task>
<option name="localTasksCounter" value="2" />
<servers />
</component>
<component name="Vcs.Log.Tabs.Properties">
......@@ -72,4 +104,8 @@
</map>
</option>
</component>
<component name="VcsManagerConfiguration">
<MESSAGE value="printing Done" />
<option name="LAST_COMMIT_MESSAGE" value="printing Done" />
</component>
</project>
\ No newline at end of file
import com.sun.org.apache.xerces.internal.impl.xpath.XPath;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.Scanner;
public class ChatClient
{
public static void main(String[] args) throws MalformedURLException, NotBoundException, RemoteException {
String loc = "localhost";
IServerChat iServerChat = (IServerChat) Naming.lookup("rmi://"+loc+":1099/chat");
String fname = "MM";
String lname = "SS";
String username = "ms";
String password = "123";
iServerChat.Register(fname , lname , username , password);
iServerChat.createChatRoom("room1" , username);
iServerChat.LogIn(username , password);
iServerChat.createChatRoom("room2" ,username);
iServerChat.createChatRoom("room3" , username);
iServerChat.deleteChatRoom("room2", username);
iServerChat.LogOut(username , password);
String fname = "Client";
String lname = "";
String userName = "C";
String password = "11";
ClientChatImp clientChatImp = new ClientChatImp(fname , lname+"1",userName+"1" , password , loc);
ClientChatImp clientChatImp2 = new ClientChatImp(fname , lname+"2",userName+"2" , password , loc);
clientChatImp.Register(fname , lname+"1",userName+"1" , password );
clientChatImp2.Register(fname , lname+"2",userName+"2" , password );
clientChatImp.createChatRoom("room1");
clientChatImp2.createChatRoom("room2");
clientChatImp.joinChatRoom("room2");
clientChatImp2.joinChatRoom("room1");
System.out.println("Hello World");
clientChatImp.sendMessage("room1" , "Hello");
clientChatImp2.sendMessage("room2" , "Hello");
/*ClientChatImp[] clientChatImps = new ClientChatImp[3];
for (Integer i=1 ; i<=3 ; i++)
{
clientChatImps[i-1] = new ClientChatImp
(fname , lname+i.toString() , userName+i.toString() , password , loc);
}
Scanner scanner = new Scanner(System.in);
Integer choice = scanner.nextInt();
choice -=1;
clientChatImps[choice].createChatRoom("room"+choice.toString());
Integer x = choice;
while (choice > 0)
{
clientChatImps[choice].joinChatRoom("room"+x.toString());
x--;
}
int g = scanner.nextInt();
clientChatImps[choice].sendMessage("room"+choice.toString() , "Hello from"+choice.toString());
for (String str : clientChatImps[choice].getChatRooms())
{
System.out.println(str);
}*/
System.out.println("DONE");
}
}
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.List;
public class ClientChatImp extends UnicastRemoteObject implements IClientChat
{
private String firstName, lastName, userName,password;
IServerChat iServerChat;
public ClientChatImp(String firstName , String lastName , String userName , String password , String server) throws RemoteException, MalformedURLException, NotBoundException {
this.lastName = lastName;
this.userName = userName;
this.firstName = firstName;
this.password = password;
this.iServerChat = (IServerChat) Naming.lookup("rmi://"+server+":1099/chat");
}
@Override
public String receiveMessage(String Message) throws RemoteException
{
System.out.println(Message);
return Message;
}
@Override
public String getUserName() throws RemoteException
{
//return this.userName + " ( " + this.firstName+" " + this.lastName+" )";
return this.userName;
}
public void createChatRoom(String roomName) throws RemoteException
{
iServerChat.createChatRoom(roomName , this);
}
public void joinChatRoom(String roomName) throws RemoteException
{
iServerChat.joinChatRoom(roomName , this);
}
public void deleteChatRoom(String roomName) throws RemoteException
{
iServerChat.deleteChatRoom(roomName , this);
}
public void Register(String fname , String lname, String username, String password ) throws RemoteException
{
iServerChat.Register(fname, lname , username , password , this);
}
public void LogIn(String password) throws RemoteException
{
iServerChat.LogIn(password , this);
}
public void LogOut() throws RemoteException
{
iServerChat.LogOut(this);
}
public List<String> getChatRooms() throws RemoteException
{
return iServerChat.getChatRooms(this);
}
public List<String> getConversation(String roomName) throws RemoteException
{
return iServerChat.getConversation(roomName , this);
}
public void sendMessage(String roomName, String message) throws RemoteException
{
iServerChat.sendMessage(roomName , message , this);
}
}
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface IClientChat extends Remote
{
public String receiveMessage (String Message) throws RemoteException;
/// public Boolean checkPassword (String password) throws RemoteException;
public String getUserName () throws RemoteException;
}
......@@ -5,23 +5,22 @@ import java.util.List;
public interface IServerChat extends Remote
{
public void createChatRoom(String roomName , String userName) throws RemoteException;
public void createChatRoom(String roomName , IClientChat iClientChat) throws RemoteException;
///public void joinChatRoom(String roomName) throws RemoteException;
public void joinChatRoom(String roomName, IClientChat iClientChat) throws RemoteException;
public void deleteChatRoom(String roomName , String userName) throws RemoteException;
public void deleteChatRoom(String roomName , IClientChat iClientChat) throws RemoteException;
public void Register(String fname , String lname, String username, String password) throws RemoteException;
public void Register(String fname , String lname, String username, String password , IClientChat iClientChat) throws RemoteException;
public void LogIn(String username, String password) throws RemoteException;
public void LogIn( String password , IClientChat iClientChat) throws RemoteException;
/// public List<String> getChatRooms(String username , String password) throws RemoteException;
public List<String> getChatRooms(IClientChat iClientChat) throws RemoteException;
/// public List<String> getConversation(String roomName , String username , String password) throws RemoteException;
public List<String> getConversation(String roomName , IClientChat iClientChat) throws RemoteException;
// public void sendMessage(String roomName, String message , String username , String password ) throws RemoteException;
public void sendMessage(String roomName, String message , IClientChat iClientChat ) throws RemoteException;
public void LogOut(String username , String Password) throws RemoteException;
public void LogOut(IClientChat iClientChat) throws RemoteException;
/// public void addListener (String roomName ) throws RemoteException;
}
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
......@@ -5,7 +5,12 @@
</component>
<component name="ChangeListManager">
<list default="true" id="23a09f69-45e8-4607-a94c-253a284a386d" name="Changes" comment="ChatServiceFirst5">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/../ChatClient/src/main/java/ClientChatImp.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/../ChatClient/src/main/java/IClientChat.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../ChatClient/.idea/vcs.xml" beforeDir="false" afterPath="$PROJECT_DIR$/../ChatClient/.idea/vcs.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../ChatClient/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/../ChatClient/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../ChatClient/src/main/java/ChatClient.java" beforeDir="false" afterPath="$PROJECT_DIR$/../ChatClient/src/main/java/ChatClient.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../ChatClient/src/main/java/IServerChat.java" beforeDir="false" afterPath="$PROJECT_DIR$/../ChatClient/src/main/java/IServerChat.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
......@@ -15,13 +20,13 @@
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Interface" />
<option value="Class" />
<option value="Interface" />
</list>
</option>
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$/.." />
</component>
<component name="MarkdownSettingsMigration">
<option name="stateVersion" value="1" />
......@@ -39,7 +44,8 @@
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"SHARE_PROJECT_CONFIGURATION_FILES": "true",
"last_opened_file_path": "D:/HIAST/FIY/FS/Distributed Systems/Lab/3/HW/ChatClient"
"last_opened_file_path": "D:/HIAST/FIY/FS/Distributed Systems/Lab/3/HW/ChatServer",
"settings.editor.selected.configurable": "project.propVCSSupport.DirectoryMappings"
}
}]]></component>
<component name="RecentsManager">
......
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface IClientChat extends Remote
{
public String receiveMessage (String Message) throws RemoteException;
/// public Boolean checkPassword (String password) throws RemoteException;
public String getUserName () throws RemoteException;
}
......@@ -5,23 +5,23 @@ import java.util.List;
public interface IServerChat extends Remote
{
public void createChatRoom(String roomName , String userName) throws RemoteException;
public void createChatRoom(String roomName , IClientChat iClientChat) throws RemoteException;
///public void joinChatRoom(String roomName) throws RemoteException;
public void joinChatRoom(String roomName, IClientChat iClientChat) throws RemoteException;
public void deleteChatRoom(String roomName , String userName) throws RemoteException;
public void deleteChatRoom(String roomName , IClientChat iClientChat) throws RemoteException;
public void Register(String fname , String lname, String username, String password) throws RemoteException;
public void Register(String fname , String lname, String username, String password , IClientChat iClientChat) throws RemoteException;
public void LogIn(String username, String password) throws RemoteException;
public void LogIn(String password , IClientChat iClientChat) throws RemoteException;
/// public List<String> getChatRooms(String username , String password) throws RemoteException;
public List<String> getChatRooms(IClientChat iClientChat) throws RemoteException;
/// public List<String> getConversation(String roomName , String username , String password) throws RemoteException;
public List<String> getConversation(String roomName , IClientChat iClientChat) throws RemoteException;
// public void sendMessage(String roomName, String message , String username , String password ) throws RemoteException;
public void sendMessage(String roomName, String message , IClientChat iClientChat ) throws RemoteException;
public void LogOut(String username , String Password) throws RemoteException;
public void LogOut(IClientChat iClientChat) throws RemoteException;
/// public void addListener (String roomName ) throws RemoteException;
}
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
public class ServerChatImp extends UnicastRemoteObject implements IServerChat
{
Map<String , UsersDB> users = new HashMap<>();
Map<UsersDB , List<String>> ChatRooms = new HashMap<>();
Map<String , IClientChat> users = new HashMap<>();
Map<IClientChat , List<String>> ChatRoomsOfUser = new HashMap<>(); /// Creator -> chat rooms created
Map<IClientChat , Boolean> ActiveUsers = new HashMap<>();
Map<IClientChat , String> UsersPasswords = new HashMap<>();
Map<UsersDB , Boolean> ActiveUsers = new HashMap<>();
Map<String , List<String>> ChatRoomMessages = new HashMap<>();
Map<String , List<IClientChat>> ChatRoomMembers = new HashMap<>();
protected ServerChatImp() throws RemoteException
{
}
@Override
public void createChatRoom(String roomName, String userName) throws RemoteException
public void createChatRoom(String roomName, IClientChat iClientChat) throws RemoteException
{
String userName = iClientChat.getUserName();
if (!users.containsKey(userName)) return;
UsersDB usersDB = users.get(userName);
ChatRooms.get(usersDB).add(roomName);
ChatRoomsOfUser.get(iClientChat).add(roomName);
ChatRoomMembers.put(roomName , new ArrayList<>());
ChatRoomMembers.get(roomName).add(iClientChat);
ChatRoomMessages.put(roomName , new ArrayList<>());
}
System.out.println("User : " + userName + " -- now has the following chat rooms");
for (String str : ChatRooms.get(usersDB))
@Override
public void joinChatRoom(String roomName, IClientChat iClientChat) throws RemoteException
{
System.out.println(str);
}
System.out.println("*****************************");
if (!ChatRoomMembers.containsKey(roomName)) return;
ChatRoomMembers.get(roomName).add(iClientChat);
// System.out.println("Added to " + roomName + " pppp : " + iClientChat.getUserName());
}
@Override
public void deleteChatRoom(String roomName, String userName) throws RemoteException
public void deleteChatRoom(String roomName, IClientChat iClientChat) throws RemoteException
{
String userName = iClientChat.getUserName();
if (!users.containsKey(userName))
{
System.out.println("No Such User");
// System.out.println("No Such User");
return;
}
UsersDB usersDB = users.get(userName);
if (ChatRooms.get(usersDB).contains(roomName)) {
ChatRooms.get(usersDB).remove(roomName);
System.out.println("User : " + userName + " -- now has the following chat rooms");
for (String str : ChatRooms.get(usersDB)) {
System.out.println(str);
}
System.out.println("*****************************");
if (ChatRoomsOfUser.get(iClientChat).contains(roomName))
{
ChatRoomsOfUser.get(iClientChat).remove(roomName);
ChatRoomMembers.remove(roomName);
}
else System.out.println("No Such Room for " + userName);
}
@Override
public void Register(String fname, String lname, String username, String password) throws RemoteException
public void Register(String fname, String lname, String username, String password , IClientChat iClientChat) throws RemoteException
{
UsersDB usersDB = new UsersDB(fname , lname , username , password);
users.put(username , usersDB);
ChatRooms.put(usersDB , new ArrayList<>());
users.put(username , iClientChat);
ChatRoomsOfUser.put(iClientChat , new ArrayList<>());
UsersPasswords.put(iClientChat , password);
ActiveUsers.put(iClientChat , true);
/// System.out.println("User " + iClientChat.getUserName() + "Logged in");
}
@Override
public void LogIn(String username, String password) throws RemoteException
public void LogIn(String password , IClientChat iClientChat) throws RemoteException
{
if (!users.containsKey(username)) return;
String username = iClientChat.getUserName();
if (!users.containsKey(username))
{
iClientChat.receiveMessage("You do not have an account");
return;
}
/*
validation and authentication for later
* */
UsersDB usersDB = users.get(username);
if (usersDB.checkPassword(password)) ActiveUsers.put(usersDB, true);
if (UsersPasswords.get(iClientChat).equals(password)) ActiveUsers.put(iClientChat , true);
else iClientChat.receiveMessage("Wrong pass");
}
@Override
public List<String> getChatRooms(IClientChat iClientChat) throws RemoteException
{
if (ChatRoomsOfUser.get(iClientChat).size() == 0)
{
return new ArrayList<>();
}
else return ChatRoomsOfUser.get(iClientChat);
}
@Override
public List<String> getConversation(String roomName, IClientChat iClientChat) throws RemoteException
{
if (!(ChatRoomMembers.containsKey(roomName) && (ChatRoomMembers.get(roomName)).contains(iClientChat)))
{
return new ArrayList<>();
}
return ChatRoomMessages.get(roomName);
}
@Override
public void sendMessage(String roomName, String Message, IClientChat iClientChat) throws RemoteException
{
if (ChatRoomMembers.containsKey(roomName) && ChatRoomMembers.get(roomName).contains(iClientChat))
{
Message = iClientChat.getUserName() + " : " + Message;
Message = Message + "\n" + "************************************************\n";
String finalMessage = Message;
ChatRoomMembers.get(roomName).stream().forEach
(
client->
{
if (ActiveUsers.get(client))
try
{
System.out.println("Can send to " + client.getUserName());
client.receiveMessage(finalMessage);
}
catch (RemoteException e)
{
ActiveUsers.put(client , false);
}
}
);
}
}
@Override
public void LogOut(String username, String Password) throws RemoteException
public void LogOut(IClientChat iClientChat) throws RemoteException
{
String username = iClientChat.getUserName();
if (!users.containsKey(username)) return;
UsersDB usersDB = users.get(username);
ActiveUsers.put(usersDB, false);
ActiveUsers.put(iClientChat, false);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment