-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShowSection.java
More file actions
81 lines (66 loc) · 2.33 KB
/
Copy pathShowSection.java
File metadata and controls
81 lines (66 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package turing;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import turing.Message.OPERATION;
import turing.Message.RESULT;
public class ShowSection implements Runnable {
SocketChannel client;
Message received;
public ShowSection (SocketChannel client, Message received){
if (client == null || received == null)
return;
this.client = client;
this.received = received;
}
public void run() {
String clientUsername = received.getUsername();
String documentName = received.getDocumentName();
int numeroSezione = received.getNumOfSection();
FileChannel fileChannel = null;
Section section = null;
RESULT result;
Message toSend = new Message();
System.out.println("Documento: " + documentName);
System.out.println("Sezione: "+ numeroSezione);
try {
result = Turing.databaseUsers.haveDocument(clientUsername,documentName);
if (result == RESULT.OP_OK)
{
if ( (section = Turing.databaseDocuments.getDocument(documentName).getSection(numeroSezione)) != null)
{
String path = Turing.DATA_DIR; //+ Turing.databaseDocuments.getDocument(documentName).getOwner() +"/";
path = path + section.getFileName();
File file = new File(path);
System.out.println(path);
if (!file.exists()) file.createNewFile();
fileChannel = FileChannel.open(Paths.get(Turing.DATA_DIR,section.getFileName()),StandardOpenOption.READ);
if (fileChannel == null)
result = RESULT.OP_ERR;
} else result = RESULT.SEC_INEXISTENT;
}
toSend.setMessageResult(OPERATION.SHOW_S,result);
Message.sendMessage(client, toSend);
System.out.println("Server-ShowSection ["+ received.getUsername()+", document: " +
received.getDocumentName()+" section:" +
received.getNumOfSection()+" ]:" +result);
if (result == RESULT.OP_OK) {
ByteBuffer buffer = Message.readFileChannel(fileChannel);
Message.send(client, buffer);
}
client.close();
} catch (Exception e){
toSend.resetMessage();
toSend.setMessageResult(OPERATION.SHOW_S,RESULT.OP_ERR);
Message.sendMessage(client, toSend);//invio l'esito
try {
client.close();
} catch (IOException e1) {
}
}
}
}