Skip to content
Browse files

demo app implements ping and pong

  • Loading branch information...
1 parent 0a8689f commit beb6f74daa3847b81dc593d140dfbc80f50ea8e2 @RJ committed
Showing with 127 additions and 85 deletions.
  1. +12 −0 app/README.txt
  2. +44 −0 app/demo_messages.h
  3. +64 −0 app/demo_protocol.h
  4. +5 −4 app/main.cpp
  5. +0 −74 include/libf2f/message.h
  6. +0 −5 src/protocol.cpp
  7. +2 −2 src/router.cpp
View
12 app/README.txt
@@ -0,0 +1,12 @@
+A basic app using libf2f, which implements a ping and pong message.
+
+To test, in console 1:
+$ bin/f2f-demo 5555
+
+In console 2:
+$ bin/f2f-demo 5556
+> connect 127.0.0.1 5555
+...
+> pingall
+
+This will run two servents, the second one will ping and the first will pong.
View
44 app/demo_messages.h
@@ -0,0 +1,44 @@
+#ifndef __LIBF2F_DEMO_MESSAGE_H__
+#define __LIBF2F_DEMO_MESSAGE_H__
+
+#include "libf2f/message.h"
+
+using namespace libf2f;
+
+#define PING 0
+#define PONG 1
+
+class PongMessage : public Message
+{
+public:
+ PongMessage()
+ {
+ message_header h;
+ memcpy( &h.guid, std::string(GENUUID).data(), 36 );
+ h.type = PONG;
+ h.ttl = 1;
+ h.hops = 0;
+ h.length = 0;
+ m_header = h;
+ m_payload = 0;
+ }
+};
+
+class PingMessage : public Message
+{
+public:
+ PingMessage()
+ {
+ message_header h;
+ memcpy( &h.guid, std::string(GENUUID).data(), 36 );
+ h.type = PING;
+ h.ttl = 1;
+ h.hops = 0;
+ h.length = 0;
+ m_header = h;
+ m_payload = 0;
+ }
+};
+
+#endif
+
View
64 app/demo_protocol.h
@@ -0,0 +1,64 @@
+#ifndef __F2FDEMO_PROTOCOL_H__
+#define __F2FDEMO_PROTOCOL_H__
+
+#include "libf2f/protocol.h"
+
+using namespace libf2f;
+
+class DemoProtocol : public Protocol
+{
+public:
+ DemoProtocol()
+ {
+ }
+
+ virtual ~DemoProtocol()
+ {
+ }
+
+ /// called when a client connects to us
+ virtual bool new_incoming_connection( connection_ptr conn )
+ {
+ std::cout << "DemoProtocol::new_incoming_connection "
+ << conn->str() << std::endl;
+ return true; // returning false rejects the connection
+ }
+
+ /// called when we opened a socket to a remote servent
+ virtual void new_outgoing_connection( connection_ptr conn )
+ {
+ std::cout << "DemoProtocol::new_outgoing_connection "
+ << conn->str() << std::endl;
+ }
+
+ /// called on a disconnection, for whatever reason
+ virtual void connection_terminated( connection_ptr conn )
+ {
+ std::cout << "Connection terminated!" << std::endl;
+ }
+
+ /// we received a msg from this connection
+ virtual void message_received( message_ptr msgp, connection_ptr conn )
+ {
+ std::cout << "DemoProtocol::message_received "
+ << conn->str() << std::endl
+ << msgp->str() << std::endl;
+ switch( msgp->type() )
+ {
+ case PING:
+ std::cout << "Got a ping, replying with a pong." << std::endl;
+ conn->async_write( message_ptr(new PongMessage()) );
+ break;
+ case PONG:
+ std::cout << "Got a pong, yay!" << std::endl;
+ break;
+ default:
+ std::cout << "Unhandled message type: "
+ << msgp->type() << std::endl;
+ }
+ }
+
+};
+
+#endif
+
View
9 app/main.cpp
@@ -4,12 +4,13 @@
#include <boost/asio.hpp>
#include <boost/thread/thread.hpp>
#include <boost/algorithm/string.hpp>
-#include "boost/lambda/lambda.hpp"
-#include <boost/lambda/if.hpp>
#include "libf2f/router.h"
#include "libf2f/protocol.h"
+#include "demo_messages.h"
+#include "demo_protocol.h"
+
using namespace std;
using namespace libf2f;
@@ -30,7 +31,7 @@ int main(int argc, char **argv)
boost::asio::io_service ios;
- Protocol p;
+ DemoProtocol p;
short port = atoi(argv[1]);
cout << "Listening on port " << port << endl;
boost::shared_ptr<boost::asio::ip::tcp::acceptor> accp(
@@ -85,4 +86,4 @@ int main(int argc, char **argv)
r.stop();
t.join();
return 0;
-}
+}
View
74 include/libf2f/message.h
@@ -9,17 +9,6 @@
#define GENUUID "266695BF-AC15-4991-A01D-21DC180FD4B1"
#endif
-#define PING 0
-#define PONG 1
-#define IDENT 2
-#define QUERY 3
-#define QUERYRESULT 4
-#define QUERYCANCEL 5
-#define SIDREQUEST 6
-#define SIDDATA 7
-#define SIDCANCEL 8
-#define BYE 9
-
namespace libf2f {
class Message;
@@ -150,42 +139,6 @@ class Message
char * m_payload;
};
-
-
-class PongMessage : public Message
-{
-public:
- PongMessage()
- {
- message_header h;
- memcpy( &h.guid, std::string(GENUUID).data(), 36 );
- //h.guid = GENUUID;
- h.type = PONG;
- h.ttl = 1;
- h.hops = 0;
- h.length = 0;
- m_header = h;
- m_payload = 0;
- }
-};
-
-class PingMessage : public Message
-{
-public:
- PingMessage()
- {
- message_header h;
- memcpy( &h.guid, std::string(GENUUID).data(), 36 );
- //h.guid = GENUUID;
- h.type = PING;
- h.ttl = 1;
- h.hops = 0;
- h.length = 0;
- m_header = h;
- m_payload = 0;
- }
-};
-
class GeneralMessage : public Message
{
public:
@@ -204,33 +157,6 @@ class GeneralMessage : public Message
}
};
-/*
-
-
-/// File transfer/streaming msgs are special in that they have an additional header describing the payload (just the sid)
-class DataMessage : public Message
-{
-public:
-
- /// Grab the SID from the first 36 bytes of the payload:
- const std::string& sid() const
- {
- if( m_sid.empty() )
- {
- m_sid = "TODO"; //std::string( payload(), 36 );
- }
- return m_sid;
- }
-
- const boost::asio::const_buffer payload_data() const
- {
- return boost::asio::const_buffer( payload()+36, length()-36 );
- }
-
-private:
- mutable std::string m_sid;
-};
-*/
}
#endif
View
5 src/protocol.cpp
@@ -13,10 +13,6 @@ bool
Protocol::new_incoming_connection( connection_ptr conn )
{
cout << "Protocol::new_incoming_connection " << conn->str() << endl;
- conn->async_write( message_ptr(new PingMessage()) );
- // first thing to expect is an ident msg, so set the msg handler to one
- // that expects it, and kills the connection otherwise:
-
return true;
}
@@ -24,7 +20,6 @@ void
Protocol::new_outgoing_connection( connection_ptr conn )
{
cout << "Protocol::new_outgoing_connection " << conn->str() << endl;
- conn->async_write( message_ptr(new PingMessage()) );
}
void
View
4 src/router.cpp
@@ -137,7 +137,7 @@ Router::message_received( message_ptr msgp, connection_ptr conn )
cout << "Dropping, msg length: " << msgp->length() << endl;
return;
}
-
+ /*
// handle ping
if( msgp->type() == PING )
{
@@ -152,7 +152,7 @@ Router::message_received( message_ptr msgp, connection_ptr conn )
cout << "got PONG" << endl;
return;
}
-
+ */
m_protocol->message_received( msgp, conn );
}

0 comments on commit beb6f74

Please sign in to comment.
Something went wrong with that request. Please try again.