Guest User

Untitled

a guest
Apr 1st, 2018
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.08 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.io.ObjectInputStream;
  3. import java.io.ObjectOutputStream;
  4. import java.io.PrintWriter;
  5. import java.net.Socket;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import java.util.Scanner;
  9. import java.util.logging.Level;
  10. import java.util.logging.Logger;
  11.  
  12.  
  13. public class ClientShell {
  14. public static final String COMMAND_SEPARATOR = "%%";
  15.  
  16. static User loggedInUser = null;
  17. public static void main(String[] args) throws ClassNotFoundException {
  18. try {
  19.  
  20. String message = "";
  21. String response = null;
  22. Scanner keyboard = new Scanner(System.in);
  23.  
  24. // 1) Build data socket for client to use
  25. Socket serverLink = new Socket("localhost", 5555);
  26.  
  27. // 2) Set up streams
  28. // PrintWriter used for sending messages
  29. PrintWriter output = new PrintWriter(serverLink.getOutputStream());
  30. // Scanner / BufferedReader used for receiving messages
  31. Scanner input = new Scanner(serverLink.getInputStream());
  32. ObjectOutputStream objectOutput = new ObjectOutputStream(serverLink.getOutputStream());
  33. ObjectInputStream objectInput = new ObjectInputStream(serverLink.getInputStream());
  34.  
  35. // 3) While we want still want to exchange messages
  36. while(!message.equalsIgnoreCase("exit"))
  37. {
  38. while(loggedInUser==null){
  39. notLoggedInOptions(keyboard,message,response,output,input,objectInput);
  40. }
  41. loggedInOptions(keyboard,message,response,output,input,objectInput,objectOutput);
  42.  
  43.  
  44. }
  45. // 8) Close the link to the server (the data socket)
  46. serverLink.close();
  47. } catch (IOException ex) {
  48. Logger.getLogger(ClientShell.class.getName()).log(Level.SEVERE, null, ex);
  49. }
  50. }
  51.  
  52. public static void notLoggedInOptions(Scanner keyboard,String message,String response,
  53. PrintWriter output,Scanner input,ObjectInputStream objectInput) throws IOException, ClassNotFoundException{
  54. System.out.println("Type login for login or register for register");
  55. message = keyboard.nextLine();
  56. if(message.equalsIgnoreCase("register")){
  57. message="";
  58. boolean validUserName = false;
  59. while(validUserName == false){
  60. System.out.println("Type the username you want as your prefix for your email");
  61. System.out.println("It must be between 5 and 10 characters");
  62. System.out.println("eg type mgraham if you want mgraham@danielhaughton@haughton.com");
  63. message = keyboard.nextLine();
  64. if(message.length()>4 && message.length()<11 == true){
  65. validUserName = true;
  66. }else{
  67. System.out.println("It must be between 5 and 10 characters try again" );
  68. }
  69. }
  70. String username = message;
  71. message="";
  72. boolean validPassword = false;
  73. while(validPassword == false){
  74. System.out.println("Type the password you want");
  75. System.out.println("It must be atleast 8 characterrs");
  76. message = keyboard.nextLine();
  77. if(message.length()> 7){
  78. validPassword = true;
  79. }else{
  80. System.out.println("It must be atleast 8 characterrs,try again");
  81. }
  82. }
  83. String password = message;
  84. output.println("register"+ COMMAND_SEPARATOR + username + COMMAND_SEPARATOR + password);
  85. output.flush();
  86.  
  87. response = input.nextLine();
  88. System.out.println(response);
  89. }else if(message.equalsIgnoreCase("login")){
  90. System.out.println("Type your full email eg goku@danielhaughton.com");
  91. String email = keyboard.nextLine();
  92. System.out.println("Type your password(note:Case sensitive)");
  93. String password = keyboard.nextLine();
  94. output.println("login"+ COMMAND_SEPARATOR + email + COMMAND_SEPARATOR + password);
  95. output.flush();
  96.  
  97. Object responseObject = objectInput.readObject();
  98.  
  99. if(responseObject == null){
  100. System.out.println("the server couldnt find a user with those details");
  101. } else {
  102. loggedInUser = (User)responseObject;
  103. System.out.println("Welcome" + loggedInUser.getEmail());
  104.  
  105. }
  106. }
  107.  
  108. else{
  109. System.out.println("Unsupported command!try again");
  110. }
  111. }
  112. public static void loggedInOptions(Scanner keyboard,String message,String response,
  113. PrintWriter output,Scanner input,ObjectInputStream objectInput,
  114. ObjectOutputStream objectOutput) throws IOException, ClassNotFoundException{
  115. System.out.println("Type compose to send an email");
  116. System.out.println("Type mysent to see all of your sent emails");
  117. System.out.println("Type unreademails to see all of your emails");
  118. System.out.println("Type allemails to view all of your emails");
  119. System.out.println("Type read-x to read the email with the ID x");
  120. message = keyboard.nextLine();
  121. if(message.equals("compose")){
  122. compose(keyboard,message,response,output,input,objectInput,objectOutput);
  123. }else if(message.equals("mysent")){
  124. mysent(keyboard,message,response,output,input,objectInput,objectOutput);
  125. }
  126. }
  127.  
  128. public static void compose(Scanner keyboard,String message,String response,
  129. PrintWriter output,Scanner input,ObjectInputStream objectInput,
  130. ObjectOutputStream objectOutput) throws IOException{
  131. System.out.println("What is the subject of your email");
  132. String subject = keyboard.nextLine();
  133. System.out.println("What is the text of your email");
  134. String text = keyboard.nextLine();
  135. ArrayList<String> to = new ArrayList<>();
  136. System.out.println("How many people do you want to send the email to?(write 2 not two)");
  137. int number = Integer.parseInt(keyboard.nextLine());
  138. for(int i=0;i<number;i++){
  139. System.out.println("Enter one of the addresses you want to send to");
  140. to.add(keyboard.nextLine());
  141. Email email = new Email(0,loggedInUser.getEmail(),to,subject,text);
  142. //0 is an id placeholder
  143. //will be replaced by server
  144. output.println("send");
  145. output.flush();
  146. objectOutput.writeObject(email);
  147. objectOutput.flush();
  148. input.reset();
  149. response = input.nextLine().trim();
  150. System.out.println(response);
  151. }
  152. }
  153. public static void mysent(Scanner keyboard,String message,String response,
  154. PrintWriter output,Scanner input,ObjectInputStream objectInput,
  155. ObjectOutputStream objectOutput) throws IOException, ClassNotFoundException{
  156. output.println("mysent");
  157. output.flush();
  158. List<Email> results = (List<Email>) objectInput.readObject();
  159. System.out.println("done");
  160. }
  161. }
  162.  
  163. class Server implements Runnable
  164. {
  165. Socket connectionSocket;
  166. static UserRepo userRepo = new UserRepo();
  167. static EmailRepo emailRepo = new EmailRepo();
  168. public static final String COMMAND_SEPARATOR = "%%";
  169. private User loggedInUser;
  170. private ObjectOutputStream outputObject;
  171. private ObjectInputStream inputObject;
  172.  
  173. public Server(Socket s){
  174. try{
  175. System.out.println("Client Got Connected " );
  176. connectionSocket=s;
  177. loggedInUser= null;
  178.  
  179. }catch(Exception e){e.printStackTrace();}
  180. }
  181.  
  182. public void run(){
  183. try{
  184.  
  185. String recievedMessage="";
  186. BufferedReader reader =
  187. new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
  188. BufferedWriter writer=
  189. new BufferedWriter(new OutputStreamWriter(connectionSocket.getOutputStream()));
  190. outputObject = new ObjectOutputStream(connectionSocket.getOutputStream());
  191. inputObject = new ObjectInputStream(connectionSocket.getInputStream());
  192. while(!recievedMessage.equalsIgnoreCase("exit")){
  193. recievedMessage = reader.readLine().trim();
  194. String [] components = recievedMessage.split(COMMAND_SEPARATOR);
  195. if(components[0].equals("register")){
  196. User user= userRepo.addUser(components[1], components[2]);
  197. if(user==null){
  198. System.out.println("User is already in system");
  199. writer.write("Server Response: User wasnt added,"
  200. + "mayby there is already an account for that email? rn " );
  201. writer.flush();
  202. }else{
  203. writer.write("Server response:User was added,try logging in rn");
  204. System.out.println("user added");
  205. writer.flush();
  206. }
  207. }
  208.  
  209. else if(components[0].equals("login")){
  210. User user= userRepo.login(components[1], components[2]);
  211. loggedInUser = user;
  212. outputObject.writeObject(user);
  213. outputObject.flush();
  214. outputObject.reset();
  215. }
  216.  
  217. else if(components[0].equals("send")){
  218. Email email = (Email) inputObject.readObject();
  219. emailRepo.addEmail(email);
  220. writer.write("Server response:Email was sent rn");
  221. writer.flush();
  222. }
  223.  
  224.  
  225. else if(components[0].equals("mysent")){
  226. List<Email> results = Collections.synchronizedList(emailRepo.findEmailsSentBy(loggedInUser.getEmail()));
  227. outputObject.writeObject(results);
  228. outputObject.flush();
  229. outputObject.reset();
  230. }
  231.  
  232.  
  233.  
  234. recievedMessage="";
  235. }
  236. connectionSocket.close();
  237. }catch(Exception e){e.printStackTrace();}
  238. }
  239.  
  240. public static void main(String argv[]) throws Exception
  241. {
  242. System.out.println("Threaded Server is Running " );
  243. ServerSocket mysocket = new ServerSocket(5555);
  244. while(true)
  245. {
  246. Socket sock = mysocket.accept();
  247. Server server=new Server(sock);
  248.  
  249. Thread serverThread=new Thread(server);
  250. serverThread.start();
  251.  
  252. }
  253. }
  254. }
  255.  
  256. public class EmailRepo {
  257. private List<Email> emails;
  258. private static int counter = 0; //used for ids for emails
  259.  
  260. public EmailRepo() {
  261. emails =Collections.synchronizedList(new ArrayList<Email>());
  262. }
  263.  
  264. public synchronized boolean addEmail(Email email){
  265. counter ++;
  266. email.setId(counter);
  267. emails.add(email);
  268. return true;
  269. }
  270.  
  271. public synchronized List<Email> findEmailsSentBy(String by){
  272. List<Email> results = Collections.synchronizedList(new ArrayList<Email>());
  273.  
  274. for(int i=0;i<emails.size();i++){
  275. if(emails.get(i).getFrom().equalsIgnoreCase(by)){
  276. results.add(emails.get(i));
  277. }
  278. }
  279.  
  280. return results;
  281. }
  282.  
  283. public class Email implements Serializable {
  284. private int id;
  285. private String from;
  286. private ArrayList<String> to;
  287. private String subject;
  288. private String text;
  289. private Date date;
  290. //unreadby contains the email address of those who recieved the email and read it,
  291. private ArrayList<String> unreadBy;
  292. //markedasspambye contains email addresses of those who recieved the email and marked it as spam
  293. private ArrayList<String> markedAsSpamBy;
  294. private ArrayList<String> deletedBy;
  295.  
  296. public Email() {
  297. }
  298.  
  299. public Email(int id, String from, ArrayList<String> to, String subject, String text, Date date, ArrayList<String> unreadBy, ArrayList<String> markedAsSpamBy, ArrayList<String> deletedBy) {
  300. this.id = id;
  301. this.from = from;
  302. this.to = to;
  303. this.subject = subject;
  304. this.text = text;
  305. this.date = date;
  306. this.unreadBy = unreadBy;
  307. this.markedAsSpamBy = markedAsSpamBy;
  308. this.deletedBy = deletedBy;
  309. }
  310. public Email(int id,String from,ArrayList<String> to,String subject,String text){
  311. this.id = id;
  312. this.from = from;
  313. this.to = to;
  314. this.subject = subject;
  315. this.text = text;
  316. date = new Date();
  317. unreadBy = new ArrayList<>();
  318. //add all receiptants to unreadby as when an email is sent nobody has read it
  319. for(int i=0;i<to.size();i++){
  320. unreadBy.add(to.get(i));
  321. }
  322. markedAsSpamBy = new ArrayList<>();
  323. deletedBy = new ArrayList<>();
  324. }
  325.  
  326. public int getId() {
  327. return id;
  328. }
  329.  
  330. public void setId(int id) {
  331. this.id = id;
  332. }
  333.  
  334. public Date getDate() {
  335. return date;
  336. }
  337.  
  338. public void setDate(Date date) {
  339. this.date = date;
  340. }
  341.  
  342. public ArrayList<String> getDeletedBy() {
  343. return deletedBy;
  344. }
  345.  
  346. public void setDeletedBy(ArrayList<String> deletedBy) {
  347. this.deletedBy = deletedBy;
  348. }
  349.  
  350.  
  351.  
  352.  
  353.  
  354. public String getSubject() {
  355. return subject;
  356. }
  357.  
  358. public void setSubject(String subject) {
  359. this.subject = subject;
  360. }
  361.  
  362. public String getFrom() {
  363. return from;
  364. }
  365.  
  366. public void setFrom(String from) {
  367. this.from = from;
  368. }
  369.  
  370. public ArrayList<String> getTo() {
  371. return to;
  372. }
  373.  
  374. public void setTo(ArrayList<String> to) {
  375. this.to = to;
  376. }
  377.  
  378. public String getText() {
  379. return text;
  380. }
  381.  
  382. public void setText(String text) {
  383. this.text = text;
  384. }
  385.  
  386. public ArrayList<String> getUnreadBy() {
  387. return unreadBy;
  388. }
  389.  
  390. public void setUnreadBy(ArrayList<String> unreadBy) {
  391. this.unreadBy = unreadBy;
  392. }
  393.  
  394. public ArrayList<String> getMarkedAsSpamBy() {
  395. return markedAsSpamBy;
  396. }
  397.  
  398. public void setMarkedAsSpamBy(ArrayList<String> markedAsSpamBy) {
  399. this.markedAsSpamBy = markedAsSpamBy;
  400. }
  401.  
  402.  
  403.  
  404.  
  405. }
Add Comment
Please, Sign In to add comment