Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
TeeTime-Framework
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Nelson Tavares de Sousa
TeeTime-Framework
Commits
849905df
Commit
849905df
authored
10 years ago
by
Christian Wulf
Browse files
Options
Downloads
Patches
Plain Diff
added AbstractTcpReader
parent
72ba5d72
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/main/java/teetime/stage/io/AbstractTcpReader.java
+71
-0
71 additions, 0 deletions
src/main/java/teetime/stage/io/AbstractTcpReader.java
with
71 additions
and
0 deletions
src/main/java/teetime/stage/io/AbstractTcpReader.java
0 → 100644
+
71
−
0
View file @
849905df
package
teetime.stage.io
;
import
java.io.IOException
;
import
java.net.InetSocketAddress
;
import
java.nio.BufferUnderflowException
;
import
java.nio.ByteBuffer
;
import
java.nio.channels.ServerSocketChannel
;
import
java.nio.channels.SocketChannel
;
import
teetime.framework.ProducerStage
;
public
abstract
class
AbstractTcpReader
<
T
>
extends
ProducerStage
<
T
>
{
private
final
int
port
;
private
final
int
bufferCapacity
;
public
AbstractTcpReader
(
final
int
port
,
final
int
bufferCapacity
)
{
super
();
this
.
port
=
port
;
this
.
bufferCapacity
=
bufferCapacity
;
}
@Override
protected
void
execute
()
{
ServerSocketChannel
serversocket
=
null
;
try
{
serversocket
=
ServerSocketChannel
.
open
();
serversocket
.
socket
().
bind
(
new
InetSocketAddress
(
this
.
port
));
logger
.
debug
(
"Listening on port "
+
this
.
port
);
final
SocketChannel
socketChannel
=
serversocket
.
accept
();
try
{
final
ByteBuffer
buffer
=
ByteBuffer
.
allocateDirect
(
bufferCapacity
);
while
(
socketChannel
.
read
(
buffer
)
!=
-
1
)
{
process
(
buffer
);
}
}
finally
{
socketChannel
.
close
();
}
}
catch
(
final
IOException
ex
)
{
logger
.
error
(
"Error while reading"
,
ex
);
}
finally
{
if
(
null
!=
serversocket
)
{
try
{
serversocket
.
close
();
}
catch
(
final
IOException
e
)
{
logger
.
debug
(
"Failed to close TCP connection!"
,
e
);
}
}
this
.
terminate
();
}
}
private
void
process
(
final
ByteBuffer
buffer
)
{
buffer
.
flip
();
try
{
while
(
buffer
.
hasRemaining
())
{
buffer
.
mark
();
this
.
read
(
buffer
);
}
buffer
.
clear
();
}
catch
(
final
BufferUnderflowException
ex
)
{
buffer
.
reset
();
buffer
.
compact
();
}
}
protected
abstract
void
read
(
final
ByteBuffer
buffer
);
}
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