· 9 years ago · Nov 29, 2016, 03:04 PM
1package server;
2
3import java.io.BufferedInputStream;
4import java.io.FileInputStream;
5import java.io.FileNotFoundException;
6import java.io.IOException;
7import java.rmi.RemoteException;
8import java.rmi.server.UnicastRemoteObject;
9import java.security.NoSuchAlgorithmException;
10import java.util.concurrent.ConcurrentHashMap;
11
12import javax.crypto.Cipher;
13import javax.crypto.KeyGenerator;
14import javax.crypto.NoSuchPaddingException;
15import javax.crypto.SecretKey;
16
17import client.ClientService;
18import model.SongList;
19
20public class ServerServent extends UnicastRemoteObject implements ServerService{
21
22 private static final long serialVersionUID = -8370393857119013394L;
23 private static final String serverFilePath = "src/resources/files";
24 private SongList list = new SongList();
25 private static ConcurrentHashMap<ClientService,Producer> map = new ConcurrentHashMap<ClientService,Producer>();
26
27 public ServerServent() throws RemoteException {
28 super();
29 list.populateListByPath(serverFilePath);
30 }
31 /**
32 * Method invoked by Client.
33 * Client is sent @SongList of songs that are locally stored on the server
34 * The Client reference and its Producer Thread are put into a @ConcurrentHashMap
35 */
36 public SongList getSongList() throws RemoteException{
37 return list;
38 }
39 /**
40 * Method invoked by Client.
41 * The Client reference and its Producer Thread are put into a @ConcurrentHashMap
42 */
43 public void selectSong(ClientService client,String filePath) throws RemoteException {
44 map.put(client,new Producer(client,filePath));
45 }
46
47 /**
48 * Method invoked by Client.
49 * This starts clients Producer thread.
50 */
51 public void startDownload(ClientService client) throws RemoteException{
52 map.get(client).start();
53 }
54
55 /**
56 * Method invoked by Client.
57 * Depending on the client that evoked it,
58 * Server sends a 8kb byte[] to the Client
59 */
60 public byte[] downloadSong(ClientService client) throws RemoteException{
61 try{
62 return map.get(client).downloadSong();
63 }catch(NullPointerException ex){
64 System.out.println("SERVER: Sending empty packet");
65 }
66 return null;
67
68 }
69 /**
70 * Method invoked by Client
71 * Depending on the client that evoked it,
72 * Tells the Producer thread of that client to start reading in the next 8kb byte[] chunk
73 */
74 public void sendNext(ClientService client){
75 map.get(client).sendNext();
76 }
77 /**
78 * Producer extends @Thread
79 * Used to read a file into a @BufferedInputStream and send it in 8kb byte[] chunks.
80 */
81 class Producer extends Thread {
82
83 private BufferedInputStream inputStream;
84 private byte[] buffer;
85 private volatile boolean sendNext;
86 private ClientService client;
87
88 private SecretKey key;
89
90 public Producer(ClientService client,String filePath){
91 this.client = client;
92
93 try {
94 inputStream = new BufferedInputStream(new FileInputStream(filePath));
95 } catch (FileNotFoundException e) {
96 System.out.println("SERVER: File does not exist :" + filePath);
97 }
98 }
99
100 private synchronized void genereKey(){
101
102 }
103 public synchronized void producer()throws IOException{
104 buffer = new byte[1024 * 8];
105 int count = 0;
106 while(count >= 0){
107
108 while(sendNext == true){
109 count = inputStream.read(buffer, 0, buffer.length);
110 sendNext = false;
111 client.isDataSent();
112 }
113 try{
114 Thread.sleep(50);}
115 catch(InterruptedException e){
116 System.out.println("SERVER: Thread shutting down...");
117 }
118 }
119 client.isFileEnd();
120
121 }
122 /**
123 * Method invoked by the Server
124 * Sends a chunk of bytes to the Client
125 */
126 public byte[] downloadSong() throws RemoteException{
127 return buffer;
128 }
129
130 /**
131 * Method invoked by the Server
132 * Tells server that the previous byte chunk has been successfully received
133 */
134 public void sendNext(){
135 this.sendNext = true;
136 }
137
138 public void run() {
139 try {
140 producer();
141
142 } catch (IOException e) {
143 e.printStackTrace();
144 Thread.currentThread().interrupt();
145 }
146 }
147 }
148
149 public void removeClient(ClientService clientServent) throws RemoteException {
150 map.get(clientServent).interrupt();
151 map.remove(clientServent);
152 System.out.println("SERVER: Client Removed");
153 }
154
155 public Cipher getCypherObject(int mode) throws RemoteException, NoSuchAlgorithmException, NoSuchPaddingException {
156 Cipher cipher = Cipher.getInstance("DES");
157 if(mode == 0)
158 cipher.init(Cipher.ENCRYPT_MODE, certificate);
159 else if(mode == 1)
160 return cipher;
161 }
162}