Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Oct 13th, 2012  |  syntax: Java  |  size: 5.11 KB  |  views: 2  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package com.jesseweiman.RPChat.manager;
  2.  
  3. import com.jesseweiman.RPChat.Message;
  4. import com.jesseweiman.RPChat.Properties;
  5. import com.jesseweiman.RPChat.Utils;
  6. import com.jesseweiman.RPChat.chat.ChatManager;
  7.  
  8. import java.io.*;
  9. import java.net.ServerSocket;
  10. import java.net.Socket;
  11. import java.net.UnknownHostException;
  12. import java.util.concurrent.ConcurrentLinkedQueue;
  13.  
  14. /**
  15.  * Created with IntelliJ IDEA.
  16.  * User: Jesse
  17.  * Date: 10/3/12
  18.  * Time: 1:23 AM
  19.  * To change this template use File | Settings | File Templates.
  20.  */
  21. public class NetworkManager {
  22.  
  23.     private static final String RPCHAT_SIGNAL = "RPCHAT";
  24.     private static final String MESSAGE_SIGNAL = "MESSAGE";
  25.     private static final String HELLO_SIGNAL = "HELLO";
  26.     private static final String BYE_SIGNAL = "BYE";
  27.  
  28.     private static final int DEFAULT_LISTEN_PORT = 1990;
  29.     private static final int DEFAULT_BUDDY_PORT = 1990;
  30.  
  31.     private ChatManager chatManager;
  32.     private ConfigManager configManager;
  33.     private Server server = new Server();
  34.     private Client client = new Client();
  35.  
  36.     private boolean buddyOnline = false;
  37.  
  38.     public NetworkManager(ChatManager chatManager, ConfigManager configManager){
  39.         this.chatManager = chatManager;
  40.         this.configManager = configManager;
  41.         server.start();
  42.         client.start();
  43.     }
  44.  
  45.     public void sendMessage(Message message){
  46.         client.offerMessage(message);
  47.     }
  48.  
  49.     private class Client extends Thread implements Runnable{
  50.         boolean sending = true;
  51.         Socket socket;
  52.         ConcurrentLinkedQueue<Message> messages = new ConcurrentLinkedQueue<Message>();
  53.  
  54.         public void stopSending(){
  55.             sending = false;
  56.         }
  57.  
  58.         public void offerMessage(Message message){
  59.             messages.offer(message);
  60.         }
  61.  
  62.         public void run(){
  63.             boolean failed;
  64.  
  65.             while(sending){
  66.                 if(messages.isEmpty()){
  67.                     Utils.sleepThread(20L);
  68.                 }
  69.                 else {
  70.                     Message message = messages.peek();
  71.  
  72.                     try {
  73.                         sendMessage(message);
  74.                         failed = false;
  75.                     }
  76.                     catch(UnknownHostException e){
  77.                         System.out.println("Unknown host! Address is: " + configManager.getString(Properties.ADDRESS_KEY, "localhost"));
  78.                         failed = true;
  79.                     }
  80.                     catch (IOException e) {
  81.                         System.out.println("There was an IO problem!");
  82.                         e.printStackTrace();
  83.                         failed = true;
  84.                     }
  85.  
  86.                     if(failed){
  87.                         Utils.sleepThread(5000);
  88.                     }
  89.                     else{
  90.                         messages.poll();
  91.                     }
  92.                 }
  93.             }
  94.  
  95.             sending = true;
  96.         }
  97.  
  98.         private void sendMessage(Message message) throws IOException {
  99.             configManager.load();
  100.             socket = new Socket(configManager.getString(Properties.ADDRESS_KEY, "localhost"), configManager.getInt(Properties.BUDDY_PORT_KEY, DEFAULT_BUDDY_PORT));
  101.             Utils.log("Address to send: " + socket.getInetAddress().getHostName() + "; Port: " + socket.getPort());
  102.  
  103.             PrintWriter streamWriter = new PrintWriter(socket.getOutputStream()) ;
  104.             BufferedWriter writer = new BufferedWriter(streamWriter);
  105.  
  106.             writer.write(RPCHAT_SIGNAL); writer.newLine();
  107.             Message.send(writer, message);
  108.             writer.close();
  109.         }
  110.     }
  111.  
  112.     private class Server extends Thread implements Runnable {
  113.         ServerSocket socket;
  114.         boolean listen = true;
  115.  
  116.         public void stopListening(){
  117.             listen = false;
  118.         }
  119.  
  120.         public void run(){
  121.             try{
  122.                 socket = new ServerSocket(configManager.getInt(Properties.LISTEN_PORT_KEY, DEFAULT_LISTEN_PORT));
  123.             }
  124.             catch(IOException e){
  125.                 Utils.log("There was a problem starting the server!");
  126.                 e.printStackTrace();
  127.                 System.exit(1);
  128.             }
  129.             try{
  130.                 configManager.load();
  131.                 while(listen){
  132.                     Socket buddy = socket.accept();
  133.  
  134.                     InputStreamReader streamReader = new InputStreamReader(buddy.getInputStream());
  135.                     BufferedReader reader = new BufferedReader(streamReader);
  136.  
  137.  
  138.                     String line = reader.readLine();
  139.  
  140.                     if(line.equals(RPCHAT_SIGNAL)){
  141.                         Message message = Message.receive(reader);
  142.                         chatManager.receiveMessage(message);
  143.                     }
  144.                     else{
  145.                         reader.close();
  146.                     }
  147.                 }
  148.             }
  149.             catch(IOException e){
  150.                 Utils.log("There was a problem recieving information!");
  151.                 e.printStackTrace();
  152.             }
  153.  
  154.             listen = true;
  155.         }
  156.     }
  157.  
  158.  
  159. }