Commit cb842ad0 authored by mohammad.salama's avatar mohammad.salama

First Commit Before our Session - Not Complete at All

parents
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="Chat-Client" />
</profile>
</annotationProcessing>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://repo.maven.apache.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="AutoImportSettings">
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="16e3e293-5a4b-4582-b2a2-90cc5c04256b" name="Changes" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Class" />
<option value="Interface" />
</list>
</option>
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="MarkdownSettingsMigration">
<option name="stateVersion" value="1" />
</component>
<component name="ProjectId" id="2XsDlLnCXQZvFPl0nP3IFSFxbQF" />
<component name="ProjectLevelVcsManager" settingsEditedManually="true">
<ConfirmationsSetting value="1" id="Add" />
</component>
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"SHARE_PROJECT_CONFIGURATION_FILES": "true",
"last_opened_file_path": "D:/HIAST/FIY/FS/Distributed Systems/Lab/3/RoomChatServer"
}
}]]></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">
<changelist id="16e3e293-5a4b-4582-b2a2-90cc5c04256b" name="Changes" comment="" />
<created>1699409043548</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1699409043548</updated>
</task>
<servers />
</component>
<component name="Vcs.Log.Tabs.Properties">
<option name="TAB_STATES">
<map>
<entry key="MAIN">
<value>
<State />
</value>
</entry>
</map>
</option>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>Chat-Client</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
\ No newline at end of file
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.List;
public interface ChatInterface extends Remote
{
public void createChatRoom(String roomName , String userName , String Password) throws RemoteException;
public void joinChatRoom(String roomName , String userName , String Password) throws RemoteException;
public void deleteChatRoom(String roomName , String userName, String Password) throws RemoteException;
public void Register(String fname , String lname, String username, String password) throws RemoteException;
public void LogIn(String username, String password , ListenersInterface listenersInterface) throws RemoteException;
public List<String> getChatRooms(String username , String password) throws RemoteException;
public List<Message> getConversation(String roomName , String username , String password) throws RemoteException;
public void sendMessage(String roomName, String message , String username , String password , ListenersInterface listenersInterface) throws RemoteException;
public void LogOut(String username , String Password) throws RemoteException;
}
import java.util.*;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class ListenersImp extends UnicastRemoteObject implements ListenersInterface
{
private String FirstName;
private String LastName;
private String userName;
private String Password;
private ChatInterface chatInterface;
public ListenersImp(String FirstName, String LastName , String userName , String Password) throws Exception
{
chatInterface = (ChatInterface) Naming.lookup("localhost/ChatService");
chatInterface.Register(FirstName , LastName , userName , Password);
this.userName = userName;
this.FirstName = FirstName;
this.LastName = LastName;
this.Password = Password;
}
public void createChatRoom(String chatRoomName) throws RemoteException
{
chatInterface.createChatRoom(chatRoomName , this.userName , this.Password);
}
public List<String> getChatRooms() throws RemoteException
{
return chatInterface.getChatRooms(this.userName , this.Password);
}
public void sendMessage(String roomName , String msg) throws RemoteException
{
chatInterface.sendMessage(roomName , msg , this.userName , this.Password , this);
}
public void receiveMessage(Message message) throws RuntimeException
{
message.printMessage();
}
public String getName()
{
return this.FirstName+" "+this.LastName+" : \n";
}
public void joinChatRoom(String roomName) throws RemoteException
{
chatInterface.joinChatRoom(roomName , this.userName , this.Password);
}
public void deleteChatRoom(String roomName) throws RemoteException
{
chatInterface.deleteChatRoom(roomName , this.userName , this.Password);
}
public void LogIn() throws RemoteException
{
chatInterface.LogIn(this.userName , this.Password , this);
}
public List<Message> getConversation(String roomName) throws RemoteException
{
return chatInterface.getConversation(roomName , this.userName , this.Password);
}
public void LogOut() throws RemoteException
{
chatInterface.LogOut(this.userName , this.Password);
}
}
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface ListenersInterface extends Remote
{
public void receiveMessage(Message message) throws RemoteException;
public String getName () throws RemoteException;
}
public class Message
{
private String owner;
private String Content;
public Message(ListenersImp owner, String content)
{
this.owner = owner.getName();
Content = content;
}
public String getMessage()
{
return owner+Content;
}
public void printMessage()
{
System.out.println(getMessage());
}
}
package org.example;
public class Main
{
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
\ No newline at end of file
# Default ignored files
/shelf/
/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="Chat-Server" />
</profile>
</annotationProcessing>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://repo.maven.apache.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>
\ No newline at end of file
grant
{
permission java.security.AllPermission;
};
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>Chat-Server</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
\ No newline at end of file
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.List;
public interface ChatInterface extends Remote
{
public void createChatRoom(String roomName , String userName , String Password) throws RemoteException;
public void joinChatRoom(String roomName , String userName , String Password) throws RemoteException;
public void deleteChatRoom(String roomName , String userName, String Password) throws RemoteException;
public void Register(String fname , String lname, String username, String password) throws RemoteException;
public void LogIn(String username, String password , ListenersInterface listenersInterface) throws RemoteException;
public List<String> getChatRooms(String username , String password) throws RemoteException;
public List<Message> getConversation(String roomName , String username , String password) throws RemoteException;
public void sendMessage(String roomName, String message , String username , String password , ListenersInterface listenersInterface) throws RemoteException;
public void LogOut(String username , String Password) throws RemoteException;
public void addListener (String roomName , ListenersInterface listenersInterface) 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;
public class ChatServer extends UnicastRemoteObject implements ChatInterface
{
static Map<String , Boolean> Active = new HashMap<>();
static Map<ListenersInterface , Boolean> ActiveListener = new HashMap<>();
static Map<String , List<ListenersInterface>> ChatRoomMembers = new HashMap<>();
public ChatServer() throws RemoteException
{
}
public boolean checkUser(String username , String Password)
{
if (!checkIfActive(username)) return false;
return User.checkUser(username , Password);
}
public boolean checkIfActive(String userName)
{
boolean active = Active.getOrDefault(userName, false);
return active;
}
public void createChatRoom(String roomName , String userName , String Password) throws RemoteException
{
if (! checkUser(userName , Password)) return;
List list = User.ChatRooms.getOrDefault(userName,null);
if (!list.contains(roomName))
{
list.add(roomName);
}
List<User> userList = new ArrayList<>();
userList.add(User.Users.get(userName));
User.ChatRoomMembers.put(roomName , userList);
}
public void joinChatRoom(String roomName, String userName, String Password) throws RemoteException
{
if (!checkUser(userName , Password)) return;
if (User.Conversations.getOrDefault(roomName , null).equals(null)) return;
List<String> list = User.ChatRooms.get(userName);
list.add(roomName);
User.ChatRooms.put(userName , list);
List<User> userList = User.ChatRoomMembers.get(roomName);
userList.add(User.Users.get(userName));
User.ChatRoomMembers.put(roomName , userList);
}
public void deleteChatRoom(String roomName , String userName, String Password) throws RemoteException
{
if (!checkUser(userName , Password)) return;
if (User.ChatRooms.getOrDefault(roomName , null) == null)
{
return;
}
User.ChatRooms.get(roomName).remove(roomName);
User.Conversations.get(roomName).clear();
List<User> list = User.ChatRoomMembers.get(roomName);
for (User user : list)
{
User.ChatRooms.get(User.Users.get(userName)).remove(roomName);
}
}
public void Register(String fname , String lname, String username, String password) throws RemoteException
{
User user = new User(fname , lname , username , password);
}
public void LogIn(String username, String password , ListenersInterface listenersInterface) throws RemoteException
{
if (!User.checkUser(username,password))
{
System.out.println("Username or Password is incorrect !!!!!");
}
else
{
Active.put(username , true);
ActiveListener.put(listenersInterface , true);
}
}
public List<String> getChatRooms(String username , String password) throws RemoteException
{
if (!checkUser(username , password)) return null;
return User.getChatRooms(username);
}
public List<Message> getConversation(String roomName , String username , String password) throws RemoteException
{
if (!checkUser(username , password)) return null;
return User.Conversations.get(roomName);
}
public void sendMessage(String roomName, String message , String username , String password , ListenersInterface listenersInterface) throws RemoteException
{
if (! checkUser(username , password)) return;
User temp = User.Users.get(username);
Message msg = new Message(listenersInterface , message);
List<ListenersInterface> list =ChatRoomMembers.get(roomName);
for (ListenersInterface LI : list)
{
LI.receiveMessage(msg);
}
}
public void LogOut(String username , String Password) throws RemoteException
{
if (!checkUser(username , Password)) return;
Active.put(username , false);
}
public void addListener(String roomName, ListenersInterface listenersInterface) throws RemoteException
{
if (ChatRoomMembers.getOrDefault(roomName , null).equals(null))
{
List<ListenersInterface> list = new ArrayList<>();
list.add(listenersInterface);
ChatRoomMembers.put(roomName , list);
}
else
{
List<ListenersInterface> list =ChatRoomMembers.get(roomName);
list.add(listenersInterface);
ChatRoomMembers.put(roomName , list);
}
}
}
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.List;
public interface ListenersInterface extends Remote
{
public void receiveMessage(Message message) throws RemoteException;
public String getName () throws RemoteException;
}
import java.rmi.RemoteException;
public class Message
{
private String owner;
private String Content;
public Message(ListenersInterface owner, String content) throws RemoteException
{
this.owner = owner.getName();
Content = content;
}
public String getMessage()
{
return owner+Content;
}
public void printMessage()
{
System.out.println(getMessage());
}
}
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.List;
public class RmiServer
{
public static void main(String[] args) throws RuntimeException, RemoteException
{
/*
System.setProperty("java.security.policy" , "./RmiPolicy.policy");
System.setSecurityManager(new RMISecurityManager());
Registry registry = LocateRegistry.createRegistry(1099);
String ChatService = "ChatService";
ChatServer chatServer = new ChatServer();
registry.rebind(ChatService , chatServer);*/
}
}
import javax.swing.*;
import java.util.*;
public class User
{
private String FirstName;
private String LastName;
private String userName;
private String Password;
/// username to chat rooms he is in
static Map<String , List<String>> ChatRooms = new HashMap<>();
//chat room name to messages in it
static Map<String , List<Message>> Conversations = new HashMap<>();
///username to password
static Map<String , String> UsernamePassword = new HashMap<>();
///Username to User Class
static Map<String , User> Users = new HashMap<>();
/// Chat Room name to its members
static Map<String , List<User>> ChatRoomMembers = new HashMap<>();
public User(String FirstName, String LastName , String userName , String Password)
{
Scanner scanner = new Scanner(System.in);
while(userName.equals(""))
{
System.out.println("User Name Cannot Be Empty , Re-Enter User Name");
userName = scanner.next();
}
while (Users.containsKey(userName))
{
System.out.println("User Name is Taken , Please Re-Enter new User Name");
userName = scanner.next();
}
this.FirstName = FirstName;
this.LastName = LastName;
this.Password = Password;
this.userName = userName;
Users.put(userName,this);
UsernamePassword.put(userName,Password);
ChatRooms.put(userName , new ArrayList<>());
Conversations.put(userName , new ArrayList<>());
}
public static boolean checkUser(String Uname , String pass)
{
return UsernamePassword.containsKey(Uname) && UsernamePassword.get(Uname).equals(pass);
}
public void createChatRoom(String chatRoomName)
{
if (ChatRooms.get(this.userName).contains(chatRoomName)) return;
ChatRooms.get(this.userName).add(chatRoomName);
}
public static List<String> getChatRooms(String userName)
{
return ChatRooms.get(userName);
}
public String getName()
{
return this.FirstName+ " " +this.LastName+" : \n";
}
public static void addMessage(String roomName , Message msg)
{
Conversations.get(roomName).add(msg);
}
public boolean checkPassword(String Password)
{
return this.Password.equals(Password);
}
}
package org.example;
public class Main
{
public static void main(String[] args)
{
}
}
\ No newline at end of file
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