Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
Actor Framework
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Metrics
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
cpp-libs
Actor Framework
Commits
94a379c7
Commit
94a379c7
authored
Aug 08, 2018
by
Joseph Noir
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add programs for measurements
parent
84b78586
Changes
7
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
1461 additions
and
34 deletions
+1461
-34
examples/CMakeLists.txt
examples/CMakeLists.txt
+6
-0
examples/measurements/one_basp_tcp.cpp
examples/measurements/one_basp_tcp.cpp
+345
-0
examples/measurements/one_basp_udp.cpp
examples/measurements/one_basp_udp.cpp
+480
-0
examples/measurements/one_raw_tcp.cpp
examples/measurements/one_raw_tcp.cpp
+294
-0
examples/measurements/one_raw_udp.cpp
examples/measurements/one_raw_udp.cpp
+321
-0
examples/remoting/basp_udp_newb.cpp
examples/remoting/basp_udp_newb.cpp
+4
-4
examples/remoting/raw_tcp_newb.cpp
examples/remoting/raw_tcp_newb.cpp
+11
-30
No files found.
examples/CMakeLists.txt
View file @
94a379c7
...
...
@@ -56,6 +56,12 @@ add(remoting basp_tcp_newb)
add
(
remoting raw_tcp_newb
)
add
(
remoting raw_udp_newb
)
# measurements not meant to stay in CAF
add
(
measurements one_raw_tcp
)
add
(
measurements one_raw_udp
)
add
(
measurements one_basp_tcp
)
add
(
measurements one_basp_udp
)
# basic I/O with brokers
add
(
broker simple_broker
)
add
(
broker simple_http_broker
)
...
...
examples/measurements/one_basp_tcp.cpp
0 → 100644
View file @
94a379c7
#include "caf/io/network/newb.hpp"
#include "caf/logger.hpp"
#include "caf/binary_deserializer.hpp"
#include "caf/binary_serializer.hpp"
#include "caf/detail/call_cfun.hpp"
#include "caf/policy/newb_tcp.hpp"
using
namespace
caf
;
using
caf
::
io
::
network
::
default_multiplexer
;
using
caf
::
io
::
network
::
invalid_native_socket
;
using
caf
::
io
::
network
::
make_client_newb
;
using
caf
::
io
::
network
::
make_server_newb
;
using
caf
::
io
::
network
::
native_socket
;
using
caf
::
policy
::
accept_tcp
;
using
caf
::
policy
::
tcp_protocol
;
using
caf
::
policy
::
tcp_transport
;
namespace
{
using
interval_atom
=
atom_constant
<
atom
(
"interval"
)
>
;
using
ordering_atom
=
atom_constant
<
atom
(
"ordering"
)
>
;
using
send_atom
=
atom_constant
<
atom
(
"send"
)
>
;
using
quit_atom
=
atom_constant
<
atom
(
"quit"
)
>
;
using
responder_atom
=
atom_constant
<
atom
(
"responder"
)
>
;
constexpr
size_t
chunk_size
=
1024
;
//128; //8192; //1024;
struct
basp_header
{
uint32_t
payload_len
;
actor_id
from
;
actor_id
to
;
};
constexpr
size_t
basp_header_len
=
sizeof
(
uint32_t
)
+
sizeof
(
actor_id
)
*
2
;
template
<
class
Inspector
>
typename
Inspector
::
result_type
inspect
(
Inspector
&
fun
,
basp_header
&
hdr
)
{
return
fun
(
meta
::
type_name
(
"tcp_basp_header"
),
hdr
.
payload_len
,
hdr
.
from
,
hdr
.
to
);
}
struct
new_basp_message
{
basp_header
header
;
char
*
payload
;
size_t
payload_len
;
};
template
<
class
Inspector
>
typename
Inspector
::
result_type
inspect
(
Inspector
&
fun
,
new_basp_message
&
msg
)
{
return
fun
(
meta
::
type_name
(
"tcp_new_basp_message"
),
msg
.
header
,
msg
.
payload_len
);
}
struct
basp
{
static
constexpr
size_t
header_size
=
basp_header_len
;
using
message_type
=
new_basp_message
;
using
result_type
=
optional
<
message_type
>
;
io
::
network
::
newb
<
message_type
>*
parent
;
message_type
msg
;
bool
expecting_header
=
true
;
basp
(
io
::
network
::
newb
<
message_type
>*
parent
)
:
parent
(
parent
)
{
// TODO: this is dangerous ...
// Maybe we need an init function that is called with `start()`?
parent
->
configure_read
(
io
::
receive_policy
::
exactly
(
basp_header_len
));
}
error
read_header
(
char
*
bytes
,
size_t
count
)
{
if
(
count
<
basp_header_len
)
return
sec
::
unexpected_message
;
binary_deserializer
bd
{
&
parent
->
backend
(),
bytes
,
count
};
bd
(
msg
.
header
);
size_t
size
=
static_cast
<
size_t
>
(
msg
.
header
.
payload_len
);
parent
->
configure_read
(
io
::
receive_policy
::
exactly
(
size
));
expecting_header
=
false
;
return
none
;
}
error
read_payload
(
char
*
bytes
,
size_t
count
)
{
if
(
count
<
msg
.
header
.
payload_len
)
{
CAF_LOG_DEBUG
(
"buffer contains "
<<
count
<<
" bytes of expected "
<<
msg
.
header
.
payload_len
);
return
sec
::
unexpected_message
;
}
msg
.
payload
=
bytes
;
msg
.
payload_len
=
msg
.
header
.
payload_len
;
parent
->
handle
(
msg
);
expecting_header
=
true
;
parent
->
configure_read
(
io
::
receive_policy
::
exactly
(
basp_header_len
));
return
none
;
}
error
read
(
char
*
bytes
,
size_t
count
)
{
if
(
expecting_header
)
return
read_header
(
bytes
,
count
);
else
return
read_payload
(
bytes
,
count
);
}
error
timeout
(
atom_value
,
uint32_t
)
{
return
none
;
}
size_t
write_header
(
io
::
network
::
byte_buffer
&
buf
,
io
::
network
::
header_writer
*
hw
)
{
CAF_ASSERT
(
hw
!=
nullptr
);
(
*
hw
)(
buf
);
return
header_size
;
}
void
prepare_for_sending
(
io
::
network
::
byte_buffer
&
buf
,
size_t
hstart
,
size_t
offset
,
size_t
plen
)
{
stream_serializer
<
charbuf
>
out
{
&
parent
->
backend
(),
buf
.
data
()
+
hstart
+
offset
,
sizeof
(
uint32_t
)};
auto
len
=
static_cast
<
uint32_t
>
(
plen
);
out
(
len
);
}
};
struct
basp_newb
:
public
io
::
network
::
newb
<
new_basp_message
>
{
using
message_type
=
new_basp_message
;
basp_newb
(
caf
::
actor_config
&
cfg
,
default_multiplexer
&
dm
,
native_socket
sockfd
)
:
newb
<
message_type
>
(
cfg
,
dm
,
sockfd
),
running
(
true
),
interval_counter
(
0
),
received_messages
(
0
),
interval
(
5000
)
{
// nop
CAF_LOG_TRACE
(
""
);
}
void
handle
(
message_type
&
msg
)
override
{
CAF_PUSH_AID_FROM_PTR
(
this
);
CAF_LOG_TRACE
(
""
);
if
(
msg
.
payload_len
==
1
)
{
// nop
}
else
{
received_messages
+=
1
;
if
(
received_messages
%
1000
==
0
)
std
::
cout
<<
"received "
<<
received_messages
<<
" messages"
<<
std
::
endl
;
// nop
}
}
behavior
make_behavior
()
override
{
set_default_handler
(
print_and_drop
);
return
{
// Must be implemented at the moment, will be cought by the broker in a
// later implementation.
[
=
](
atom_value
atm
,
uint32_t
id
)
{
protocol
->
timeout
(
atm
,
id
);
},
[
=
](
send_atom
,
char
c
)
{
if
(
running
)
{
delayed_send
(
this
,
interval
,
send_atom
::
value
,
char
((
c
+
1
)
%
256
));
auto
hw
=
caf
::
make_callback
([
&
](
io
::
network
::
byte_buffer
&
buf
)
->
error
{
binary_serializer
bs
(
&
backend
(),
buf
);
bs
(
basp_header
{
0
,
id
(),
actor_id
{}});
return
none
;
});
auto
whdl
=
wr_buf
(
&
hw
);
CAF_ASSERT
(
whdl
.
buf
!=
nullptr
);
CAF_ASSERT
(
whdl
.
protocol
!=
nullptr
);
binary_serializer
bs
(
&
backend
(),
*
whdl
.
buf
);
auto
start
=
whdl
.
buf
->
size
();
whdl
.
buf
->
resize
(
start
+
chunk_size
);
std
::
fill
(
whdl
.
buf
->
begin
()
+
start
,
whdl
.
buf
->
end
(),
c
);
}
},
[
=
](
responder_atom
,
actor
r
)
{
std
::
cout
<<
"got responder assigned"
<<
std
::
endl
;
responder
=
r
;
send
(
r
,
this
);
},
[
=
](
interval_atom
)
{
if
(
running
)
{
delayed_send
(
this
,
std
::
chrono
::
seconds
(
1
),
interval_atom
::
value
);
interval_counter
+=
1
;
data
.
emplace_back
(
interval
,
transport
->
count
,
transport
->
offline_buffer
.
size
());
if
(
interval_counter
%
10
==
0
)
{
auto
cnt
=
interval
.
count
();
auto
dec
=
cnt
>
1000
?
1000
:
(
cnt
>
100
?
100
:
10
);
interval
-=
std
::
chrono
::
microseconds
(
dec
);
}
transport
->
count
=
0
;
if
(
interval
.
count
()
<=
0
)
running
=
false
;
}
else
{
std
::
map
<
size_t
,
std
::
vector
<
size_t
>>
aggregate
;
for
(
auto
&
t
:
data
)
{
auto
expected
=
(
1000000
/
get
<
0
>
(
t
).
count
());
aggregate
[
expected
].
push_back
(
get
<
1
>
(
t
));
}
for
(
auto
&
p
:
aggregate
)
{
std
::
cerr
<<
p
.
first
;
for
(
auto
v
:
p
.
second
)
std
::
cerr
<<
", "
<<
v
;
std
::
cerr
<<
std
::
endl
;
}
send
(
this
,
quit_atom
::
value
);
}
},
[
=
](
quit_atom
)
{
std
::
cout
<<
"got quit message"
<<
std
::
endl
;
// Remove from multiplexer loop.
stop
();
// Quit actor.
quit
();
send
(
responder
,
quit_atom
::
value
);
}
};
}
bool
running
;
actor
responder
;
uint32_t
interval_counter
;
uint32_t
received_messages
;
std
::
chrono
::
microseconds
interval
;
// values: measurement point, current interval, messages sent in interval, offline buffer size
std
::
vector
<
std
::
tuple
<
std
::
chrono
::
microseconds
,
size_t
,
size_t
>>
data
;
};
template
<
class
ProtocolPolicy
>
struct
tcp_acceptor
:
public
io
::
network
::
newb_acceptor
<
typename
ProtocolPolicy
::
message_type
>
{
using
super
=
io
::
network
::
newb_acceptor
<
typename
ProtocolPolicy
::
message_type
>
;
tcp_acceptor
(
default_multiplexer
&
dm
,
native_socket
sockfd
)
:
super
(
dm
,
sockfd
)
{
// nop
}
expected
<
actor
>
create_newb
(
native_socket
sockfd
,
io
::
network
::
transport_policy_ptr
pol
)
override
{
CAF_LOG_TRACE
(
CAF_ARG
(
sockfd
));
std
::
cout
<<
"creating newb"
<<
std
::
endl
;
auto
n
=
io
::
network
::
make_newb
<
basp_newb
>
(
this
->
backend
().
system
(),
sockfd
);
auto
ptr
=
caf
::
actor_cast
<
caf
::
abstract_actor
*>
(
n
);
if
(
ptr
==
nullptr
)
return
sec
::
runtime_error
;
auto
&
ref
=
dynamic_cast
<
basp_newb
&>
(
*
ptr
);
ref
.
transport
=
std
::
move
(
pol
);
ref
.
protocol
.
reset
(
new
ProtocolPolicy
(
&
ref
));
ref
.
responder
=
responder
;
ref
.
configure_read
(
io
::
receive_policy
::
exactly
(
basp_header_len
));
anon_send
(
responder
,
n
);
return
n
;
}
actor
responder
;
};
class
config
:
public
actor_system_config
{
public:
uint16_t
port
=
12345
;
std
::
string
host
=
"127.0.0.1"
;
bool
is_server
=
false
;
config
()
{
opt_group
{
custom_options_
,
"global"
}
.
add
(
port
,
"port,P"
,
"set port"
)
.
add
(
host
,
"host,H"
,
"set host"
)
.
add
(
is_server
,
"server,s"
,
"set server"
);
}
};
void
caf_main
(
actor_system
&
sys
,
const
config
&
cfg
)
{
using
acceptor_t
=
tcp_acceptor
<
tcp_protocol
<
basp
>>
;
const
char
*
host
=
cfg
.
host
.
c_str
();
const
uint16_t
port
=
cfg
.
port
;
scoped_actor
self
{
sys
};
auto
running
=
[
=
](
event_based_actor
*
self
,
std
::
string
,
actor
m
,
actor
)
->
behavior
{
return
{
[
=
](
quit_atom
)
{
self
->
send
(
m
,
quit_atom
::
value
);
}
};
};
auto
init
=
[
=
](
event_based_actor
*
self
,
std
::
string
name
,
actor
m
)
->
behavior
{
self
->
set_default_handler
(
skip
);
return
{
[
=
](
actor
b
)
{
std
::
cout
<<
"["
<<
name
<<
"] got broker, let's do this"
<<
std
::
endl
;
self
->
become
(
running
(
self
,
name
,
m
,
b
));
self
->
set_default_handler
(
print_and_drop
);
}
};
};
auto
dummy_broker
=
[](
io
::
broker
*
)
->
behavior
{
return
{
[](
io
::
new_connection_msg
&
)
{
std
::
cout
<<
"got new connection"
<<
std
::
endl
;
}
};
};
auto
name
=
cfg
.
is_server
?
"server"
:
"client"
;
auto
helper
=
sys
.
spawn
(
init
,
name
,
self
);
actor
nb
;
auto
await_done
=
[
&
]()
{
self
->
receive
(
[
&
](
quit_atom
)
{
std
::
cout
<<
"done"
<<
std
::
endl
;
}
);
};
if
(
cfg
.
is_server
)
{
std
::
cout
<<
"creating new server"
<<
std
::
endl
;
auto
server_ptr
=
make_server_newb
<
acceptor_t
,
accept_tcp
>
(
sys
,
port
,
nullptr
,
true
);
//server_ptr->responder = helper;
//std::cout << "creating new client" << std::endl;
//auto client = make_client_newb<basp_newb, tcp_transport,
//tcp_protocol<raw_tcp>>(sys, host, port);
// If I don't do this, our newb acceptor will never get events ...
auto
b
=
sys
.
middleman
().
spawn_server
(
dummy_broker
,
port
+
1
);
await_done
();
}
else
{
std
::
cout
<<
"creating new client"
<<
std
::
endl
;
auto
client
=
make_client_newb
<
basp_newb
,
tcp_transport
,
tcp_protocol
<
basp
>>
(
sys
,
host
,
port
);
self
->
send
(
client
,
responder_atom
::
value
,
helper
);
self
->
send
(
client
,
send_atom
::
value
,
char
(
0
));
self
->
send
(
client
,
interval_atom
::
value
);
await_done
();
}
}
}
// namespace anonymous
CAF_MAIN
(
io
::
middleman
);
examples/measurements/one_basp_udp.cpp
0 → 100644
View file @
94a379c7
#include "caf/io/network/newb.hpp"
#include "caf/logger.hpp"
#include "caf/binary_deserializer.hpp"
#include "caf/binary_serializer.hpp"
#include "caf/detail/call_cfun.hpp"
#include "caf/policy/newb_udp.hpp"
using
namespace
caf
;
using
caf
::
io
::
network
::
default_multiplexer
;
using
caf
::
io
::
network
::
invalid_native_socket
;
using
caf
::
io
::
network
::
make_client_newb
;
using
caf
::
io
::
network
::
make_server_newb
;
using
caf
::
io
::
network
::
native_socket
;
using
caf
::
policy
::
accept_udp
;
using
caf
::
policy
::
udp_protocol
;
using
caf
::
policy
::
udp_transport
;
namespace
{
using
interval_atom
=
atom_constant
<
atom
(
"interval"
)
>
;
using
ordering_atom
=
atom_constant
<
atom
(
"ordering"
)
>
;
using
send_atom
=
atom_constant
<
atom
(
"send"
)
>
;
using
quit_atom
=
atom_constant
<
atom
(
"quit"
)
>
;
using
responder_atom
=
atom_constant
<
atom
(
"responder"
)
>
;
using
start_atom
=
atom_constant
<
atom
(
"start"
)
>
;
using
handshake_atom
=
atom_constant
<
atom
(
"handshake"
)
>
;
constexpr
size_t
chunk_size
=
8192
;
//8192; //128; //1024;
struct
basp_header
{
uint32_t
payload_len
;
actor_id
from
;
actor_id
to
;
};
template
<
class
Inspector
>
typename
Inspector
::
result_type
inspect
(
Inspector
&
fun
,
basp_header
&
hdr
)
{
return
fun
(
meta
::
type_name
(
"basp_header"
),
hdr
.
payload_len
,
hdr
.
from
,
hdr
.
to
);
}
constexpr
size_t
udp_basp_header_len
=
sizeof
(
uint32_t
)
+
sizeof
(
actor_id
)
*
2
;
using
sequence_type
=
uint16_t
;
struct
ordering_header
{
sequence_type
seq
;
};
template
<
class
Inspector
>
typename
Inspector
::
result_type
inspect
(
Inspector
&
fun
,
ordering_header
&
hdr
)
{
return
fun
(
meta
::
type_name
(
"ordering_header"
),
hdr
.
seq
);
}
constexpr
size_t
udp_ordering_header_len
=
sizeof
(
sequence_type
);
struct
new_basp_message
{
basp_header
header
;
char
*
payload
;
size_t
payload_len
;
};
template
<
class
Inspector
>
typename
Inspector
::
result_type
inspect
(
Inspector
&
fun
,
new_basp_message
&
msg
)
{
return
fun
(
meta
::
type_name
(
"new_basp_message"
),
msg
.
header
,
msg
.
payload_len
);
}
struct
basp
{
static
constexpr
size_t
header_size
=
udp_basp_header_len
;
using
message_type
=
new_basp_message
;
using
result_type
=
optional
<
message_type
>
;
io
::
network
::
newb
<
message_type
>*
parent
;
message_type
msg
;
basp
(
io
::
network
::
newb
<
message_type
>*
parent
)
:
parent
(
parent
)
{
// nop
}
error
read
(
char
*
bytes
,
size_t
count
)
{
// Read header.
if
(
count
<
udp_basp_header_len
)
{
CAF_LOG_DEBUG
(
"not enought bytes for basp header"
);
return
sec
::
unexpected_message
;
}
binary_deserializer
bd
{
&
parent
->
backend
(),
bytes
,
count
};
bd
(
msg
.
header
);
size_t
payload_len
=
static_cast
<
size_t
>
(
msg
.
header
.
payload_len
);
// Read payload.
auto
remaining
=
count
-
udp_basp_header_len
;
// TODO: Could be `!=` ?
if
(
remaining
<
payload_len
)
{
CAF_LOG_ERROR
(
"not enough bytes remaining to fit payload"
);
return
sec
::
unexpected_message
;
}
msg
.
payload
=
bytes
+
udp_basp_header_len
;
msg
.
payload_len
=
msg
.
header
.
payload_len
;
parent
->
handle
(
msg
);
return
none
;
}
error
timeout
(
atom_value
,
uint32_t
)
{
return
none
;
}
size_t
write_header
(
io
::
network
::
byte_buffer
&
buf
,
io
::
network
::
header_writer
*
hw
)
{
CAF_ASSERT
(
hw
!=
nullptr
);
(
*
hw
)(
buf
);
return
header_size
;
}
void
prepare_for_sending
(
io
::
network
::
byte_buffer
&
buf
,
size_t
hstart
,
size_t
offset
,
size_t
plen
)
{
stream_serializer
<
charbuf
>
out
{
&
parent
->
backend
(),
buf
.
data
()
+
hstart
+
offset
,
sizeof
(
uint32_t
)};
auto
len
=
static_cast
<
uint32_t
>
(
plen
);
out
(
len
);
}
};
template
<
class
Next
>
struct
ordering
{
static
constexpr
size_t
header_size
=
udp_ordering_header_len
;
using
message_type
=
typename
Next
::
message_type
;
using
result_type
=
typename
Next
::
result_type
;
sequence_type
seq_read
=
0
;
sequence_type
seq_write
=
0
;
size_t
max_pending_messages
=
10
;
std
::
chrono
::
milliseconds
pending_to
=
std
::
chrono
::
milliseconds
(
100
);
io
::
network
::
newb
<
message_type
>*
parent
;
Next
next
;
std
::
unordered_map
<
sequence_type
,
std
::
vector
<
char
>>
pending
;
ordering
(
io
::
network
::
newb
<
message_type
>*
parent
)
:
parent
(
parent
),
next
(
parent
)
{
// nop
}
error
deliver_pending
()
{
if
(
pending
.
empty
())
return
none
;
while
(
pending
.
count
(
seq_read
)
>
0
)
{
auto
&
buf
=
pending
[
seq_read
];
auto
res
=
next
.
read
(
buf
.
data
(),
buf
.
size
());
pending
.
erase
(
seq_read
);
// TODO: Cancel timeout.
if
(
res
)
return
res
;
}
return
none
;
}
error
add_pending
(
char
*
bytes
,
size_t
count
,
sequence_type
seq
)
{
pending
[
seq
]
=
std
::
vector
<
char
>
(
bytes
+
header_size
,
bytes
+
count
);
parent
->
set_timeout
(
pending_to
,
ordering_atom
::
value
,
seq
);
if
(
pending
.
size
()
>
max_pending_messages
)
{
seq_read
=
pending
.
begin
()
->
first
;
return
deliver_pending
();
}
return
none
;
}
error
read
(
char
*
bytes
,
size_t
count
)
{
if
(
count
<
header_size
)
return
sec
::
unexpected_message
;
ordering_header
hdr
;
binary_deserializer
bd
(
&
parent
->
backend
(),
bytes
,
count
);
bd
(
hdr
);
// TODO: Use the comparison function from BASP instance.
if
(
hdr
.
seq
==
seq_read
)
{
seq_read
+=
1
;
auto
res
=
next
.
read
(
bytes
+
header_size
,
count
-
header_size
);
if
(
res
)
return
res
;
return
deliver_pending
();
}
else
if
(
hdr
.
seq
>
seq_read
)
{
add_pending
(
bytes
,
count
,
hdr
.
seq
);
return
none
;
}
return
none
;
}
error
timeout
(
atom_value
atm
,
uint32_t
id
)
{
if
(
atm
==
ordering_atom
::
value
)
{
error
err
=
none
;
sequence_type
seq
=
static_cast
<
sequence_type
>
(
id
);
if
(
pending
.
count
(
seq
)
>
0
)
{
seq_read
=
static_cast
<
sequence_type
>
(
seq
);
err
=
deliver_pending
();
}
return
err
;
}
return
next
.
timeout
(
atm
,
id
);
}
void
write_header
(
io
::
network
::
byte_buffer
&
buf
,
io
::
network
::
header_writer
*
hw
)
{
binary_serializer
bs
(
&
parent
->
backend
(),
buf
);
bs
(
ordering_header
{
seq_write
});
seq_write
+=
1
;
next
.
write_header
(
buf
,
hw
);
return
;
}
void
prepare_for_sending
(
io
::
network
::
byte_buffer
&
buf
,
size_t
hstart
,
size_t
offset
,
size_t
plen
)
{
next
.
prepare_for_sending
(
buf
,
hstart
,
offset
+
header_size
,
plen
);
}
};
struct
raw_newb
:
public
io
::
network
::
newb
<
new_basp_message
>
{
using
message_type
=
new_basp_message
;
raw_newb
(
caf
::
actor_config
&
cfg
,
default_multiplexer
&
dm
,
native_socket
sockfd
)
:
newb
<
message_type
>
(
cfg
,
dm
,
sockfd
),
running
(
true
),
is_client
(
true
),
interval_counter
(
0
),
received_messages
(
0
),
interval
(
5000
)
{
// nop
CAF_LOG_TRACE
(
""
);
}
void
handle
(
message_type
&
msg
)
override
{
CAF_PUSH_AID_FROM_PTR
(
this
);
CAF_LOG_TRACE
(
""
);
if
(
is_client
)
{
send
(
responder
,
handshake_atom
::
value
);
}
else
if
(
msg
.
payload_len
==
1
)
{
auto
byte
=
*
msg
.
payload
;
if
(
byte
==
'h'
)
std
::
cout
<<
"I'll consider this the handshake"
<<
std
::
endl
;
else
if
(
byte
==
'q'
)
send
(
this
,
quit_atom
::
value
);
send
(
this
,
handshake_atom
::
value
);
}
else
{
if
(
msg
.
payload_len
!=
chunk_size
)
std
::
cout
<<
"Hmmm, payload is "
<<
msg
.
payload_len
<<
" and not "
<<
chunk_size
<<
std
::
endl
;
received_messages
+=
1
;
if
(
received_messages
%
1000
==
0
)
std
::
cout
<<
"received "
<<
received_messages
<<
" messages"
<<
std
::
endl
;
//std::cout << "received message" << std::endl;
// nop
}
}
behavior
make_behavior
()
override
{
set_default_handler
(
print_and_drop
);
return
{
// Must be implemented at the moment, will be cought by the broker in a
// later implementation.
[
=
](
atom_value
atm
,
uint32_t
id
)
{
protocol
->
timeout
(
atm
,
id
);
},
[
=
](
handshake_atom
)
{
auto
hw
=
caf
::
make_callback
([
&
](
io
::
network
::
byte_buffer
&
buf
)
->
error
{
binary_serializer
bs
(
&
backend
(),
buf
);
bs
(
basp_header
{
0
,
id
(),
actor_id
{}});
return
none
;
});
auto
whdl
=
wr_buf
(
&
hw
);
CAF_ASSERT
(
whdl
.
buf
!=
nullptr
);
CAF_ASSERT
(
whdl
.
protocol
!=
nullptr
);
binary_serializer
bs
(
&
backend
(),
*
whdl
.
buf
);
whdl
.
buf
->
push_back
(
'h'
);
},
[
=
](
send_atom
,
char
c
)
{
if
(
running
)
{
delayed_send
(
this
,
interval
,
send_atom
::
value
,
char
((
c
+
1
)
%
256
));
auto
hw
=
caf
::
make_callback
([
&
](
io
::
network
::
byte_buffer
&
buf
)
->
error
{
binary_serializer
bs
(
&
backend
(),
buf
);
bs
(
basp_header
{
0
,
id
(),
actor_id
{}});
return
none
;
});
auto
whdl
=
wr_buf
(
&
hw
);
CAF_ASSERT
(
whdl
.
buf
!=
nullptr
);
CAF_ASSERT
(
whdl
.
protocol
!=
nullptr
);
binary_serializer
bs
(
&
backend
(),
*
whdl
.
buf
);
auto
start
=
whdl
.
buf
->
size
();
whdl
.
buf
->
resize
(
start
+
chunk_size
);
std
::
fill
(
whdl
.
buf
->
begin
()
+
start
,
whdl
.
buf
->
end
(),
c
);
}
},
[
=
](
responder_atom
,
actor
r
)
{
std
::
cout
<<
"got responder assigned"
<<
std
::
endl
;
responder
=
r
;
send
(
r
,
this
);
},
[
=
](
interval_atom
)
{
if
(
running
)
{
delayed_send
(
this
,
std
::
chrono
::
seconds
(
1
),
interval_atom
::
value
);
data
.
emplace_back
(
interval
,
transport
->
count
,
transport
->
offline_buffer
.
size
());
interval_counter
+=
1
;
if
(
interval_counter
%
10
==
0
)
{
auto
cnt
=
interval
.
count
();
auto
dec
=
cnt
>
1000
?
1000
:
(
cnt
>
100
?
100
:
10
);
interval
-=
std
::
chrono
::
microseconds
(
dec
);
}
transport
->
count
=
0
;
if
(
interval
.
count
()
<=
0
)
running
=
false
;
}
else
{
std
::
map
<
size_t
,
std
::
vector
<
size_t
>>
aggregate
;
for
(
auto
&
t
:
data
)
{
auto
expected
=
(
1000000
/
get
<
0
>
(
t
).
count
());
aggregate
[
expected
].
push_back
(
get
<
1
>
(
t
));
}
for
(
auto
&
p
:
aggregate
)
{
std
::
cerr
<<
p
.
first
;
for
(
auto
v
:
p
.
second
)
std
::
cerr
<<
", "
<<
v
;
std
::
cerr
<<
std
::
endl
;
}
/*
for (auto& t : data)
std::cerr << (1000000 / get<0>(t).count()) << ", "
<< get<1>(t) << ", " << get<2>(t) << std::endl;
*/
send
(
this
,
quit_atom
::
value
);
}
},
[
=
](
quit_atom
)
{
std
::
cout
<<
"got quit message"
<<
std
::
endl
;
// Remove from multiplexer loop.
stop
();
// Quit actor.
quit
();
send
(
responder
,
quit_atom
::
value
);
}
};
}
bool
running
;
bool
is_client
;
actor
responder
;
uint32_t
interval_counter
;
uint32_t
received_messages
;
std
::
chrono
::
microseconds
interval
;
// values: measurement point, current interval, messages sent in interval, offline buffer size
std
::
vector
<
std
::
tuple
<
std
::
chrono
::
microseconds
,
size_t
,
size_t
>>
data
;
};
template
<
class
ProtocolPolicy
>
struct
udp_acceptor
:
public
io
::
network
::
newb_acceptor
<
typename
ProtocolPolicy
::
message_type
>
{
using
super
=
io
::
network
::
newb_acceptor
<
typename
ProtocolPolicy
::
message_type
>
;
udp_acceptor
(
default_multiplexer
&
dm
,
native_socket
sockfd
)
:
super
(
dm
,
sockfd
)
{
// nop
}
~
udp_acceptor
()
{
std
::
cout
<<
"terminating udp acceptor"
<<
std
::
endl
;
}
expected
<
actor
>
create_newb
(
native_socket
sockfd
,
io
::
network
::
transport_policy_ptr
pol
)
override
{
CAF_LOG_TRACE
(
CAF_ARG
(
sockfd
));
std
::
cout
<<
"creating newb"
<<
std
::
endl
;
auto
n
=
io
::
network
::
make_newb
<
raw_newb
>
(
this
->
backend
().
system
(),
sockfd
);
auto
ptr
=
caf
::
actor_cast
<
caf
::
abstract_actor
*>
(
n
);
if
(
ptr
==
nullptr
)
return
sec
::
runtime_error
;
auto
&
ref
=
dynamic_cast
<
raw_newb
&>
(
*
ptr
);
ref
.
transport
=
std
::
move
(
pol
);
ref
.
protocol
.
reset
(
new
ProtocolPolicy
(
&
ref
));
ref
.
responder
=
responder
;
// Read first message from this socket
ref
.
is_client
=
false
;
ref
.
transport
->
prepare_next_read
(
this
);
ref
.
transport
->
read_some
(
this
,
*
ref
.
protocol
.
get
());
// TODO: Just a workaround.
anon_send
(
responder
,
n
);
return
n
;
}
actor
responder
;
};
class
config
:
public
actor_system_config
{
public:
uint16_t
port
=
12345
;
std
::
string
host
=
"127.0.0.1"
;
bool
is_server
=
false
;
config
()
{
opt_group
{
custom_options_
,
"global"
}
.
add
(
port
,
"port,P"
,
"set port"
)
.
add
(
host
,
"host,H"
,
"set host"
)
.
add
(
is_server
,
"server,s"
,
"set server"
);
}
};
void
caf_main
(
actor_system
&
sys
,
const
config
&
cfg
)
{
using
acceptor_t
=
udp_acceptor
<
udp_protocol
<
ordering
<
basp
>>>
;
const
char
*
host
=
cfg
.
host
.
c_str
();
const
uint16_t
port
=
cfg
.
port
;
scoped_actor
self
{
sys
};
auto
running
=
[
=
](
event_based_actor
*
self
,
std
::
string
name
,
actor
m
,
actor
)
->
behavior
{
return
{
[
=
](
handshake_atom
)
{
std
::
cout
<<
"["
<<
name
<<
"] got server"
<<
std
::
endl
;
self
->
send
(
m
,
quit_atom
::
value
);
},
[
=
](
quit_atom
)
{
self
->
send
(
m
,
quit_atom
::
value
);
}
};
};
auto
init
=
[
=
](
event_based_actor
*
self
,
std
::
string
name
,
actor
m
)
->
behavior
{
self
->
set_default_handler
(
skip
);
return
{
[
=
](
actor
b
)
{
std
::
cout
<<
"["
<<
name
<<
"] got broker, let's do this"
<<
std
::
endl
;
self
->
become
(
running
(
self
,
name
,
m
,
b
));
self
->
set_default_handler
(
print_and_drop
);
}
};
};
auto
dummy_broker
=
[](
io
::
broker
*
)
->
behavior
{
return
{
[](
io
::
new_connection_msg
&
)
{
std
::
cout
<<
"got new connection"
<<
std
::
endl
;
}
};
};
auto
name
=
cfg
.
is_server
?
"server"
:
"client"
;
auto
helper
=
sys
.
spawn
(
init
,
name
,
self
);
actor
nb
;
auto
await_done
=
[
&
](
std
::
string
msg
)
{
self
->
receive
(
[
&
](
quit_atom
)
{
std
::
cout
<<
msg
<<
std
::
endl
;
}
);
};
if
(
cfg
.
is_server
)
{
std
::
cout
<<
"creating new server"
<<
std
::
endl
;
auto
server_ptr
=
make_server_newb
<
acceptor_t
,
accept_udp
>
(
sys
,
port
,
nullptr
,
true
);
// If I don't do this, our newb acceptor will never get events ...
auto
b
=
sys
.
middleman
().
spawn_server
(
dummy_broker
,
port
+
1
);
await_done
(
"done"
);
}
else
{
std
::
cout
<<
"creating new client"
<<
std
::
endl
;
auto
client
=
make_client_newb
<
raw_newb
,
udp_transport
,
udp_protocol
<
ordering
<
basp
>>>
(
sys
,
host
,
port
);
self
->
send
(
client
,
responder_atom
::
value
,
helper
);
self
->
send
(
client
,
handshake_atom
::
value
);
await_done
(
"let's start"
);
self
->
send
(
client
,
send_atom
::
value
,
char
(
0
));
self
->
send
(
client
,
interval_atom
::
value
);
await_done
(
"done"
);
std
::
abort
();
}
}
}
// namespace anonymous
CAF_MAIN
(
io
::
middleman
);
examples/measurements/one_raw_tcp.cpp
0 → 100644
View file @
94a379c7
#include "caf/io/network/newb.hpp"
#include "caf/logger.hpp"
#include "caf/binary_deserializer.hpp"
#include "caf/binary_serializer.hpp"
#include "caf/detail/call_cfun.hpp"
#include "caf/policy/newb_tcp.hpp"
using
namespace
caf
;
using
caf
::
io
::
network
::
default_multiplexer
;
using
caf
::
io
::
network
::
invalid_native_socket
;
using
caf
::
io
::
network
::
make_client_newb
;
using
caf
::
io
::
network
::
make_server_newb
;
using
caf
::
io
::
network
::
native_socket
;
using
caf
::
policy
::
accept_tcp
;
using
caf
::
policy
::
tcp_protocol
;
using
caf
::
policy
::
tcp_transport
;
namespace
{
using
interval_atom
=
atom_constant
<
atom
(
"interval"
)
>
;
using
ordering_atom
=
atom_constant
<
atom
(
"ordering"
)
>
;
using
send_atom
=
atom_constant
<
atom
(
"send"
)
>
;
using
quit_atom
=
atom_constant
<
atom
(
"quit"
)
>
;
using
responder_atom
=
atom_constant
<
atom
(
"responder"
)
>
;
constexpr
size_t
chunk_size
=
8192
;
//128; //8192; //1024;
struct
new_data
{
char
*
payload
;
size_t
payload_len
;
};
template
<
class
Inspector
>
typename
Inspector
::
result_type
inspect
(
Inspector
&
fun
,
new_data
&
data
)
{
return
fun
(
meta
::
type_name
(
"new_data"
),
data
.
payload_len
);
}
struct
raw_tcp
{
using
message_type
=
new_data
;
using
result_type
=
optional
<
message_type
>
;
io
::
network
::
newb
<
message_type
>*
parent
;
message_type
msg
;
raw_tcp
(
io
::
network
::
newb
<
message_type
>*
parent
)
:
parent
(
parent
)
{
// nop
}
error
read
(
char
*
bytes
,
size_t
count
)
{
msg
.
payload
=
bytes
;
msg
.
payload_len
=
count
;
parent
->
handle
(
msg
);
return
none
;
}
error
timeout
(
atom_value
,
uint32_t
)
{
return
none
;
}
size_t
write_header
(
io
::
network
::
byte_buffer
&
,
io
::
network
::
header_writer
*
)
{
return
0
;
}
void
prepare_for_sending
(
io
::
network
::
byte_buffer
&
,
size_t
,
size_t
,
size_t
)
{
// nop
}
};
struct
raw_newb
:
public
io
::
network
::
newb
<
new_data
>
{
using
message_type
=
new_data
;
raw_newb
(
caf
::
actor_config
&
cfg
,
default_multiplexer
&
dm
,
native_socket
sockfd
)
:
newb
<
message_type
>
(
cfg
,
dm
,
sockfd
),
running
(
true
),
interval_counter
(
0
),
received_messages
(
0
),
interval
(
5000
)
{
// nop
CAF_LOG_TRACE
(
""
);
}
void
handle
(
message_type
&
msg
)
override
{
CAF_PUSH_AID_FROM_PTR
(
this
);
CAF_LOG_TRACE
(
""
);
if
(
msg
.
payload_len
==
1
)
{
// nop
}
else
{
received_messages
+=
1
;
if
(
received_messages
%
1000
==
0
)
std
::
cout
<<
"received "
<<
received_messages
<<
" messages"
<<
std
::
endl
;
//std::cout << "received message" << std::endl;
// nop
}
}
behavior
make_behavior
()
override
{
set_default_handler
(
print_and_drop
);
return
{
// Must be implemented at the moment, will be cought by the broker in a
// later implementation.
[
=
](
atom_value
atm
,
uint32_t
id
)
{
protocol
->
timeout
(
atm
,
id
);
},
[
=
](
send_atom
,
char
c
)
{
if
(
running
)
{
delayed_send
(
this
,
interval
,
send_atom
::
value
,
char
((
c
+
1
)
%
256
));
auto
whdl
=
wr_buf
(
nullptr
);
CAF_ASSERT
(
whdl
.
buf
!=
nullptr
);
CAF_ASSERT
(
whdl
.
protocol
!=
nullptr
);
binary_serializer
bs
(
&
backend
(),
*
whdl
.
buf
);
whdl
.
buf
->
resize
(
chunk_size
);
std
::
fill
(
whdl
.
buf
->
begin
(),
whdl
.
buf
->
end
(),
c
);
}
},
[
=
](
responder_atom
,
actor
r
)
{
std
::
cout
<<
"got responder assigned"
<<
std
::
endl
;
responder
=
r
;
send
(
r
,
this
);
},
[
=
](
interval_atom
)
{
if
(
running
)
{
delayed_send
(
this
,
std
::
chrono
::
seconds
(
1
),
interval_atom
::
value
);
data
.
emplace_back
(
interval
,
transport
->
count
,
transport
->
offline_buffer
.
size
());
interval_counter
+=
1
;
if
(
interval_counter
%
10
==
0
)
{
auto
cnt
=
interval
.
count
();
auto
dec
=
cnt
>
1000
?
1000
:
(
cnt
>
100
?
100
:
10
);
interval
-=
std
::
chrono
::
microseconds
(
dec
);
}
transport
->
count
=
0
;
if
(
interval
.
count
()
<=
0
)
running
=
false
;
}
else
{
std
::
map
<
size_t
,
std
::
vector
<
size_t
>>
aggregate
;
for
(
auto
&
t
:
data
)
{
auto
expected
=
(
1000000
/
get
<
0
>
(
t
).
count
());
aggregate
[
expected
].
push_back
(
get
<
1
>
(
t
));
}
for
(
auto
&
p
:
aggregate
)
{
std
::
cerr
<<
p
.
first
;
for
(
auto
v
:
p
.
second
)
std
::
cerr
<<
", "
<<
v
;
std
::
cerr
<<
std
::
endl
;
}
send
(
this
,
quit_atom
::
value
);
}
},
[
=
](
quit_atom
)
{
std
::
cout
<<
"got quit message"
<<
std
::
endl
;
// Remove from multiplexer loop.
stop
();
// Quit actor.
quit
();
send
(
responder
,
quit_atom
::
value
);
}
};
}
bool
running
;
actor
responder
;
uint32_t
interval_counter
;
uint32_t
received_messages
;
std
::
chrono
::
microseconds
interval
;
// values: measurement point, current interval, messages sent in interval, offline buffer size
std
::
vector
<
std
::
tuple
<
std
::
chrono
::
microseconds
,
size_t
,
size_t
>>
data
;
};
template
<
class
ProtocolPolicy
>
struct
tcp_acceptor
:
public
io
::
network
::
newb_acceptor
<
typename
ProtocolPolicy
::
message_type
>
{
using
super
=
io
::
network
::
newb_acceptor
<
typename
ProtocolPolicy
::
message_type
>
;
tcp_acceptor
(
default_multiplexer
&
dm
,
native_socket
sockfd
)
:
super
(
dm
,
sockfd
)
{
// nop
}
expected
<
actor
>
create_newb
(
native_socket
sockfd
,
io
::
network
::
transport_policy_ptr
pol
)
override
{
CAF_LOG_TRACE
(
CAF_ARG
(
sockfd
));
std
::
cout
<<
"tcp_acceptor::creating newb"
<<
std
::
endl
;
auto
n
=
io
::
network
::
make_newb
<
raw_newb
>
(
this
->
backend
().
system
(),
sockfd
);
auto
ptr
=
caf
::
actor_cast
<
caf
::
abstract_actor
*>
(
n
);
if
(
ptr
==
nullptr
)
return
sec
::
runtime_error
;
auto
&
ref
=
dynamic_cast
<
raw_newb
&>
(
*
ptr
);
ref
.
transport
=
std
::
move
(
pol
);
ref
.
protocol
.
reset
(
new
ProtocolPolicy
(
&
ref
));
ref
.
responder
=
responder
;
ref
.
configure_read
(
io
::
receive_policy
::
exactly
(
chunk_size
));
anon_send
(
responder
,
n
);
return
n
;
}
actor
responder
;
};
class
config
:
public
actor_system_config
{
public:
uint16_t
port
=
12345
;
std
::
string
host
=
"127.0.0.1"
;
bool
is_server
=
false
;
config
()
{
opt_group
{
custom_options_
,
"global"
}
.
add
(
port
,
"port,P"
,
"set port"
)
.
add
(
host
,
"host,H"
,
"set host"
)
.
add
(
is_server
,
"server,s"
,
"set server"
);
}
};
struct
state
{
size_t
count
=
0
;
};
void
caf_main
(
actor_system
&
sys
,
const
config
&
cfg
)
{
using
acceptor_t
=
tcp_acceptor
<
tcp_protocol
<
raw_tcp
>>
;
const
char
*
host
=
cfg
.
host
.
c_str
();
const
uint16_t
port
=
cfg
.
port
;
scoped_actor
self
{
sys
};
auto
running
=
[
=
](
event_based_actor
*
self
,
std
::
string
,
actor
m
,
actor
)
->
behavior
{
return
{
[
=
](
quit_atom
)
{
self
->
send
(
m
,
quit_atom
::
value
);
}
};
};
auto
init
=
[
=
](
event_based_actor
*
self
,
std
::
string
name
,
actor
m
)
->
behavior
{
self
->
set_default_handler
(
skip
);
return
{
[
=
](
actor
b
)
{
std
::
cout
<<
"["
<<
name
<<
"] got broker, let's do this"
<<
std
::
endl
;
self
->
become
(
running
(
self
,
name
,
m
,
b
));
self
->
set_default_handler
(
print_and_drop
);
}
};
};
auto
dummy_broker
=
[](
io
::
stateful_broker
<
state
>*
self
)
->
behavior
{
return
{
[
=
](
io
::
new_connection_msg
&
msg
)
{
std
::
cout
<<
"got new connection"
<<
std
::
endl
;
self
->
configure_read
(
msg
.
handle
,
io
::
receive_policy
::
exactly
(
chunk_size
));
},
[
=
](
io
::
new_data_msg
&
)
{
self
->
state
.
count
+=
1
;
if
(
self
->
state
.
count
%
1000
==
0
)
std
::
cout
<<
"received "
<<
self
->
state
.
count
<<
" messages"
<<
std
::
endl
;
}
};
};
auto
name
=
cfg
.
is_server
?
"server"
:
"client"
;
auto
helper
=
sys
.
spawn
(
init
,
name
,
self
);
actor
nb
;
auto
await_done
=
[
&
]()
{
self
->
receive
(
[
&
](
quit_atom
)
{
std
::
cout
<<
"done"
<<
std
::
endl
;
}
);
};
if
(
cfg
.
is_server
)
{
std
::
cout
<<
"creating new server"
<<
std
::
endl
;
auto
server_ptr
=
make_server_newb
<
acceptor_t
,
accept_tcp
>
(
sys
,
port
,
nullptr
,
true
);
// If I don't do this, our newb acceptor will never get events ...
auto
b
=
sys
.
middleman
().
spawn_server
(
dummy_broker
,
port
+
1
);
await_done
();
}
else
{
std
::
cout
<<
"creating new client"
<<
std
::
endl
;
auto
client
=
make_client_newb
<
raw_newb
,
tcp_transport
,
tcp_protocol
<
raw_tcp
>>
(
sys
,
host
,
port
);
self
->
send
(
client
,
responder_atom
::
value
,
helper
);
self
->
send
(
client
,
send_atom
::
value
,
char
(
0
));
self
->
send
(
client
,
interval_atom
::
value
);
await_done
();
}
}
}
// namespace anonymous
CAF_MAIN
(
io
::
middleman
);
examples/measurements/one_raw_udp.cpp
0 → 100644
View file @
94a379c7
#include "caf/io/network/newb.hpp"
#include "caf/logger.hpp"
#include "caf/binary_deserializer.hpp"
#include "caf/binary_serializer.hpp"
#include "caf/detail/call_cfun.hpp"
#include "caf/policy/newb_udp.hpp"
using
namespace
caf
;
using
caf
::
io
::
network
::
default_multiplexer
;
using
caf
::
io
::
network
::
invalid_native_socket
;
using
caf
::
io
::
network
::
make_client_newb
;
using
caf
::
io
::
network
::
make_server_newb
;
using
caf
::
io
::
network
::
native_socket
;
using
caf
::
policy
::
accept_udp
;
using
caf
::
policy
::
udp_protocol
;
using
caf
::
policy
::
udp_transport
;
namespace
{
using
interval_atom
=
atom_constant
<
atom
(
"interval"
)
>
;
using
ordering_atom
=
atom_constant
<
atom
(
"ordering"
)
>
;
using
send_atom
=
atom_constant
<
atom
(
"send"
)
>
;
using
quit_atom
=
atom_constant
<
atom
(
"quit"
)
>
;
using
responder_atom
=
atom_constant
<
atom
(
"responder"
)
>
;
using
start_atom
=
atom_constant
<
atom
(
"start"
)
>
;
using
handshake_atom
=
atom_constant
<
atom
(
"handshake"
)
>
;
constexpr
size_t
chunk_size
=
8192
;
//128; //1024;
struct
new_data
{
char
*
payload
;
size_t
payload_len
;
};
template
<
class
Inspector
>
typename
Inspector
::
result_type
inspect
(
Inspector
&
fun
,
new_data
&
data
)
{
return
fun
(
meta
::
type_name
(
"new_data"
),
data
.
payload_len
);
}
struct
raw_udp
{
using
message_type
=
new_data
;
using
result_type
=
optional
<
message_type
>
;
io
::
network
::
newb
<
message_type
>*
parent
;
message_type
msg
;
raw_udp
(
io
::
network
::
newb
<
message_type
>*
parent
)
:
parent
(
parent
)
{
// nop
}
error
read
(
char
*
bytes
,
size_t
count
)
{
msg
.
payload
=
bytes
;
msg
.
payload_len
=
count
;
parent
->
handle
(
msg
);
return
none
;
}
error
timeout
(
atom_value
,
uint32_t
)
{
return
none
;
}
size_t
write_header
(
io
::
network
::
byte_buffer
&
,
io
::
network
::
header_writer
*
)
{
return
0
;
}
void
prepare_for_sending
(
io
::
network
::
byte_buffer
&
,
size_t
,
size_t
,
size_t
)
{
// nop
}
};
struct
raw_newb
:
public
io
::
network
::
newb
<
new_data
>
{
using
message_type
=
new_data
;
raw_newb
(
caf
::
actor_config
&
cfg
,
default_multiplexer
&
dm
,
native_socket
sockfd
)
:
newb
<
message_type
>
(
cfg
,
dm
,
sockfd
),
running
(
true
),
is_client
(
true
),
interval_counter
(
0
),
received_messages
(
0
),
interval
(
5000
)
{
// nop
CAF_LOG_TRACE
(
""
);
}
void
handle
(
message_type
&
msg
)
override
{
CAF_PUSH_AID_FROM_PTR
(
this
);
CAF_LOG_TRACE
(
""
);
if
(
is_client
)
{
send
(
responder
,
handshake_atom
::
value
);
}
else
if
(
msg
.
payload_len
==
1
)
{
auto
byte
=
*
msg
.
payload
;
if
(
byte
==
'h'
)
std
::
cout
<<
"I'll consider this the handshake"
<<
std
::
endl
;
else
if
(
byte
==
'q'
)
send
(
this
,
quit_atom
::
value
);
send
(
this
,
handshake_atom
::
value
);
}
else
{
received_messages
+=
1
;
if
(
received_messages
%
1000
==
0
)
std
::
cout
<<
"received "
<<
received_messages
<<
" messages"
<<
std
::
endl
;
//std::cout << "received message" << std::endl;
// nop
}
}
behavior
make_behavior
()
override
{
set_default_handler
(
print_and_drop
);
return
{
// Must be implemented at the moment, will be cought by the broker in a
// later implementation.
[
=
](
atom_value
atm
,
uint32_t
id
)
{
protocol
->
timeout
(
atm
,
id
);
},
[
=
](
handshake_atom
)
{
auto
whdl
=
wr_buf
(
nullptr
);
CAF_ASSERT
(
whdl
.
buf
!=
nullptr
);
CAF_ASSERT
(
whdl
.
protocol
!=
nullptr
);
binary_serializer
bs
(
&
backend
(),
*
whdl
.
buf
);
whdl
.
buf
->
push_back
(
'h'
);
},
[
=
](
send_atom
,
char
c
)
{
if
(
running
)
{
delayed_send
(
this
,
interval
,
send_atom
::
value
,
char
((
c
+
1
)
%
256
));
auto
whdl
=
wr_buf
(
nullptr
);
CAF_ASSERT
(
whdl
.
buf
!=
nullptr
);
CAF_ASSERT
(
whdl
.
protocol
!=
nullptr
);
binary_serializer
bs
(
&
backend
(),
*
whdl
.
buf
);
whdl
.
buf
->
resize
(
chunk_size
);
std
::
fill
(
whdl
.
buf
->
begin
(),
whdl
.
buf
->
end
(),
c
);
}
},
[
=
](
responder_atom
,
actor
r
)
{
std
::
cout
<<
"got responder assigned"
<<
std
::
endl
;
responder
=
r
;
send
(
r
,
this
);
},
[
=
](
interval_atom
)
{
if
(
running
)
{
delayed_send
(
this
,
std
::
chrono
::
seconds
(
1
),
interval_atom
::
value
);
data
.
emplace_back
(
interval
,
transport
->
count
,
transport
->
offline_buffer
.
size
());
interval_counter
+=
1
;
if
(
interval_counter
%
10
==
0
)
{
auto
cnt
=
interval
.
count
();
auto
dec
=
cnt
>
1000
?
1000
:
(
cnt
>
100
?
100
:
10
);
interval
-=
std
::
chrono
::
microseconds
(
dec
);
}
transport
->
count
=
0
;
if
(
interval
.
count
()
<=
0
)
running
=
false
;
}
else
{
std
::
map
<
size_t
,
std
::
vector
<
size_t
>>
aggregate
;
for
(
auto
&
t
:
data
)
{
auto
expected
=
(
1000000
/
get
<
0
>
(
t
).
count
());
aggregate
[
expected
].
push_back
(
get
<
1
>
(
t
));
}
for
(
auto
&
p
:
aggregate
)
{
std
::
cerr
<<
p
.
first
;
for
(
auto
v
:
p
.
second
)
std
::
cerr
<<
", "
<<
v
;
std
::
cerr
<<
std
::
endl
;
}
/*
for (auto& t : data)
std::cerr << (1000000 / get<0>(t).count()) << ", "
<< get<1>(t) << ", " << get<2>(t) << std::endl;
*/
send
(
this
,
quit_atom
::
value
);
}
},
[
=
](
quit_atom
)
{
std
::
cout
<<
"got quit message"
<<
std
::
endl
;
// Remove from multiplexer loop.
stop
();
// Quit actor.
quit
();
send
(
responder
,
quit_atom
::
value
);
}
};
}
bool
running
;
bool
is_client
;
actor
responder
;
uint32_t
interval_counter
;
uint32_t
received_messages
;
std
::
chrono
::
microseconds
interval
;
// values: measurement point, current interval, messages sent in interval, offline buffer size
std
::
vector
<
std
::
tuple
<
std
::
chrono
::
microseconds
,
size_t
,
size_t
>>
data
;
};
template
<
class
ProtocolPolicy
>
struct
udp_acceptor
:
public
io
::
network
::
newb_acceptor
<
typename
ProtocolPolicy
::
message_type
>
{
using
super
=
io
::
network
::
newb_acceptor
<
typename
ProtocolPolicy
::
message_type
>
;
udp_acceptor
(
default_multiplexer
&
dm
,
native_socket
sockfd
)
:
super
(
dm
,
sockfd
)
{
// nop
}
~
udp_acceptor
()
{
std
::
cout
<<
"terminating udp acceptor"
<<
std
::
endl
;
}
expected
<
actor
>
create_newb
(
native_socket
sockfd
,
io
::
network
::
transport_policy_ptr
pol
)
override
{
CAF_LOG_TRACE
(
CAF_ARG
(
sockfd
));
std
::
cout
<<
"creating newb"
<<
std
::
endl
;
auto
n
=
io
::
network
::
make_newb
<
raw_newb
>
(
this
->
backend
().
system
(),
sockfd
);
auto
ptr
=
caf
::
actor_cast
<
caf
::
abstract_actor
*>
(
n
);
if
(
ptr
==
nullptr
)
return
sec
::
runtime_error
;
auto
&
ref
=
dynamic_cast
<
raw_newb
&>
(
*
ptr
);
ref
.
transport
=
std
::
move
(
pol
);
ref
.
protocol
.
reset
(
new
ProtocolPolicy
(
&
ref
));
ref
.
responder
=
responder
;
// Read first message from this socket
ref
.
is_client
=
false
;
ref
.
transport
->
prepare_next_read
(
this
);
ref
.
transport
->
read_some
(
this
,
*
ref
.
protocol
.
get
());
// TODO: Just a workaround.
anon_send
(
responder
,
n
);
return
n
;
}
actor
responder
;
};
class
config
:
public
actor_system_config
{
public:
uint16_t
port
=
12345
;
std
::
string
host
=
"127.0.0.1"
;
bool
is_server
=
false
;
config
()
{
opt_group
{
custom_options_
,
"global"
}
.
add
(
port
,
"port,P"
,
"set port"
)
.
add
(
host
,
"host,H"
,
"set host"
)
.
add
(
is_server
,
"server,s"
,
"set server"
);
}
};
void
caf_main
(
actor_system
&
sys
,
const
config
&
cfg
)
{
using
acceptor_t
=
udp_acceptor
<
udp_protocol
<
raw_udp
>>
;
const
char
*
host
=
cfg
.
host
.
c_str
();
const
uint16_t
port
=
cfg
.
port
;
scoped_actor
self
{
sys
};
auto
running
=
[
=
](
event_based_actor
*
self
,
std
::
string
name
,
actor
m
,
actor
)
->
behavior
{
return
{
[
=
](
handshake_atom
)
{
std
::
cout
<<
"["
<<
name
<<
"] got server"
<<
std
::
endl
;
self
->
send
(
m
,
quit_atom
::
value
);
},
[
=
](
quit_atom
)
{
self
->
send
(
m
,
quit_atom
::
value
);
}
};
};
auto
init
=
[
=
](
event_based_actor
*
self
,
std
::
string
name
,
actor
m
)
->
behavior
{
self
->
set_default_handler
(
skip
);
return
{
[
=
](
actor
b
)
{
std
::
cout
<<
"["
<<
name
<<
"] got broker, let's do this"
<<
std
::
endl
;
self
->
become
(
running
(
self
,
name
,
m
,
b
));
self
->
set_default_handler
(
print_and_drop
);
}
};
};
auto
dummy_broker
=
[](
io
::
broker
*
)
->
behavior
{
return
{
[](
io
::
new_connection_msg
&
)
{
std
::
cout
<<
"got new connection"
<<
std
::
endl
;
}
};
};
auto
name
=
cfg
.
is_server
?
"server"
:
"client"
;
auto
helper
=
sys
.
spawn
(
init
,
name
,
self
);
actor
nb
;
auto
await_done
=
[
&
](
std
::
string
msg
)
{
self
->
receive
(
[
&
](
quit_atom
)
{
std
::
cout
<<
msg
<<
std
::
endl
;
}
);
};
if
(
cfg
.
is_server
)
{
std
::
cout
<<
"creating new server"
<<
std
::
endl
;
auto
server_ptr
=
make_server_newb
<
acceptor_t
,
accept_udp
>
(
sys
,
port
,
nullptr
,
true
);
// If I don't do this, our newb acceptor will never get events ...
auto
b
=
sys
.
middleman
().
spawn_server
(
dummy_broker
,
port
+
1
);
await_done
(
"done"
);
}
else
{
std
::
cout
<<
"creating new client"
<<
std
::
endl
;
auto
client
=
make_client_newb
<
raw_newb
,
udp_transport
,
udp_protocol
<
raw_udp
>>
(
sys
,
host
,
port
);
self
->
send
(
client
,
responder_atom
::
value
,
helper
);
self
->
send
(
client
,
handshake_atom
::
value
);
await_done
(
"let's start"
);
self
->
send
(
client
,
send_atom
::
value
,
char
(
0
));
self
->
send
(
client
,
interval_atom
::
value
);
await_done
(
"done"
);
std
::
abort
();
}
}
}
// namespace anonymous
CAF_MAIN
(
io
::
middleman
);
examples/remoting/basp_udp_newb.cpp
View file @
94a379c7
...
...
@@ -26,14 +26,14 @@ using responder_atom = atom_constant<atom("responder")>;
// -- udp impls ----------------------------------------------------------------
struct
ud
p_header
{
struct
bas
p_header
{
uint32_t
payload_len
;
actor_id
from
;
actor_id
to
;
};
template
<
class
Inspector
>
typename
Inspector
::
result_type
inspect
(
Inspector
&
fun
,
ud
p_header
&
hdr
)
{
typename
Inspector
::
result_type
inspect
(
Inspector
&
fun
,
bas
p_header
&
hdr
)
{
return
fun
(
meta
::
type_name
(
"basp_header"
),
hdr
.
payload_len
,
hdr
.
from
,
hdr
.
to
);
}
...
...
@@ -54,7 +54,7 @@ typename Inspector::result_type inspect(Inspector& fun, ordering_header& hdr) {
constexpr
size_t
udp_ordering_header_len
=
sizeof
(
sequence_type
);
struct
new_basp_message
{
ud
p_header
header
;
bas
p_header
header
;
char
*
payload
;
size_t
payload_len
;
};
...
...
@@ -267,7 +267,7 @@ struct basp_newb : public io::network::newb<new_basp_message> {
[
=
](
send_atom
,
actor_id
sender
,
actor_id
receiver
,
std
::
string
payload
)
{
auto
hw
=
caf
::
make_callback
([
&
](
io
::
network
::
byte_buffer
&
buf
)
->
error
{
binary_serializer
bs
(
&
backend
(),
buf
);
bs
(
ud
p_header
{
0
,
sender
,
receiver
});
bs
(
bas
p_header
{
0
,
sender
,
receiver
});
return
none
;
});
auto
whdl
=
wr_buf
(
&
hw
);
...
...
examples/remoting/raw_tcp_newb.cpp
View file @
94a379c7
...
...
@@ -38,6 +38,7 @@ struct raw_tcp {
raw_tcp
(
io
::
network
::
newb
<
message_type
>*
parent
)
:
parent
(
parent
)
{
// nop
parent
->
configure_read
(
io
::
receive_policy
::
exactly
(
1000
));
}
error
read
(
char
*
bytes
,
size_t
count
)
{
...
...
@@ -80,9 +81,7 @@ struct raw_newb : public io::network::newb<new_data> {
void
handle
(
message_type
&
msg
)
override
{
CAF_PUSH_AID_FROM_PTR
(
this
);
CAF_LOG_TRACE
(
""
);
std
::
string
res
;
binary_deserializer
bd
(
&
backend
(),
msg
.
payload
,
msg
.
payload_len
);
bd
(
res
);
char
res
=
*
msg
.
payload
;
send
(
responder
,
res
);
}
...
...
@@ -94,15 +93,12 @@ struct raw_newb : public io::network::newb<new_data> {
[
=
](
atom_value
atm
,
uint32_t
id
)
{
protocol
->
timeout
(
atm
,
id
);
},
[
=
](
send_atom
,
std
::
string
payload
)
{
[
=
](
send_atom
,
char
c
)
{
auto
whdl
=
wr_buf
(
nullptr
);
CAF_ASSERT
(
whdl
.
buf
!=
nullptr
);
CAF_ASSERT
(
whdl
.
protocol
!=
nullptr
);
binary_serializer
bs
(
&
backend
(),
*
whdl
.
buf
);
auto
from
=
whdl
.
buf
->
size
();
whdl
.
buf
->
resize
(
1000
);
std
::
fill
(
whdl
.
buf
->
begin
()
+
from
,
whdl
.
buf
->
end
(),
0
);
bs
(
payload
);
std
::
fill
(
whdl
.
buf
->
begin
(),
whdl
.
buf
->
end
(),
c
);
},
[
=
](
responder_atom
,
actor
r
)
{
aout
(
this
)
<<
"got responder assigned"
<<
std
::
endl
;
...
...
@@ -168,12 +164,12 @@ void caf_main(actor_system& sys, const actor_system_config&) {
auto
running
=
[
=
](
event_based_actor
*
self
,
std
::
string
name
,
actor
,
actor
b
)
->
behavior
{
return
{
[
=
](
std
::
string
str
)
{
aout
(
self
)
<<
"["
<<
name
<<
"] received '"
<<
str
<<
"'"
<<
std
::
endl
;
[
=
](
char
c
)
{
aout
(
self
)
<<
"["
<<
name
<<
"] received '"
<<
c
<<
"'"
<<
std
::
endl
;
},
[
=
](
send_atom
,
std
::
string
str
)
{
aout
(
self
)
<<
"["
<<
name
<<
"] sending '"
<<
str
<<
"'"
<<
std
::
endl
;
self
->
send
(
b
,
send_atom
::
value
,
self
->
id
(),
actor_id
{},
str
);
[
=
](
send_atom
,
char
c
)
{
aout
(
self
)
<<
"["
<<
name
<<
"] sending '"
<<
c
<<
"'"
<<
std
::
endl
;
self
->
send
(
b
,
send_atom
::
value
,
c
);
},
};
};
...
...
@@ -202,29 +198,14 @@ void caf_main(actor_system& sys, const actor_system_config&) {
tcp_protocol
<
raw_tcp
>>
(
sys
,
host
,
port
);
self
->
send
(
client
,
responder_atom
::
value
,
client_helper
);
self
->
send
(
client_helper
,
send_atom
::
value
,
"hallo"
);
self
->
send
(
server_helper
,
send_atom
::
value
,
"hallo"
);
self
->
send
(
client_helper
,
send_atom
::
value
,
'a'
);
self
->
send
(
server_helper
,
send_atom
::
value
,
'b'
);
self
->
receive
(
[
&
](
quit_atom
)
{
aout
(
self
)
<<
"check"
<<
std
::
endl
;
}
);
/*
main_actor->receive(
[](quit_atom) {
CAF_LOG_DEBUG("check");
}
);
CAF_LOG_DEBUG("shutting everything down");
newb_acceptor_ptr->stop();
anon_send(newb_actor, quit_atom::value);
anon_send(helper_actor, quit_atom::value);
anon_send(test_broker, quit_atom::value);
sys.await_all_actors_done();
CAF_LOG_DEBUG("done");
*/
}
}
// namespace anonymous
...
...
Write
Preview
Markdown
is supported
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