(file) Return to server.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus_unsupported / utils / memo

File: [Pegasus] / pegasus_unsupported / utils / memo / server.cpp (download)
Revision: 1.1, Wed Oct 26 17:19:58 2005 UTC (18 years, 7 months ago) by karl
Branch: MAIN
CVS Tags: HEAD
PEP#: 240
TITLE: Add new tool (memo)

DESCRIPTION: Add new tool.  Note readme explains tool.

/*
**==============================================================================
**
** Copyright (c) 2003, 2004, 2005 Michael E. Brasher
** 
** Permission is hereby granted, free of charge, to any person obtaining a
** copy of this software and associated documentation files (the "Software"),
** to deal in the Software without restriction, including without limitation
** the rights to use, copy, modify, merge, publish, distribute, sublicense,
** and/or sell copies of the Software, and to permit persons to whom the
** Software is furnished to do so, subject to the following conditions:
** 
** The above copyright notice and this permission notice shall be included in
** all copies or substantial portions of the Software.
** 
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
** SOFTWARE.
**
**==============================================================================
*/

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <unistd.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <pthread.h>
#include <cassert>
#include "memo.h"

unsigned short SERVER_PORT = 8888;

static int _close_on_exec(int sock)
{
    return ::fcntl(sock, F_SETFD, ::fcntl(sock, F_GETFD) | FD_CLOEXEC);
}

static void _handle_client_request(int sock)
{
    unsigned char request;

    int n = read(sock, &request, sizeof(request));

    if (n == -1)
    {
	fprintf(stderr, "MEMO: error reading socket: %d\n", sock);
	return;
    }

    switch (tolower(request))
    {
	case 't':
	{
	    size_t size = memo_total_bytes();
	    char buffer[64];
	    sprintf(buffer, "%d bytes\n", (int)size);
	    write(sock, buffer, strlen(buffer) + 1);
	    break;
	}

	case 'c':
	{
	    memo_check();
	    break;
	}

	default:
	{
	    const char buffer[] = "Illegal request\n";
	    write(sock, buffer, strlen(buffer) + 1);
	}
    }

    close(sock);
}

void _create_port_file(unsigned port)
{
    char path[64];
    sprintf(path, "/tmp/memo.port");

    FILE* os = fopen(path, "wb");

    if (!os)
    {
	fprintf(stderr, "MEMO: failed to create port file: %s\n", path);
	assert(0);
    }

    fprintf(os, "%u\n", port);
    fflush(os);
    fclose(os);
}

void run_server()
{
    // printf("MEMO: server started\n");

    //// Initialize server address:

    sockaddr_in server_addr;
    bzero(&server_addr, sizeof(sockaddr_in));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    server_addr.sin_port = 0;

    //// Figure out which port was used:

    // htons(SERVER_PORT);

    //// Create socket:

    int listen_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

    _close_on_exec(listen_sock);
    int opt = 1;
    setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, (char*)&opt, sizeof opt);

    bind(listen_sock, (sockaddr*)(&server_addr), sizeof server_addr);

    sockaddr_in tmp_addr;
    socklen_t tmp_len = sizeof(tmp_addr);
    getsockname(listen_sock, (sockaddr*)&tmp_addr, &tmp_len);
    _create_port_file(ntohs(tmp_addr.sin_port));
    
    listen(listen_sock, 5);

    //// Accept and handle connections:

    for (;;)
    {
	sockaddr_in addr;
	socklen_t length = sizeof addr;
	int sock = accept(listen_sock, (sockaddr*)&addr, &length);

	if (sock == -1)
	    continue;

	_close_on_exec(sock);
	_handle_client_request(sock);
	close(sock);
    }
}

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2