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
ab34ff1b
Commit
ab34ff1b
authored
Aug 09, 2012
by
Dominik Charousset
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
moved behavior definition of ping & pong to factory functions
parent
5e602a8b
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
48 additions
and
76 deletions
+48
-76
unit_testing/ping_pong.cpp
unit_testing/ping_pong.cpp
+48
-76
No files found.
unit_testing/ping_pong.cpp
View file @
ab34ff1b
...
@@ -6,104 +6,76 @@
...
@@ -6,104 +6,76 @@
#include "cppa/sb_actor.hpp"
#include "cppa/sb_actor.hpp"
#include "cppa/to_string.hpp"
#include "cppa/to_string.hpp"
namespace
{
size_t
s_pongs
=
0
;
}
using
std
::
cout
;
using
std
::
cout
;
using
std
::
endl
;
using
std
::
endl
;
using
namespace
cppa
;
using
namespace
cppa
;
size_t
pongs
()
{
namespace
{
return
s_pongs
;
}
void
ping
(
size_t
num_pings
)
{
size_t
s_pongs
=
0
;
s_pongs
=
0
;
do_receive
(
behavior
ping_behavior
(
size_t
num_pings
)
{
on
<
atom
(
"pong"
),
int
>
()
>>
[
&
](
int
value
)
{
return
on
(
atom
(
"pong"
),
arg_match
)
>>
[
num_pings
](
int
value
)
{
//cout << to_string(self->last_dequeued()) << endl;
//cout << to_string(self->last_dequeued()) << endl;
if
(
++
s_pongs
=
=
num_pings
)
{
if
(
++
s_pongs
>
=
num_pings
)
{
reply
(
atom
(
"EXIT"
),
exit_reason
::
user_defined
);
reply
(
atom
(
"EXIT"
),
exit_reason
::
user_defined
);
self
->
quit
();
}
}
else
{
else
{
reply
(
atom
(
"ping"
),
value
);
reply
(
atom
(
"ping"
),
value
);
}
}
},
},
others
()
>>
[]()
{
others
()
>>
[]
{
cout
<<
"unexpected message; "
cout
<<
"unexpected message; "
<<
__FILE__
<<
" line "
<<
__LINE__
<<
": "
<<
__FILE__
<<
" line "
<<
__LINE__
<<
": "
<<
to_string
(
self
->
last_dequeued
())
<<
endl
;
<<
to_string
(
self
->
last_dequeued
())
<<
endl
;
self
->
quit
(
exit_reason
::
user_defined
);
self
->
quit
(
exit_reason
::
user_defined
);
}
};
)
.
until
(
gref
(
s_pongs
)
==
num_pings
);
}
}
actor_ptr
spawn_event_based_ping
(
size_t
num_pings
)
{
behavior
pong_behavior
()
{
s_pongs
=
0
;
return
on
<
atom
(
"ping"
),
int
>
()
>>
[](
int
value
)
{
struct
impl
:
public
sb_actor
<
impl
>
{
behavior
init_state
;
impl
(
size_t
num_pings
)
{
init_state
=
(
on
<
atom
(
"pong"
),
int
>
()
>>
[
num_pings
,
this
](
int
value
)
{
//cout << to_string(self->last_dequeued()) << endl;
//cout << to_string(self->last_dequeued()) << endl;
if
(
++
s_pongs
>=
num_pings
)
{
reply
(
atom
(
"pong"
),
value
+
1
);
reply
(
atom
(
"EXIT"
),
exit_reason
::
user_defined
);
quit
();
}
else
{
reply
(
atom
(
"ping"
),
value
);
}
},
},
others
()
>>
[]()
{
others
()
>>
[]()
{
cout
<<
"unexpected message; "
cout
<<
"unexpected message; "
<<
__FILE__
<<
" line "
<<
__LINE__
<<
": "
<<
__FILE__
<<
" line "
<<
__LINE__
<<
": "
<<
to_string
(
self
->
last_dequeued
())
<<
endl
;
<<
to_string
(
self
->
last_dequeued
())
<<
endl
;
self
->
quit
(
exit_reason
::
user_defined
);
self
->
quit
(
exit_reason
::
user_defined
);
}
);
}
};
};
return
spawn
<
impl
>
(
num_pings
);
}
}
// namespace <anonymous>
size_t
pongs
()
{
return
s_pongs
;
}
void
ping
(
size_t
num_pings
)
{
s_pongs
=
0
;
receive_loop
(
ping_behavior
(
num_pings
));
}
actor_ptr
spawn_event_based_ping
(
size_t
num_pings
)
{
s_pongs
=
0
;
return
factory
::
event_based
(
[
num_pings
]
{
self
->
become
(
ping_behavior
(
num_pings
));
}
).
spawn
();
}
}
void
pong
(
actor_ptr
ping_actor
)
{
void
pong
(
actor_ptr
ping_actor
)
{
// kickoff
// kickoff
send
(
ping_actor
,
atom
(
"pong"
),
0
);
send
(
ping_actor
,
atom
(
"pong"
),
0
);
receive_loop
(
receive_loop
(
pong_behavior
());
on
<
atom
(
"ping"
),
int
>
()
>>
[](
int
value
)
{
//cout << to_string(self->last_dequeued()) << endl;
reply
(
atom
(
"pong"
),
value
+
1
);
},
others
()
>>
[]()
{
cout
<<
"unexpected message; "
<<
__FILE__
<<
" line "
<<
__LINE__
<<
": "
<<
to_string
(
self
->
last_dequeued
())
<<
endl
;
self
->
quit
(
exit_reason
::
user_defined
);
}
);
}
}
actor_ptr
spawn_event_based_pong
(
actor_ptr
ping_actor
)
{
actor_ptr
spawn_event_based_pong
(
actor_ptr
ping_actor
)
{
CPPA_REQUIRE
(
ping_actor
.
get
()
!=
nullptr
);
CPPA_REQUIRE
(
ping_actor
.
get
()
!=
nullptr
);
struct
impl
:
public
sb_actor
<
impl
>
{
return
factory
::
event_based
([
=
]
{
behavior
init_state
;
self
->
become
(
pong_behavior
());
impl
()
{
send
(
ping_actor
,
atom
(
"pong"
),
0
);
init_state
=
(
}).
spawn
();
on
<
atom
(
"ping"
),
int
>
()
>>
[](
int
value
)
{
//cout << to_string(self->last_dequeued()) << endl;
reply
(
atom
(
"pong"
),
value
+
1
);
},
others
()
>>
[]()
{
cout
<<
"unexpected message; "
<<
__FILE__
<<
" line "
<<
__LINE__
<<
": "
<<
to_string
(
self
->
last_dequeued
())
<<
endl
;
self
->
quit
(
exit_reason
::
user_defined
);
}
);
}
};
auto
pptr
=
spawn
<
impl
>
();
// kickoff
ping_actor
->
enqueue
(
pptr
.
get
(),
make_any_tuple
(
atom
(
"pong"
),
0
));
return
pptr
;
}
}
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