Base Framework
testsuite/server.cpp
/***************************************************************************
The Base Framework (Test Suite)
A framework for developing platform independent applications
See COPYRIGHT.txt for details.
This framework is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
For the licensing terms refer to the file 'LICENSE'.
***************************************************************************/
#include <base/Application.h>
#include <base/string/FormatInputStream.h>
#include <base/string/FormatOutputStream.h>
#include <base/net/InetInterface.h>
#include <base/net/InetService.h>
#include <base/net/ServerSocket.h>
#include <base/Integer.h>
#include <base/TypeInfo.h>
using namespace com::azure::dev::base;
class ServerApplication : public Application {
private:
static const unsigned int MAJOR_VERSION = 1;
static const unsigned int MINOR_VERSION = 0;
public:
ServerApplication()
: Application("server")
{
}
void server(String a, String servicename) {
fout << "Hostname: " << InetAddress::getLocalHost() << ENDL;
{
fout << "Available interfaces:" << ENDL;
while (enu.hasNext()) {
const InetInterface& i = enu.next();
fout << " interface: index=" << i.getIndex() << " name=" << i.getName() << ENDL;
}
}
InetAddress address; // the address to bind the server socket to
if (a == "") { // should we find an address
fout << "Local addresses:" << ENDL;
unsigned int index = 0;
while (enu.hasNext()) {
const InetAddress& temp = enu.next();
if (index == 0) { // use the first address
address = temp;
fout << " address " << index++ << ": " << temp << " (USING THIS)" << ENDL;
} else {
fout << " address " << index++ << ": " << temp << ENDL;
}
}
} else {
address = InetAddress(a);
}
unsigned short port = 0; // the port to bind the server socket to
try {
Integer integer(servicename);
if ((integer < 0) || (integer > 0xffff)) {
_throw OutOfRange("Port is out of range.");
}
port = integer;
} catch (InvalidFormat&) {
try {
InetService service(servicename);
port = service.getPort();
fout << "Service: name=" << service.getName()
<< " port=" << service.getPort()
<< " protocol=" << service.getProtocol() << ENDL;
} catch (ServiceNotFound& e) {
fout << "Error: " << e.getMessage() << ENDL;
return;
}
}
fout << "Initializing server socket..." << ENDL;
ServerSocket serverSocket(address, port, 1);
fout << "Server address..." << ENDL;
fout << " address=" << serverSocket.getLocalAddress() << " port=" << serverSocket.getLocalPort() << ENDL;
fout << "Waiting for client to connect..." << ENDL;
unsigned int connection = 1; // the number of connections so far
bool terminate = false; // specifies whether the server should be terminated
while (!terminate) {
fout << "Listening for connections..." << ENDL;
StreamSocket socket(serverSocket.accept());
fout << "Connection number " << connection++ << " established from" << ENDL;
fout << " remote: address=" << socket.getAddress() << " port=" << socket.getPort() << ENDL;
fout << "Talking with client..." << ENDL;
{
FormatOutputStream outstream(socket); // must be destroyed before socket is closed
FormatInputStream instream(socket);
fout << "Waiting for request" << FLUSH;
while (!instream.wait(1000000)) {
fout << '.' << FLUSH;
}
fout << ENDL;
fout << "Processing request" << ENDL;
fout << ">: ";
while (instream.available()) {
char ch;
instream >> ch;
fout << ch;
}
fout << "Sending acknowledge" << ENDL;
outstream << "Hi, I'm the server and this is my response" << ENDL;
fout << "Waiting for termination request" << FLUSH;
while (!instream.wait(1000000)) {
fout << '.' << FLUSH;
}
fout << ENDL;
fout << "Processing terminating request" << ENDL;
fout << ">: ";
while (instream.available()) {
char ch;
instream >> ch;
fout << ch;
}
}
fout << "Closing connection..." << ENDL;
socket.close();
if (connection == 5) {
terminate = true;
}
}
fout << "Closing server socket..." << ENDL;
serverSocket.close();
}
void main()
{
fout << getFormalName() << " version "
<< MAJOR_VERSION << '.' << MINOR_VERSION << EOL
<< "The Base Framework (Test Suite)" << EOL
<< ENDL;
Array<String> arguments = getArguments();
String address; // default address
String service = "1234"; // default service
switch (arguments.getSize()) {
case 0:
// use defaults
break;
case 1:
service = arguments[0]; // the service
break;
case 2:
address = arguments[0]; // the address
service = arguments[1]; // the service
break;
default:
fout << "server [address] [service]" << ENDL;
return;
}
server(address, service);
}
};
APPLICATION_STUB(ServerApplication);
ServiceNotFound
Internet Protocol service exception.
Definition: ServiceNotFound.h:28
StreamSocket::getAddress
const InetAddress & getAddress() const noexcept
Definition: StreamSocket.h:155
List::getReadEnumerator
ReadEnumerator getReadEnumerator() const noexcept
Definition: List.h:618
ServerSocket::getLocalPort
unsigned short getLocalPort() const noexcept
Definition: ServerSocket.h:133
InetInterface::getName
static String getName(unsigned int index)
StreamSocket::close
void close()
Definition: StreamSocket.h:116
ServerSocket::accept
StreamSocket accept()
Definition: ServerSocket.h:86
DoubleLinkedNodeReadEnumerator
Non-modifying enumerator of DoubleLinkedNode.
Definition: DoubleLinkedNode.h:607
InetAddress::getAddressesByName
static List< InetAddress > getAddressesByName(const String &name)
FormatOutputStream
Format output stream.
Definition: FormatOutputStream.h:106
InetInterface::getIndex
unsigned int getIndex() const noexcept
Definition: InetInterface.h:129
InetService::getName
const String & getName() const noexcept
InetInterface::getInterfaces
static List< InetInterface > getInterfaces()
OutOfRange
Ouf of range exception.
Definition: OutOfRange.h:28
StreamSocket
Stream socket.
Definition: StreamSocket.h:33
FormatInputStream::available
unsigned int available() const
StreamSocket::getPort
unsigned short getPort() const noexcept
Definition: StreamSocket.h:163
String
String.
Definition: String.h:102
Array::getSize
MemorySize getSize() const noexcept
Definition: Array.h:339
FormatInputStream
Reader of formatted input.
Definition: FormatInputStream.h:32
DoubleLinkedNodeReadEnumerator::next
Reference next()
Definition: DoubleLinkedNode.h:648
InvalidFormat
Invalid formation exception.
Definition: InvalidFormat.h:29
DoubleLinkedNodeReadEnumerator::hasNext
bool hasNext() const noexcept
Definition: DoubleLinkedNode.h:640
InetInterface
Internet Protocol network interface.
Definition: InetInterface.h:33
Application
Application.
Definition: Application.h:53
InetService::getProtocol
const String & getProtocol() const noexcept
ServerSocket
Server socket.
Definition: ServerSocket.h:30
List
List collection.
Definition: List.h:45
ServerSocket::close
void close()
Definition: ServerSocket.h:107
FilterInputStream::wait
void wait() const
InetAddress::getLocalHost
static String getLocalHost()
Array< String >
InetService
Internet Protocol service.
Definition: InetService.h:31
ServerSocket::getLocalAddress
const InetAddress & getLocalAddress() const noexcept
Definition: ServerSocket.h:125
InetAddress
Internet Protocol address.
Definition: InetAddress.h:38
Exception::getMessage
const char * getMessage() const noexcept
Definition: Exception.h:234
Integer
Signed integer.
Definition: Integer.h:30
InetService::getPort
unsigned short getPort() const noexcept