mirror of
				https://github.com/pocoproject/poco.git
				synced 2025-10-27 02:53:10 +01:00 
			
		
		
		
	simplified the Ping sample
This commit is contained in:
		| @@ -59,7 +59,7 @@ using Poco::Net::IPAddress; | ||||
| using Poco::Net::ICMPEventArgs; | ||||
| using Poco::AutoPtr; | ||||
| using Poco::NumberParser; | ||||
| using Poco::Delegate; | ||||
| using Poco::delegate; | ||||
|  | ||||
|  | ||||
| class Ping: public Application | ||||
| @@ -84,18 +84,21 @@ protected: | ||||
| 		loadConfiguration(); // load default configuration files, if present | ||||
| 		Application::initialize(self); | ||||
| 		 | ||||
| 		_icmpClient.pingBegin += Delegate<Ping, ICMPEventArgs>(this, &Ping::onBegin); | ||||
| 		_icmpClient.pingReply += Delegate<Ping, ICMPEventArgs>(this, &Ping::onReply); | ||||
| 		_icmpClient.pingError += Delegate<Ping, ICMPEventArgs>(this, &Ping::onError); | ||||
| 		_icmpClient.pingEnd   += Delegate<Ping, ICMPEventArgs>(this, &Ping::onEnd); | ||||
| 		// Note: Handlers are static member functions in this example. | ||||
| 		// A non-static handler would require this pointer explicitly | ||||
| 		// specified as the first argument, e.g. delegate(this, &Ping::onBegin); | ||||
| 		_icmpClient.pingBegin += delegate(&Ping::onBegin); | ||||
| 		_icmpClient.pingReply += delegate(&Ping::onReply); | ||||
| 		_icmpClient.pingError += delegate(&Ping::onError); | ||||
| 		_icmpClient.pingEnd   += delegate(&Ping::onEnd); | ||||
| 	} | ||||
| 	 | ||||
| 	void uninitialize() | ||||
| 	{ | ||||
| 		_icmpClient.pingBegin -= Delegate<Ping, ICMPEventArgs>(this, &Ping::onBegin); | ||||
| 		_icmpClient.pingReply -= Delegate<Ping, ICMPEventArgs>(this, &Ping::onReply); | ||||
| 		_icmpClient.pingError -= Delegate<Ping, ICMPEventArgs>(this, &Ping::onError); | ||||
| 		_icmpClient.pingEnd   -= Delegate<Ping, ICMPEventArgs>(this, &Ping::onEnd); | ||||
| 		_icmpClient.pingBegin -= delegate(&Ping::onBegin); | ||||
| 		_icmpClient.pingReply -= delegate(&Ping::onReply); | ||||
| 		_icmpClient.pingError -= delegate(&Ping::onError); | ||||
| 		_icmpClient.pingEnd   -= delegate(&Ping::onEnd); | ||||
|  | ||||
| 		Application::uninitialize(); | ||||
| 	} | ||||
| @@ -157,42 +160,38 @@ protected: | ||||
| 	} | ||||
|  | ||||
|  | ||||
| 	void onBegin(const void* pSender, ICMPEventArgs& args) | ||||
| 	static void onBegin(ICMPEventArgs& args) | ||||
| 	{ | ||||
| 		std::ostringstream os; | ||||
| 		os << "Pinging " << args.hostName() << " [" << args.hostAddress() << "] with " << args.dataSize() << " bytes of data:"  | ||||
| 		   << std::endl << "---------------------------------------------" << std::endl; | ||||
| 		logger().information(os.str()); | ||||
| 		std::cout << "Pinging " << args.hostName()  | ||||
| 			<< " [" << args.hostAddress() << "] with "  | ||||
| 			<< args.dataSize() << " bytes of data:"  | ||||
| 			<< std::endl << "---------------------------------------------" | ||||
| 			<< std::endl; | ||||
| 	} | ||||
|  | ||||
| 	void onReply(const void* pSender, ICMPEventArgs& args) | ||||
| 	static void onReply(ICMPEventArgs& args) | ||||
| 	{ | ||||
| 		std::ostringstream os; | ||||
| 		os << "Reply from " << args.hostAddress() | ||||
| 		   << " bytes=" << args.dataSize()  | ||||
| 		   << " time=" << args.replyTime() << "ms" | ||||
| 		   << " TTL=" << args.ttl(); | ||||
| 		logger().information(os.str()); | ||||
| 		std::cout << "Reply from " << args.hostAddress() | ||||
| 			<< " bytes=" << args.dataSize()  | ||||
| 			<< " time=" << args.replyTime() << "ms" | ||||
| 			<< " TTL=" << args.ttl() << std::endl; | ||||
| 	} | ||||
|  | ||||
| 	void onError(const void* pSender, ICMPEventArgs& args) | ||||
| 	static void onError(ICMPEventArgs& args) | ||||
| 	{ | ||||
| 		std::ostringstream os; | ||||
| 		os << args.error(); | ||||
| 		logger().information(os.str()); | ||||
| 		std::cout << args.error() << std::endl; | ||||
| 	} | ||||
|  | ||||
| 	void onEnd(const void* pSender, ICMPEventArgs& args) | ||||
| 	static void onEnd(ICMPEventArgs& args) | ||||
| 	{ | ||||
| 		std::ostringstream os; | ||||
| 		os << std::endl << "--- Ping statistics for " << args.hostName() << " ---" | ||||
| 		   << std::endl << "Packets: Sent=" << args.sent() << ", Received=" << args.received() | ||||
| 		   << " Lost=" << args.repetitions() - args.received() << " (" << 100.0 - args.percent() << "% loss)," | ||||
| 		   << std::endl << "Approximate round trip times in milliseconds: " << std::endl | ||||
| 		   << "Minimum=" << args.minRTT() << "ms, Maximum=" << args.maxRTT()   | ||||
| 		   << "ms, Average=" << args.avgRTT() << "ms"  | ||||
| 		   << std::endl << "------------------------------------------"; | ||||
| 		logger().information(os.str()); | ||||
| 		std::cout << std::endl << "--- Ping statistics for " << args.hostName() << " ---" | ||||
| 			<< std::endl << "Packets: Sent=" << args.sent() << ", Received=" << args.received() | ||||
| 			<< " Lost=" << args.repetitions() - args.received() << " (" << 100.0 - args.percent() << "% loss)," | ||||
| 			<< std::endl << "Approximate round trip times in milliseconds: " << std::endl | ||||
| 			<< "Minimum=" << args.minRTT() << "ms, Maximum=" << args.maxRTT()   | ||||
| 			<< "ms, Average=" << args.avgRTT() << "ms"  | ||||
| 			<< std::endl << "------------------------------------------" | ||||
| 			<< std::endl; | ||||
| 	} | ||||
|  | ||||
| private: | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Aleksandar Fabijanic
					Aleksandar Fabijanic