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
61c5751f
Commit
61c5751f
authored
4 years ago
by
tmp1u19
Browse files
Options
Downloads
Patches
Plain Diff
Add the handler methods into the dstore
parent
03c30ec7
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/Handler.java
+0
-129
0 additions, 129 deletions
src/Handler.java
with
0 additions
and
129 deletions
src/Handler.java
deleted
100644 → 0
+
0
−
129
View file @
03c30ec7
import
java.io.*
;
import
java.net.*
;
import
java.util.ArrayList
;
import
java.util.List
;
public
class
Handler
{
public
void
handleDstoreClientReq
(
Socket
client
,
Dstore
dstore
)
throws
IOException
{
OutputStream
out
=
client
.
getOutputStream
();
InputStream
in
=
client
.
getInputStream
();
BufferedReader
req
=
new
BufferedReader
(
new
InputStreamReader
(
in
));
PrintWriter
res
=
new
PrintWriter
(
new
OutputStreamWriter
(
out
));
String
line
;
while
((
line
=
req
.
readLine
())
!=
null
)
{
String
[]
tokens
=
line
.
split
(
" "
);
String
command
=
tokens
[
0
];
if
(
dstore
.
getFiles
().
size
()
<
Controller
.
R
)
{
res
.
println
(
"ERROR_NOT_ENOUGH_DSTORES"
);
res
.
flush
();
}
else
{
switch
(
command
)
{
case
"STORE"
:
{
try
{
res
.
println
(
"ACK"
);
res
.
flush
();
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
);
if
(
Controller
.
store
.
containsKey
(
filename
))
{
Controller
.
store
.
get
(
filename
).
add
(
dstore
);
}
else
{
List
<
Dstore
>
d
=
new
ArrayList
<>();
d
.
add
(
dstore
);
Controller
.
store
.
put
(
filename
,
d
);
}
PrintWriter
r
=
new
PrintWriter
(
new
OutputStreamWriter
(
dstore
.
getControllerSocket
().
getOutputStream
()));
r
.
println
(
"STORE_COMPLETE"
);
}
catch
(
IndexOutOfBoundsException
e
)
{
System
.
out
.
println
(
"Arguments don;t match the STORE operation"
);
}
}
case
"LOAD_DATA"
:
{
try
{
String
filename
=
tokens
[
1
];
if
(
dstore
.
getFiles
().
contains
(
filename
))
{
File
file
=
new
File
(
dstore
.
getFile_folder
()
+
"/"
+
filename
);
int
n
;
while
((
n
=
in
.
read
())!=
-
1
)
{
client
.
getOutputStream
().
write
(
n
);
}
}
else
{
client
.
close
();
}
}
catch
(
IndexOutOfBoundsException
e
)
{
System
.
out
.
println
(
"Arguments don't match in LOAD operation"
);
}
}
}
}
}
}
public
void
handleDstoreControllerReq
(
Dstore
dstore
)
throws
IOException
{
OutputStream
out
=
dstore
.
getControllerSocket
().
getOutputStream
();
InputStream
in
=
dstore
.
getControllerSocket
().
getInputStream
();
BufferedReader
req
=
new
BufferedReader
(
new
InputStreamReader
(
in
));
PrintWriter
res
=
new
PrintWriter
(
new
OutputStreamWriter
(
out
));
String
line
;
while
((
line
=
req
.
readLine
())
!=
null
)
{
String
[]
tokens
=
line
.
split
(
" "
);
String
command
=
tokens
[
0
];
if
(
dstore
.
getFiles
().
size
()
<
Controller
.
R
)
{
res
.
println
(
"ERROR_NOT_ENOUGH_DSTORES"
);
res
.
flush
();
}
else
{
switch
(
command
)
{
case
"REMOVE"
:
{
try
{
String
filename
=
tokens
[
1
];
if
(
dstore
.
getFiles
().
contains
(
filename
))
{
File
file
=
new
File
(
dstore
.
getFile_folder
()
+
"/"
+
filename
);
file
.
delete
();
Controller
.
store
.
get
(
filename
).
remove
(
dstore
);
res
.
println
(
"REMOVE_ACK"
);
res
.
flush
();
}
else
{
res
.
println
(
"ERROR_FILE_DOES_NOT_EXIST"
);
res
.
flush
();
}
}
catch
(
IndexOutOfBoundsException
e
)
{
System
.
out
.
println
(
"Arguments don't match in REMOVE opretaion"
);
}
}
}
}
}
}
}
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