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
ef10b95b
Commit
ef10b95b
authored
3 years ago
by
pr1n19
Browse files
Options
Downloads
Patches
Plain Diff
Wrapped Store and Load processes into runnable classes
parent
906fc8fa
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/DStore.java
+36
-68
36 additions, 68 deletions
src/DStore.java
src/LoadThread.java
+45
-0
45 additions, 0 deletions
src/LoadThread.java
src/StoreThread.java
+55
-0
55 additions, 0 deletions
src/StoreThread.java
with
136 additions
and
68 deletions
src/DStore.java
+
36
−
68
View file @
ef10b95b
import
java.io.*
;
import
java.net.*
;
import
java.util.HashMap
;
public
class
DStore
{
private
static
String
fileFolder
;
private
static
int
timeout
;
private
final
static
HashMap
<
String
,
String
>
fileList
=
new
HashMap
<>()
;
private
static
Pr
int
Writer
controller
;
public
static
void
main
(
String
[]
args
)
{
//Take arguments from command line
int
port
=
Integer
.
parseInt
(
args
[
0
]);
int
cPort
=
Integer
.
parseInt
(
args
[
1
]);
timeout
=
Integer
.
parse
In
t
(
args
[
2
]);
fileFolder
=
args
[
3
];
Float
timeout
=
Float
.
parse
Floa
t
(
args
[
2
]);
String
fileFolder
=
args
[
3
];
//TODO Make connection with controller & listen to send acknowledgments
try
{
//
Socket socket = new Socket(InetAddress.getLocalHost(),cPort);
//PrintWriter toC
ontroller = new PrintWriter(socket.getOutputStream());
Socket
socket
=
new
Socket
(
InetAddress
.
getLocalHost
(),
cPort
);
c
ontroller
=
new
PrintWriter
(
socket
.
getOutputStream
());
//Send "JOIN port" to controller
//New thread to listen for "LIST" on cport
//New thread to listen for "LIST"
or "REMOVE filename"
on cport
//Reply with "LIST file_list" then go back to listening
...
...
@@ -34,84 +35,51 @@ public class DStore {
ServerSocket
ss
=
new
ServerSocket
(
port
);
//Listen for connections
for
(;;
){
while
(
true
){
try
{
//Accept connection
& take inputs
//Accept connection
to new thread
Socket
client
=
ss
.
accept
();
//Make output stream returning to client
OutputStream
out
=
client
.
getOutputStream
();
//Get instruction from connection
InputStream
in
=
client
.
getInputStream
();
byte
[]
buf
=
new
byte
[
1000
];
int
bufLen
=
in
.
read
(
buf
);
String
firstBuffer
=
new
String
(
buf
,
0
,
bufLen
);
//Make output stream returning to client
OutputStream
out
=
client
.
getOutputStream
();
//Get command (text before first space)
//Get command
int
firstSpace
=
firstBuffer
.
indexOf
(
" "
);
String
command
=
firstBuffer
.
substring
(
0
,
firstSpace
);
//Get file
name
(text between first and second space)
//Get filename
int
secondSpace
=
firstBuffer
.
indexOf
(
" "
,
firstSpace
+
1
);
String
fileName
=
firstBuffer
.
substring
(
firstSpace
,
secondSpace
);
//Proceed depending on command
switch
(
command
)
{
case
"STORE"
->
{
//Get file size (text after third space)
int
thirdSpace
=
firstBuffer
.
indexOf
(
" "
,
secondSpace
+
1
);
String
fileSize
=
firstBuffer
.
substring
(
secondSpace
,
thirdSpace
);
//If command = store, load or remove
switch
(
command
)
{
case
"STORE"
->
storeFile
(
in
,
out
,
fileName
);
case
"LOAD"
->
loadFile
(
in
,
out
,
fileName
);
case
"REMOVE"
->
removeFile
(
in
,
out
,
fileName
);
//Store file in new thread and update list
new
Thread
(
new
StoreThread
(
controller
,
fileFolder
,
fileName
,
in
,
out
,
client
)).
start
();
fileList
.
put
(
fileName
,
fileSize
);
}
//Close everything after use
in
.
close
();
out
.
close
();
client
.
close
();
}
catch
(
Exception
e
){
System
.
out
.
println
(
"error "
+
e
);
case
"LOAD_DATA"
->
{
//Remove file in new thread and update list
new
Thread
(
new
LoadThread
((
fileFolder
+
fileName
),
in
,
out
,
client
)).
start
();
fileList
.
remove
(
fileName
);
}
}
}
catch
(
Exception
e
){
System
.
out
.
println
(
"error "
+
e
);
}
}
private
static
void
storeFile
(
InputStream
in
,
OutputStream
out
,
String
fileName
)
{
try
{
//Send ACK to client
out
.
write
(
"ACK"
.
getBytes
());
//Prepare new buffer for more data
byte
[]
buf
=
new
byte
[
1000
];
int
bufLen
;
//Prepare to write to file
File
outputFile
=
new
File
(
fileFolder
+
fileName
);
FileOutputStream
fileOutput
=
new
FileOutputStream
(
outputFile
);
//Wait for receipt of new data
while
((
bufLen
=
in
.
read
(
buf
))
!=
-
1
)
{
fileOutput
.
write
(
buf
,
0
,
bufLen
);
}
//Close file output
fileOutput
.
close
();
}
catch
(
Exception
e
){
System
.
out
.
println
(
"error "
+
e
);
}
}
private
static
void
loadFile
(
InputStream
in
,
OutputStream
out
,
String
fileName
)
{
}
private
static
void
removeFile
(
InputStream
in
,
OutputStream
out
,
String
fileName
)
{
}
}
This diff is collapsed.
Click to expand it.
src/LoadThread.java
0 → 100644
+
45
−
0
View file @
ef10b95b
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.InputStream
;
import
java.io.OutputStream
;
import
java.net.Socket
;
public
class
LoadThread
implements
Runnable
{
private
final
String
filePath
;
private
final
InputStream
in
;
private
final
OutputStream
out
;
private
final
Socket
client
;
public
LoadThread
(
String
f
,
InputStream
i
,
OutputStream
o
,
Socket
c
){
filePath
=
f
;
in
=
i
;
out
=
o
;
client
=
c
;
}
@Override
public
void
run
()
{
try
{
//Load file and prepare input stream
File
inputFile
=
new
File
(
filePath
);
FileInputStream
inf
=
new
FileInputStream
(
inputFile
);
//Prepare byte stream for
byte
[]
buf
=
new
byte
[
1000
];
int
bufLen
;
//Send the file out
while
((
bufLen
=
inf
.
read
(
buf
))
!=
-
1
){
out
.
write
(
buf
,
0
,
bufLen
);
}
//Close streams
in
.
close
();
out
.
close
();
client
.
close
();
}
catch
(
Exception
e
){
System
.
out
.
println
(
"error "
+
e
);
}
}
}
This diff is collapsed.
Click to expand it.
src/StoreThread.java
0 → 100644
+
55
−
0
View file @
ef10b95b
import
java.io.*
;
import
java.net.Socket
;
public
class
StoreThread
implements
Runnable
{
private
final
PrintWriter
controller
;
private
final
String
filePath
;
private
final
String
fileName
;
private
final
InputStream
in
;
private
final
OutputStream
out
;
private
final
Socket
client
;
public
StoreThread
(
PrintWriter
co
,
String
p
,
String
n
,
InputStream
i
,
OutputStream
o
,
Socket
c
)
{
controller
=
co
;
filePath
=
p
;
fileName
=
n
;
in
=
i
;
out
=
o
;
client
=
c
;
}
@Override
public
void
run
()
{
try
{
//Send ACK to client
out
.
write
(
"ACK"
.
getBytes
());
//Prepare to write to file
File
outputFile
=
new
File
(
filePath
+
fileName
);
FileOutputStream
fileOutput
=
new
FileOutputStream
(
outputFile
);
//Prepare new buffer for data from client
byte
[]
buf
=
new
byte
[
1000
];
int
bufLen
;
//Wait for receipt of new data
while
((
bufLen
=
in
.
read
(
buf
))
!=
-
1
)
{
fileOutput
.
write
(
buf
,
0
,
bufLen
);
}
//Send completion ack to controller
controller
.
write
(
"STORE_ACK "
+
fileName
);
//Close file output
fileOutput
.
close
();
//Close streams
in
.
close
();
out
.
close
();
client
.
close
();
}
catch
(
Exception
e
){
System
.
out
.
println
(
"error "
+
e
);
}
}
}
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