int main(int argc, char ** argv) { CompleteSetupSystem css; PrintExampleDescription(); // Let's enable a bit of debug-output, just to see what the server is doing SetConsoleLogLevel(MUSCLE_LOG_DEBUG); // This object contains our server's event loop. ReflectServer reflectServer; // This factory will create a StorageReflectSession object whenever // a TCP connection is received on SMART_SERVER_TCP_PORT, and // attach the StorageReflectSession to the ReflectServer for use. StorageReflectSessionFactory smartSessionFactory; if (reflectServer.PutAcceptFactory(SMART_SERVER_TCP_PORT, ReflectSessionFactoryRef(&smartSessionFactory, false)) != B_NO_ERROR) { LogTime(MUSCLE_LOG_CRITICALERROR, "Couldn't bind to TCP port %u! (Perhaps a copy of this program is already running?\n", SMART_SERVER_TCP_PORT); return 5; } // This UDP session will handle the UDP ping pong games UDPPingPongSession udpPingPong; if (reflectServer.AddNewSession(AbstractReflectSessionRef(&udpPingPong, false)) != B_NO_ERROR) { LogTime(MUSCLE_LOG_CRITICALERROR, "Couldn't add UDP ping pong session!\n"); return 5; } LogTime(MUSCLE_LOG_INFO, "example_7_smart_server_wth_udp_pingpong is listening for incoming TCP connections on port %u\n", SMART_SERVER_TCP_PORT); LogTime(MUSCLE_LOG_INFO, "Try running one or more instances of example_5_smart_client to connect/chat/subscribe!\n"); LogTime(MUSCLE_LOG_INFO, "\n"); // Our server's event loop will run here -- ServerProcessLoop() return until it's time for the server to exit if (reflectServer.ServerProcessLoop() == B_NO_ERROR) { LogTime(MUSCLE_LOG_INFO, "example_7_smart_server_wth_udp_pingpong is exiting normally.\n"); } else LogTime(MUSCLE_LOG_ERROR, "example_7_smart_server_wth_udp_pingpong is exiting due to an error.\n"); // Make sure our server lets go of all of its sessions and factories // before they are destroyed (necessary only because we may have // allocated some of them on the stack rather than on the heap) reflectServer.Cleanup(); return 0; }
int main(int /*argc*/, char ** /*argv*/) { CompleteSetupSystem css; // set up our environment ReflectServer server; TestSession testSession; // detects config changes and computer sleeps/wakes SomeOtherSession otherSession; // just to verify that the callbacks get called on other sessions also if ((server.AddNewSession(AbstractReflectSessionRef(&testSession, false)) == B_NO_ERROR)&&(server.AddNewSession(AbstractReflectSessionRef(&otherSession, false)) == B_NO_ERROR)) { LogTime(MUSCLE_LOG_INFO, "Beginning Network-Configuration-Change-Detector test... try changing your network config, or plugging/unplugging an Ethernet cable, or putting your computer to sleep.\n"); if (server.ServerProcessLoop() == B_NO_ERROR) LogTime(MUSCLE_LOG_INFO, "testnetconfigdetect event loop exiting.\n"); else LogTime(MUSCLE_LOG_CRITICALERROR, "testnetconfigdetect event loop exiting with an error condition.\n"); } server.Cleanup(); return 0; }
int main(int argc, char ** argv) { CompleteSetupSystem css; PrintExampleDescription(); // This object contains our server's event loop. ReflectServer reflectServer; // This factory will create a DumbReflectSession object whenever // a TCP connection is received on DUMB_SERVER_TCP_PORT, and // attach the DumbReflectSession to the ReflectServer for use. MyDumbReflectSessionFactory dumbSessionFactory; if (reflectServer.PutAcceptFactory(DUMB_SERVER_TCP_PORT, ReflectSessionFactoryRef(&dumbSessionFactory, false)) != B_NO_ERROR) { LogTime(MUSCLE_LOG_CRITICALERROR, "Couldn't bind to TCP port %u! (Perhaps a copy of this program is already running?\n", DUMB_SERVER_TCP_PORT); return 5; } // Here's where our server will spend all of its time LogTime(MUSCLE_LOG_INFO, "example_3_annotated_dumb_server is listening for incoming TCP connections on port %u\n", DUMB_SERVER_TCP_PORT); LogTime(MUSCLE_LOG_INFO, "Try running one or more instances of example_2_dumb_client to connect and chat!\n"); printf("\n"); // Our server's event loop will run here -- ServerProcessLoop() return until it's time for the server to exit if (reflectServer.ServerProcessLoop() == B_NO_ERROR) { LogTime(MUSCLE_LOG_INFO, "example_3_annotated_dumb_server is exiting normally.\n"); } else LogTime(MUSCLE_LOG_ERROR, "example_3_annotated_dumb_server is exiting due to an error.\n"); // Make sure our server lets go of all of its sessions and factories // before they are destroyed (necessary only because we may have // allocated some of them on the stack rather than on the heap) reflectServer.Cleanup(); return 0; }
int main(int argc, char ** argv) { CompleteSetupSystem css; PrintExampleDescription(); // Let's enable a bit of debug-output, just to see what the client is doing SetConsoleLogLevel(MUSCLE_LOG_DEBUG); ReflectServer reflectServer; // Still using a DumbReflectSession here since all we need is Message-forwarding. // (All of the client's "smarts" will be implemented in the MySmartStdinSession class) MyTCPSession tcpSession; if (reflectServer.AddNewConnectSession(AbstractReflectSessionRef(&tcpSession, false), localhostIP, SMART_SERVER_TCP_PORT, SecondsToMicros(1)) != B_NO_ERROR) { LogTime(MUSCLE_LOG_CRITICALERROR, "Couldn't add tcpSession to the client, aborting!\n"); return 10; } // Set up this client's subscription { MessageRef subscribeToNodesMsg = GetMessageFromPool(PR_COMMAND_SETPARAMETERS); // Our filter to only match Messages whose "User String" field contains \"magic\" StringQueryFilter sqf("User String", StringQueryFilter::OP_CONTAINS_IGNORECASE, "magic"); if (subscribeToNodesMsg()->AddArchiveMessage("SUBSCRIBE:/*/*/*", sqf) != B_NO_ERROR) { LogTime(MUSCLE_LOG_ERROR, "Couldn't add StringQueryFilter to subscribe Message, aborting!\n"); return 10; } // Send off our subscription request LogTime(MUSCLE_LOG_INFO, "Sending StringQueryFiltered-subscription request message to server:\n"); subscribeToNodesMsg()->PrintToStream(); // Send off our subscription request if (tcpSession.AddOutgoingMessage(subscribeToNodesMsg) != B_NO_ERROR) { LogTime(MUSCLE_LOG_ERROR, "Couldn't send filtered-subscription Message, aborting!\n"); return 10; } } LogTime(MUSCLE_LOG_INFO, "This program is designed to be run in conjunction with example_4_smart_server\n"); LogTime(MUSCLE_LOG_INFO, "Run this program, then run another smart client (e.g. example_5_smart_client)\n"); LogTime(MUSCLE_LOG_INFO, "in another Terminal window, and start creating nodes with the other smart client\n"); LogTime(MUSCLE_LOG_INFO, "by typing commands like these into the example_5_smart_client's Terminal window:\n"); LogTime(MUSCLE_LOG_INFO, " set node1 = foo\n"); LogTime(MUSCLE_LOG_INFO, " set node2 = magic foo\n"); LogTime(MUSCLE_LOG_INFO, " set node3 = bar\n"); LogTime(MUSCLE_LOG_INFO, " set node4 = magic bar\n"); LogTime(MUSCLE_LOG_INFO, " delete node*\n"); LogTime(MUSCLE_LOG_INFO, "... and note that this client ONLY gets updates about nodes whose contents contain \"magic\"!\n"); LogTime(MUSCLE_LOG_INFO, "(Other nodes don't match the QueryFilter we supplied and thus don't exist as far as our\n"); LogTime(MUSCLE_LOG_INFO, "subscription is concerned)\n"); printf("\n"); if (reflectServer.ServerProcessLoop() == B_NO_ERROR) { LogTime(MUSCLE_LOG_INFO, "eexample_2_smart_client_with_queryfilter is exiting normally.\n"); } else LogTime(MUSCLE_LOG_ERROR, "example_2_smart_client_with_queryfilter is exiting due to an error.\n"); // Make sure our server lets go of all of its sessions // before they are destroyed (necessary only because we have // allocated some of them on the stack rather than on the heap) reflectServer.Cleanup(); return 0; }