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
de9967ca
Commit
de9967ca
authored
4 years ago
by
tmp1u19
Browse files
Options
Downloads
Patches
Plain Diff
Recognise some messages from client
parent
bbe2edae
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/ClientMain.java
+0
-123
0 additions, 123 deletions
src/ClientMain.java
src/Controller.java
+68
-8
68 additions, 8 deletions
src/Controller.java
with
68 additions
and
131 deletions
src/ClientMain.java
deleted
100644 → 0
+
0
−
123
View file @
bbe2edae
import
java.io.File
;
import
java.io.IOException
;
import
java.util.Random
;
public
class
ClientMain
{
public
static
void
main
(
String
[]
args
)
throws
Exception
{
final
int
cport
=
Integer
.
parseInt
(
args
[
0
]);
int
timeout
=
Integer
.
parseInt
(
args
[
1
]);
File
downloadFolder
=
new
File
(
"downloads"
);
if
(!
downloadFolder
.
exists
())
if
(!
downloadFolder
.
mkdir
())
throw
new
RuntimeException
(
"Cannot create download folder (folder absolute path: "
+
downloadFolder
.
getAbsolutePath
()
+
")"
);
File
uploadFolder
=
new
File
(
"to_store"
);
if
(!
uploadFolder
.
exists
())
throw
new
RuntimeException
(
"to_store folder does not exist"
);
// testClient(cport, timeout, downloadFolder);
// example to launch a number of concurrent clients, each doing the same operations
for
(
int
i
=
0
;
i
<
10
;
i
++)
{
new
Thread
()
{
public
void
run
()
{
test2Client
(
cport
,
timeout
,
downloadFolder
,
uploadFolder
);
}
}.
start
();
}
}
public
static
void
test2Client
(
int
cport
,
int
timeout
,
File
downloadFolder
,
File
uploadFolder
)
{
Client
client
=
null
;
try
{
client
=
new
Client
(
cport
,
timeout
,
Logger
.
LoggingType
.
ON_FILE_AND_TERMINAL
);
client
.
connect
();
Random
random
=
new
Random
(
System
.
currentTimeMillis
()
*
System
.
nanoTime
());
File
fileList
[]
=
uploadFolder
.
listFiles
();
for
(
int
i
=
0
;
i
<
fileList
.
length
/
2
;
i
++)
{
File
fileToStore
=
fileList
[
random
.
nextInt
(
fileList
.
length
)];
try
{
client
.
store
(
fileToStore
);
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
"Error storing file "
+
fileToStore
);
e
.
printStackTrace
();
}
}
String
list
[]
=
null
;
try
{
list
=
list
(
client
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
for
(
int
i
=
0
;
i
<
list
.
length
/
4
;
i
++)
{
String
fileToRemove
=
list
[
random
.
nextInt
(
list
.
length
)];
try
{
client
.
remove
(
fileToRemove
);
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
"Error remove file "
+
fileToRemove
);
e
.
printStackTrace
();
}
}
try
{
list
=
list
(
client
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
finally
{
if
(
client
!=
null
)
try
{
client
.
disconnect
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
}
public
static
void
testClient
(
int
cport
,
int
timeout
,
File
downloadFolder
)
{
Client
client
=
null
;
try
{
client
=
new
Client
(
cport
,
timeout
,
Logger
.
LoggingType
.
ON_FILE_AND_TERMINAL
);
try
{
client
.
connect
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
return
;
}
try
{
list
(
client
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
try
{
client
.
store
(
new
File
(
"Clipboard01.pdf"
));
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
try
{
client
.
store
(
new
File
(
"Clipboard01.pdf"
));
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
try
{
client
.
store
(
new
File
(
"Clipboard01.jpg"
));
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
String
list
[]
=
null
;
try
{
list
=
list
(
client
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
if
(
list
!=
null
)
for
(
String
filename
:
list
)
try
{
client
.
load
(
filename
,
downloadFolder
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
/*if (list != null)
for (String filename : list)
try { client.remove(filename); } catch(IOException e) { e.printStackTrace(); }
try { client.remove(list[0]); } catch(IOException e) { e.printStackTrace(); }
try { list(client); } catch(IOException e) { e.printStackTrace(); }*/
}
finally
{
if
(
client
!=
null
)
try
{
client
.
disconnect
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
}
public
static
String
[]
list
(
Client
client
)
throws
IOException
,
NotEnoughDstoresException
{
System
.
out
.
println
(
"Retrieving list of files..."
);
String
list
[]
=
client
.
list
();
System
.
out
.
println
(
"Ok, "
+
list
.
length
+
" files:"
);
int
i
=
0
;
for
(
String
filename
:
list
)
System
.
out
.
println
(
"["
+
i
++
+
"] "
+
filename
);
return
list
;
}
}
This diff is collapsed.
Click to expand it.
src/Controller.java
+
68
−
8
View file @
de9967ca
...
@@ -10,6 +10,8 @@ public class Controller {
...
@@ -10,6 +10,8 @@ public class Controller {
// waits for Dstores to join the datastore (rebalance operation)
// waits for Dstores to join the datastore (rebalance operation)
// it doesn't serve any client request until at least R Dstores have joined the system
// it doesn't serve any client request until at least R Dstores have joined the system
File
file
=
new
File
(
"output.txt"
);
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
// port to listen on
// port to listen on
...
@@ -35,13 +37,17 @@ public class Controller {
...
@@ -35,13 +37,17 @@ public class Controller {
ports
.
add
(
client
);
ports
.
add
(
client
);
R
=
R
-
1
;
R
=
R
-
1
;
BufferedReader
in
=
new
BufferedReader
(
Thread
t
=
new
Thread
()
{
new
InputStreamReader
(
client
.
getInputStream
()));
@Override
String
line
;
public
void
run
()
{
while
((
line
=
in
.
readLine
())
!=
null
)
try
{
System
.
out
.
println
(
line
+
" received"
);
handleDstores
(
client
);
client
.
close
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
};
t
.
start
();
}
else
{
}
else
{
break
;
break
;
}
}
...
@@ -54,7 +60,7 @@ public class Controller {
...
@@ -54,7 +60,7 @@ public class Controller {
/*
/*
#TODO
#TODO
- use threads to connect multiple clients to a server
- use threads to connect multiple clients
(Dstore and Client)
to a server
*/
*/
}
catch
(
IOException
e
)
{
}
catch
(
IOException
e
)
{
...
@@ -62,4 +68,58 @@ public class Controller {
...
@@ -62,4 +68,58 @@ public class Controller {
}
}
}
}
private
static
void
handleDstores
(
Socket
clientSocket
)
throws
IOException
,
InterruptedException
{
OutputStream
outputStream
=
clientSocket
.
getOutputStream
();
InputStream
inputStream
=
clientSocket
.
getInputStream
();
BufferedReader
reader
=
new
BufferedReader
(
new
InputStreamReader
(
inputStream
));
String
line
;
while
((
line
=
reader
.
readLine
())
!=
null
)
{
String
[]
tokens
=
line
.
split
(
" "
);
String
cmd
=
tokens
[
0
];
if
(
cmd
.
equals
(
"ACK"
))
{
}
else
if
(
cmd
.
equals
(
"STORE_ACK"
))
{
}
else
if
(
cmd
.
equals
(
"REMOVE_ACK"
))
{
}
else
if
(
cmd
.
equals
(
"QUIT"
))
{
clientSocket
.
close
();
break
;
}
else
{
String
msg
=
"unknown command "
+
line
+
"\n"
;
outputStream
.
write
(
msg
.
getBytes
());
}
String
msg
=
"You typed "
+
line
+
"\n"
;
outputStream
.
write
(
msg
.
getBytes
());
}
if
(!
clientSocket
.
isClosed
())
{
clientSocket
.
close
();
}
}
private
void
createLogFile
()
{
try
{
if
(
file
.
createNewFile
())
{
System
.
out
.
println
(
"File created"
);
}
else
{
System
.
out
.
println
(
"File already exists"
);
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
private
void
log
(
String
message
)
{
try
{
FileWriter
writer
=
new
FileWriter
(
file
);
writer
.
write
(
message
+
" at "
+
System
.
currentTimeMillis
()
+
"\n"
);
writer
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
}
\ No newline at end of file
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