Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
D
DStore
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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
pr1n19
DStore
Commits
2bf9e310
Commit
2bf9e310
authored
3 years ago
by
pr1n19
Browse files
Options
Downloads
Patches
Plain Diff
Added functionality for controller commands
parent
7d3bc914
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/DStore.java
+31
-16
31 additions, 16 deletions
src/DStore.java
src/StoreThread.java
+6
-9
6 additions, 9 deletions
src/StoreThread.java
with
37 additions
and
25 deletions
src/DStore.java
+
31
−
16
View file @
2bf9e310
import
java.io.*
;
import
java.io.*
;
import
java.net.*
;
import
java.net.*
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.Hash
Map
;
import
java.util.Hash
Set
;
import
java.util.
List
;
import
java.util.
Iterator
;
public
class
DStore
{
public
class
DStore
{
private
final
static
Hash
Map
<
String
,
String
>
fileList
=
new
Hash
Map
<>();
private
final
static
Hash
Set
<
String
>
fileList
=
new
Hash
Set
<>();
private
static
PrintWriter
controller
;
private
static
PrintWriter
controller
;
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
...
@@ -16,16 +16,31 @@ public class DStore {
...
@@ -16,16 +16,31 @@ public class DStore {
Float
timeout
=
Float
.
parseFloat
(
args
[
2
]);
Float
timeout
=
Float
.
parseFloat
(
args
[
2
]);
String
fileFolder
=
args
[
3
];
String
fileFolder
=
args
[
3
];
//
TODO
Make connection with controller & listen to send acknowledgments
//Make connection with controller & listen to send acknowledgments
try
{
try
{
Socket
socket
=
new
Socket
(
InetAddress
.
getLocalHost
(),
cPort
);
Socket
socket
=
new
Socket
(
InetAddress
.
getLocalHost
(),
cPort
);
controller
=
new
PrintWriter
(
socket
.
getOutputStream
());
controller
=
new
PrintWriter
(
socket
.
getOutputStream
());
//Send "JOIN port" to controller
//Send "JOIN port" to controller
//New thread to listen for "LIST" or "REMOVE filename" on cport
controller
.
write
(
"JOIN "
+
port
);
//Reply with "LIST file_list" then go back to listening
//New thread to listen for "LIST" & "REMOVE FILENAME"
new
Thread
(()
->
{
try
{
BufferedReader
contIn
=
new
BufferedReader
(
new
InputStreamReader
(
socket
.
getInputStream
()));
while
(
true
){
String
received
=
contIn
.
readLine
();
String
command
=
received
.
split
(
" "
)[
0
];
switch
(
command
)
{
case
"LIST"
->
listToController
();
case
"REMOVE"
->
removeFile
(
fileFolder
,(
received
.
split
(
" "
)[
1
]));
}
}
}
catch
(
Exception
e
){
System
.
out
.
println
(
"error "
+
e
);
}
}).
start
();
}
catch
(
Exception
e
){
}
catch
(
Exception
e
){
System
.
out
.
println
(
"error "
+
e
);
System
.
out
.
println
(
"error "
+
e
);
...
@@ -64,11 +79,11 @@ public class DStore {
...
@@ -64,11 +79,11 @@ public class DStore {
case
"STORE"
->
{
case
"STORE"
->
{
//Get file size (text after third space)
//Get file size (text after third space)
int
thirdSpace
=
firstBuffer
.
indexOf
(
" "
,
secondSpace
+
1
);
int
thirdSpace
=
firstBuffer
.
indexOf
(
" "
,
secondSpace
+
1
);
Str
in
g
fileSize
=
firstBuffer
.
substring
(
secondSpace
,
thirdSpace
);
in
t
fileSize
=
Integer
.
parseInt
(
firstBuffer
.
substring
(
secondSpace
,
thirdSpace
)
)
;
//Store file in new thread and update list
//Store file in new thread and update list
new
Thread
(
new
StoreThread
(
controller
,
fileFolder
,
fileName
,
in
,
out
,
client
)).
start
();
new
Thread
(
new
StoreThread
(
controller
,
fileFolder
,
fileName
,
in
,
out
,
client
,
fileSize
)).
start
();
fileList
.
put
(
fileName
,
fileSize
);
fileList
.
add
(
fileName
);
}
}
case
"LOAD_DATA"
->
//Load file in new thread
case
"LOAD_DATA"
->
//Load file in new thread
new
Thread
(
new
LoadThread
((
fileFolder
+
fileName
),
in
,
out
,
client
)).
start
();
new
Thread
(
new
LoadThread
((
fileFolder
+
fileName
),
in
,
out
,
client
)).
start
();
...
@@ -82,19 +97,19 @@ public class DStore {
...
@@ -82,19 +97,19 @@ public class DStore {
}
}
}
}
private
void
listToController
()
{
private
static
void
listToController
()
{
//Compile list as string
//Compile list as string
StringBuilder
stringList
=
new
StringBuilder
();
StringBuilder
stringList
=
new
StringBuilder
();
ArrayList
<
String
>
keys
=
new
ArrayList
<>(
fileList
.
keySet
());
for
(
String
file
:
keys
)
{
for
(
String
s
:
fileList
)
{
stringList
.
append
(
file
).
append
(
" "
).
append
(
fileList
.
get
(
file
)
).
append
(
"
;
"
);
stringList
.
append
(
s
).
append
(
"
"
);
}
}
//Return value
//Return value
controller
.
write
(
stringList
.
toString
());
controller
.
write
(
stringList
.
toString
());
}
}
private
void
removeFile
(
String
filePath
,
String
fileName
){
private
static
void
removeFile
(
String
filePath
,
String
fileName
){
//Remove file from folder
//Remove file from folder
File
file
=
new
File
(
filePath
+
fileName
);
File
file
=
new
File
(
filePath
+
fileName
);
file
.
delete
();
file
.
delete
();
...
...
This diff is collapsed.
Click to expand it.
src/StoreThread.java
+
6
−
9
View file @
2bf9e310
...
@@ -9,14 +9,16 @@ public class StoreThread implements Runnable{
...
@@ -9,14 +9,16 @@ public class StoreThread implements Runnable{
private
final
InputStream
in
;
private
final
InputStream
in
;
private
final
OutputStream
out
;
private
final
OutputStream
out
;
private
final
Socket
client
;
private
final
Socket
client
;
private
final
int
fileSize
;
public
StoreThread
(
PrintWriter
co
,
String
p
,
String
n
,
InputStream
i
,
OutputStream
o
,
Socket
c
)
{
public
StoreThread
(
PrintWriter
co
,
String
p
,
String
n
,
InputStream
i
,
OutputStream
o
,
Socket
c
,
int
fs
)
{
controller
=
co
;
controller
=
co
;
filePath
=
p
;
filePath
=
p
;
fileName
=
n
;
fileName
=
n
;
in
=
i
;
in
=
i
;
out
=
o
;
out
=
o
;
client
=
c
;
client
=
c
;
fileSize
=
fs
;
}
}
@Override
@Override
...
@@ -29,14 +31,9 @@ public class StoreThread implements Runnable{
...
@@ -29,14 +31,9 @@ public class StoreThread implements Runnable{
File
outputFile
=
new
File
(
filePath
+
fileName
);
File
outputFile
=
new
File
(
filePath
+
fileName
);
FileOutputStream
fileOutput
=
new
FileOutputStream
(
outputFile
);
FileOutputStream
fileOutput
=
new
FileOutputStream
(
outputFile
);
//Prepare new buffer for data from client
//Read correct bytes from input stream and write to file
byte
[]
buf
=
new
byte
[
1000
];
byte
[]
buf
=
in
.
readNBytes
(
fileSize
);
int
bufLen
;
fileOutput
.
write
(
buf
);
//Wait for receipt of new data
while
((
bufLen
=
in
.
read
(
buf
))
!=
-
1
)
{
fileOutput
.
write
(
buf
,
0
,
bufLen
);
}
//Send completion ack to controller
//Send completion ack to controller
controller
.
write
(
"STORE_ACK "
+
fileName
);
controller
.
write
(
"STORE_ACK "
+
fileName
);
...
...
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