Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
D
Distributed File System
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
tmp1u19
Distributed File System
Commits
93d1175c
Commit
93d1175c
authored
4 years ago
by
tmp1u19
Browse files
Options
Downloads
Patches
Plain Diff
Add REALOD operation and other error handling statements
parent
ff088abf
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/Dstore.java
+44
-25
44 additions, 25 deletions
src/Dstore.java
src/Server.java
+60
-5
60 additions, 5 deletions
src/Server.java
with
104 additions
and
30 deletions
src/Dstore.java
+
44
−
25
View file @
93d1175c
...
...
@@ -102,15 +102,26 @@ public class Dstore {
String
filename
=
tokens
[
1
];
int
filesize
=
Integer
.
parseInt
(
tokens
[
2
]);
// read data and store the file
byte
[]
data
=
new
byte
[
filesize
];
client
.
getInputStream
().
readNBytes
(
data
,
0
,
filesize
);
FileOutputStream
o
=
new
FileOutputStream
(
dstore
.
getFile_folder
()
+
"/"
+
filename
);
o
.
write
(
data
);
o
.
flush
();
o
.
close
();
dstore
.
getFiles
().
add
(
filename
);
long
limit
=
System
.
currentTimeMillis
()
+
dstore
.
getTimeout
();
boolean
finished
=
false
;
while
(!
finished
&&
System
.
currentTimeMillis
()
<
limit
)
{
// read data and store the file
byte
[]
data
=
new
byte
[
filesize
];
client
.
getInputStream
().
readNBytes
(
data
,
0
,
filesize
);
FileOutputStream
o
=
new
FileOutputStream
(
dstore
.
getFile_folder
()
+
"/"
+
filename
);
o
.
write
(
data
);
o
.
flush
();
o
.
close
();
dstore
.
getFiles
().
add
(
filename
);
finished
=
true
;
}
if
(!
finished
)
{
System
.
out
.
println
(
"ERROR: time expired"
);
return
;
}
PrintWriter
r
=
new
PrintWriter
(
new
OutputStreamWriter
(
controllerSocket
.
getOutputStream
()));
r
.
println
(
"STORE_ACK "
+
filename
+
" "
+
dstore
.
getPort
());
...
...
@@ -120,27 +131,35 @@ public class Dstore {
System
.
out
.
println
(
"Arguments don;t match the STORE operation"
);
}
break
;
case
"LOAD_DATA"
:
try
{
if
(
tokens
.
length
!=
2
)
{
System
.
out
.
println
(
"Arguments don't match in LOAD operation"
);
}
else
{
String
filename
=
tokens
[
1
];
if
(
dstore
.
getFiles
().
contains
(
filename
))
{
File
file
=
new
File
(
dstore
.
getFile_folder
()
+
"/"
+
filename
);
FileInputStream
i
=
new
FileInputStream
(
file
);
int
n
;
while
((
n
=
i
.
read
())
!=
-
1
)
{
client
.
getOutputStream
().
write
(
n
);
try
{
if
(
dstore
.
getFiles
().
contains
(
filename
))
{
File
file
=
new
File
(
dstore
.
getFile_folder
()
+
"/"
+
filename
);
FileInputStream
i
=
new
FileInputStream
(
file
);
int
n
;
while
((
n
=
i
.
read
())
!=
-
1
)
{
client
.
getOutputStream
().
write
(
n
);
}
i
.
close
();
}
else
{
done
=
true
;
client
.
close
();
}
i
.
close
()
;
}
else
{
done
=
true
;
client
.
close
();
Server
.
pos
=
0
;
}
catch
(
Exception
ingored
)
{
res
.
println
(
"RELOAD "
+
filename
)
;
res
.
flush
();
}
}
catch
(
IndexOutOfBoundsException
e
)
{
System
.
out
.
println
(
"Arguments don't match in LOAD operation"
);
}
break
;
default
:
System
.
out
.
println
(
"Unknown command"
);
...
...
@@ -180,7 +199,7 @@ public class Dstore {
dstore
.
getFiles
().
remove
(
filename
);
res
.
println
(
"REMOVE_ACK "
+
filename
);
}
else
{
res
.
println
(
"ERROR_FILE_DOES_NOT_EXIST
"
);
res
.
println
(
"ERROR_FILE_DOES_NOT_EXIST
"
+
filename
);
}
res
.
flush
();
...
...
This diff is collapsed.
Click to expand it.
src/Server.java
+
60
−
5
View file @
93d1175c
import
java.io.*
;
import
java.net.*
;
import
java.nio.CharBuffer
;
import
java.util.*
;
public
class
Server
{
private
final
Controller
controller
;
public
static
FileSystem
fileSystem
;
public
static
int
pos
;
public
Server
(
Controller
controller
,
FileSystem
fileSystem
)
{
this
.
controller
=
controller
;
...
...
@@ -15,6 +17,7 @@ public class Server {
public
void
handleClient
(
Socket
client
)
throws
IOException
{
OutputStream
out
=
client
.
getOutputStream
();
InputStream
in
=
client
.
getInputStream
();
pos
=
0
;
BufferedReader
req
=
new
BufferedReader
(
new
InputStreamReader
(
in
));
PrintWriter
res
=
new
PrintWriter
(
new
OutputStreamWriter
(
out
));
...
...
@@ -41,21 +44,27 @@ public class Server {
}
else
if
(
command
.
equals
(
"REMOVE_ACK"
)){
handleRemoveACK
(
tokens
);
}
else
{
System
.
out
.
println
(
"Unkown command"
);
System
.
out
.
println
(
"Unk
n
own command"
);
}
}
else
{
switch
(
command
)
{
case
"STORE"
->
handleStore
(
client
,
tokens
);
case
"LOAD"
->
handleLoad
(
client
,
tokens
);
case
"RELOAD"
->
handleReload
(
client
,
tokens
);
case
"REMOVE"
->
handleRemove
(
client
,
tokens
);
case
"LIST"
->
handleList
(
client
);
default
->
System
.
out
.
println
(
"
Command unknown
"
);
default
->
System
.
out
.
println
(
"
Unknown command
"
);
}
}
}
}
}
/**
* @param client
* @param tokens gets the filename and the filesize
* @throws IOException
*/
private
void
handleStore
(
Socket
client
,
String
[]
tokens
)
throws
IOException
{
try
{
...
...
@@ -63,7 +72,16 @@ public class Server {
int
filesize
=
Integer
.
parseInt
(
tokens
[
2
]);
PrintWriter
res
=
new
PrintWriter
(
new
OutputStreamWriter
(
client
.
getOutputStream
()));
if
(
fileSystem
.
getStore
().
containsKey
(
filename
))
{
res
.
println
(
"ERROR_FILE_ALREADY_EXISTS"
);
res
.
flush
();
return
;
}
// update index to store in progress
fileSystem
.
addIndex
(
filename
,
"store in progress"
);
// select R Dstores and create a string of all of their endpoints
String
msg
=
""
;
int
i
=
0
;
List
<
FSStore
>
temp
=
new
ArrayList
<>();
...
...
@@ -73,13 +91,18 @@ public class Server {
break
;
}
// in case all the ACK are received keep in memory all the dstores
temp
.
add
(
fileSystem
.
getDstores
().
get
(
port
));
msg
=
fileSystem
.
getDstores
().
get
(
port
).
getPublicPort
()
+
msg
+
" "
;
// construct the message
msg
=
fileSystem
.
getDstores
().
get
(
port
).
getPublicPort
()
+
" "
+
msg
;
i
++;
}
res
.
println
(
"STORE_TO "
+
msg
);
res
.
flush
();
// check if all the dstores have sent an ACK and send appropriate messages
boolean
done
=
false
;
long
limit
=
System
.
currentTimeMillis
()
+
controller
.
getTimeout
();
...
...
@@ -88,12 +111,15 @@ public class Server {
done
=
true
;
fileSystem
.
addIndex
(
filename
,
"store complete"
);
fileSystem
.
addStore
(
filename
,
temp
);
fileSystem
.
addFileSize
(
filename
,
filesize
);
res
.
println
(
"STORE_COMPLETE"
);
res
.
flush
();
}
}
// if the dstores didn't send a STORE_ACK in the timeout => store failed
if
(!
done
)
{
fileSystem
.
removeIndex
(
filename
);
System
.
out
.
println
(
filename
+
" failed to upload"
);
}
...
...
@@ -107,12 +133,41 @@ public class Server {
try
{
String
filename
=
tokens
[
1
];
PrintWriter
res
=
new
PrintWriter
(
new
OutputStreamWriter
(
client
.
getOutputStream
()));
if
(!
fileSystem
.
getStore
().
containsKey
(
filename
))
{
res
.
println
(
"ERROR_FILE_DOES_NOT_EXIST"
);
}
else
{
// select a Dstore from there and give an appropriate error if all Dstores fail
res
.
println
(
"LOAD_FROM "
+
fileSystem
.
getStore
().
get
(
filename
).
get
(
pos
).
getPublicPort
()
+
" "
+
fileSystem
.
getFileSizes
().
get
(
filename
));
}
res
.
flush
();
}
catch
(
IndexOutOfBoundsException
e
)
{
System
.
out
.
println
(
"Arguments don't match in LOAD operation"
);
}
}
private
void
handleReload
(
Socket
client
,
String
[]
tokens
)
throws
IOException
{
pos
=
pos
+
1
;
PrintWriter
res
=
new
PrintWriter
(
new
OutputStreamWriter
(
client
.
getOutputStream
()));
try
{
String
filename
=
tokens
[
1
];
if
(!
fileSystem
.
getStore
().
containsKey
(
filename
))
{
pos
=
0
;
res
.
println
(
"ERROR_FILE_DOES_NOT_EXIST"
);
}
else
{
if
(
pos
==
controller
.
getR
())
{
pos
=
0
;
res
.
println
(
"ERROR_LOAD"
);
res
.
flush
();
return
;
}
// select a Dstore from there and give an appropriate error if all Dstores fail
res
.
println
(
"LOAD_FROM "
+
fileSystem
.
getStore
().
get
(
filename
).
get
(
0
).
getPublicPort
());
res
.
println
(
"LOAD_FROM "
+
fileSystem
.
getStore
().
get
(
filename
).
get
(
pos
).
getPublicPort
()
+
" "
+
fileSystem
.
getFileSizes
().
get
(
filename
));
}
res
.
flush
();
...
...
@@ -130,7 +185,6 @@ public class Server {
System
.
out
.
println
(
fileSystem
.
index
.
get
(
filename
));
if
(!
fileSystem
.
store
.
containsKey
(
filename
))
{
System
.
out
.
println
(
"Entered in Controller request"
);
PrintWriter
res
=
new
PrintWriter
(
new
OutputStreamWriter
(
client
.
getOutputStream
()));
res
.
println
(
"ERROR_FILE_DOES_NOT_EXIST"
);
res
.
flush
();
...
...
@@ -153,6 +207,7 @@ public class Server {
done
=
true
;
fileSystem
.
addIndex
(
filename
,
"remove complete"
);
fileSystem
.
getStore
().
remove
(
filename
);
fileSystem
.
getFileSizes
().
remove
(
filename
);
res
.
println
(
"REMOVE_COMPLETE"
);
res
.
flush
();
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment