Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Jonathan Sudibya
IF3230-Tugas-Besar-Sister-2015
Commits
97f096dc
Commit
97f096dc
authored
Apr 23, 2015
by
Jonathan Sudibya
Browse files
tracker final
parent
a48460aa
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/server/TCPServer/src/Controller/ConnectionHandler.java
View file @
97f096dc
...
...
@@ -33,13 +33,15 @@ public class ConnectionHandler implements Runnable {
private
Vector
<
Client
>
clients
;
private
boolean
active
;
private
int
globalID
;
private
int
serverSocket
;
public
ConnectionHandler
(){
threadName
=
"Connection-Handler"
;
active
=
true
;
globalID
=
0
;
serverSocket
=
6789
;
try
{
welcomeSocket
=
new
ServerSocket
(
6789
);
welcomeSocket
=
new
ServerSocket
(
serverSocket
);
clients
=
new
Vector
<>();
}
catch
(
IOException
ex
)
{
Logger
.
getLogger
(
ConnectionHandler
.
class
.
getName
()).
log
(
Level
.
SEVERE
,
null
,
ex
);
...
...
@@ -51,17 +53,22 @@ public class ConnectionHandler implements Runnable {
public
void
run
()
{
System
.
out
.
println
(
"Running thread "
+
threadName
);
System
.
out
.
println
(
"Connecting to tracker server..."
);
connectTracker
();
System
.
out
.
println
(
"Connection to tracker server successfull... Ready to recieve connection!"
);
while
(
active
){
try
{
clients
.
add
(
new
Client
(
globalID
,
welcomeSocket
.
accept
()));
clients
.
lastElement
().
start
();
globalID
++;
}
catch
(
IOException
ex
)
{
Logger
.
getLogger
(
ConnectionHandler
.
class
.
getName
()).
log
(
Level
.
SEVERE
,
null
,
ex
);
if
(
connectTracker
())
{
System
.
out
.
println
(
"Connection to tracker server successfull... Ready to recieve connection!"
);
while
(
active
){
try
{
clients
.
add
(
new
Client
(
globalID
,
welcomeSocket
.
accept
()));
clients
.
lastElement
().
start
();
globalID
++;
}
catch
(
IOException
ex
)
{
Logger
.
getLogger
(
ConnectionHandler
.
class
.
getName
()).
log
(
Level
.
SEVERE
,
null
,
ex
);
}
}
}
else
{
System
.
out
.
println
(
"Error, Failed to connect server.. "
);
}
System
.
out
.
println
(
"Thread "
+
threadName
+
" exiting..."
);
}
...
...
@@ -77,8 +84,9 @@ public class ConnectionHandler implements Runnable {
active
=
_active
;
}
private
void
connectTracker
()
private
boolean
connectTracker
()
{
boolean
ret
;
try
{
String
sentence
;
String
modifiedSentence
;
...
...
@@ -95,8 +103,11 @@ public class ConnectionHandler implements Runnable {
modifiedSentence
=
inFromTracker
.
readLine
();
System
.
out
.
println
(
"FROM TRACKER: "
+
modifiedSentence
);
newServer
.
close
();
ret
=
true
;
}
catch
(
IOException
ex
)
{
ret
=
false
;
Logger
.
getLogger
(
ConnectionHandler
.
class
.
getName
()).
log
(
Level
.
SEVERE
,
null
,
ex
);
}
return
ret
;
}
}
src/server/TCPServer/src/Model/Client.java
View file @
97f096dc
...
...
@@ -2,6 +2,7 @@ package Model;
import
java.io.BufferedReader
;
import
java.io.DataInputStream
;
import
java.io.DataOutputStream
;
import
java.io.IOException
;
import
java.io.InputStreamReader
;
...
...
@@ -29,7 +30,6 @@ public class Client implements Runnable {
private
Socket
connectionSocket
;
private
Thread
t
;
private
boolean
active
;
private
String
clientSentence
;
private
String
capitalizedSentence
;
public
Client
(
int
_id
,
Socket
_client
)
...
...
@@ -48,16 +48,29 @@ public class Client implements Runnable {
while
(
active
)
{
DataOutputStream
outToClient
=
null
;
BufferedReader
inFromClient
=
null
;
//BufferedReader inFromClient = null;
InputStreamReader
inFromClient
=
null
;
String
clientSentence
=
""
;
int
buf
;
try
{
inFromClient
=
new
BufferedReader
(
new
InputStreamReader
(
connectionSocket
.
getInputStream
()));
clientSentence
=
inFromClient
.
readLine
();
if
(
clientSentence
!=
null
){
System
.
out
.
println
(
"Client ip "
+
threadName
+
" :"
+
connectionSocket
.
getRemoteSocketAddress
());
//connectionSocket.setSoTimeout(3000);
//inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
//clientSentence = inFromClient.readLine();
inFromClient
=
new
InputStreamReader
(
connectionSocket
.
getInputStream
());
while
((
buf
=
inFromClient
.
read
())
!=
-
1
){
char
c
=
(
char
)
buf
;
clientSentence
+=
Character
.
toString
(
c
);
//System.out.print(c);
}
//System.out.println();
System
.
out
.
println
(
"Received From "
+
connectionSocket
.
getInetAddress
().
getHostAddress
()+
": "
+
clientSentence
);
if
(!
clientSentence
.
equals
(
""
)){
outToClient
=
new
DataOutputStream
(
connectionSocket
.
getOutputStream
());
System
.
out
.
println
(
"Received: "
+
clientSentence
);
Object
obj
=
parser
.
parse
(
clientSentence
);
if
(
obj
!=
null
){
JSONObject
data
=
(
JSONObject
)
obj
;
System
.
out
.
println
(
"Message :"
+
data
.
toJSONString
());
}
JSONObject
correct
=
new
JSONObject
();
correct
.
put
(
"status"
,
"ok"
);
...
...
@@ -67,6 +80,7 @@ public class Client implements Runnable {
active
=
false
;
}
else
{
System
.
out
.
println
(
"Null message detected"
);
active
=
false
;
}
}
catch
(
IOException
ex
)
{
...
...
@@ -76,7 +90,7 @@ public class Client implements Runnable {
}
finally
{
try
{
inFromClient
.
close
();
outToClient
.
close
();
//
outToClient.close();
}
catch
(
IOException
ex
)
{
Logger
.
getLogger
(
Client
.
class
.
getName
()).
log
(
Level
.
SEVERE
,
null
,
ex
);
}
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment