~ubuntu-branches/ubuntu/oneiric/procserv/oneiric

« back to all changes in this revision

Viewing changes to acceptFactory.cc

  • Committer: Bazaar Package Importer
  • Author(s): Ralph Lange
  • Date: 2010-01-04 16:19:35 UTC
  • Revision ID: james.westby@ubuntu.com-20100104161935-uaosvjyry3zc5l5l
Tags: upstream-2.5.0
ImportĀ upstreamĀ versionĀ 2.5.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Process server for soft ioc
 
2
// David H. Thompson 8/29/2003
 
3
// Ralph Lange 04/22/2008
 
4
// GNU Public License (GPLv3) applies - see www.gnu.org
 
5
 
 
6
#include <unistd.h>
 
7
#include <stdio.h>
 
8
#include <stdlib.h>
 
9
#include <sys/types.h> 
 
10
#include <sys/socket.h> 
 
11
#include <netinet/in.h> 
 
12
#include <errno.h>
 
13
#include <netinet/in.h>
 
14
#include <arpa/inet.h>
 
15
#include <string.h>
 
16
 
 
17
#include "procServ.h"
 
18
 
 
19
 
 
20
class acceptItem : public connectionItem
 
21
{
 
22
public:
 
23
    acceptItem ( int port, bool local, bool readonly );
 
24
    bool OnPoll();
 
25
    int Send ( const char *, int );
 
26
 
 
27
public:
 
28
    virtual ~acceptItem();
 
29
};
 
30
 
 
31
// service and calls clientFactory when clients are accepted
 
32
connectionItem * acceptFactory ( int port, bool local, bool readonly )
 
33
{
 
34
    PRINTF("Creating new acceptItem %d\n", port);
 
35
    return new acceptItem(port, local, readonly);
 
36
}
 
37
 
 
38
acceptItem::~acceptItem()
 
39
{
 
40
    if (_ioHandle>=0) close(_ioHandle);
 
41
    PRINTF("~acceptItem()\n");
 
42
}
 
43
 
 
44
 
 
45
// Accept item constructor
 
46
// This opens a socket, binds it to the decided port,
 
47
// and sets it to listen mode
 
48
acceptItem::acceptItem ( int port, bool local, bool readonly )
 
49
{
 
50
    int optval = 1;
 
51
    struct sockaddr_in addr;
 
52
    int bindStatus;
 
53
 
 
54
    _readonly = readonly;
 
55
 
 
56
    _ioHandle = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP );
 
57
    assert( _ioHandle>0 );
 
58
 
 
59
    setsockopt( _ioHandle, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval) );
 
60
 
 
61
    addr.sin_family = AF_INET;
 
62
    addr.sin_port = htons(port);
 
63
    if ( local )
 
64
        inet_aton( "127.0.0.1", &addr.sin_addr );
 
65
    else 
 
66
        addr.sin_addr.s_addr = htonl( INADDR_ANY );
 
67
 
 
68
    bindStatus = bind( _ioHandle, (struct sockaddr *) &addr, sizeof(addr) );
 
69
    if (bindStatus<0)
 
70
    {
 
71
        PRINTF( "Bind: %s\n", strerror(errno) );
 
72
        // exit(-1);
 
73
        throw errno;
 
74
    }
 
75
    else
 
76
        PRINTF( "Bind returned %d\n", bindStatus );
 
77
 
 
78
    listen(_ioHandle,5);
 
79
    return; 
 
80
}
 
81
 
 
82
// OnPoll is called after a poll returns non-zero in events
 
83
bool acceptItem::OnPoll()
 
84
{
 
85
    int newSocket;
 
86
    struct sockaddr addr;
 
87
    socklen_t len = sizeof(addr);
 
88
 
 
89
    if ( _pfd==NULL || _pfd->revents==0 ) return false;
 
90
 
 
91
    // Otherwise process the revents and return true
 
92
 
 
93
    if ( _pfd->revents & (POLLIN|POLLPRI) )
 
94
    {
 
95
        newSocket = accept( _ioHandle, &addr, &len );
 
96
        PRINTF( "acceptItem: Accepted connection on handle %d\n", newSocket );
 
97
        AddConnection( clientFactory(newSocket, _readonly) );
 
98
    }
 
99
    if ( _pfd->revents & (POLLHUP|POLLERR) )
 
100
    {
 
101
        PRINTF( "acceptItem: Got hangup or error\n ");
 
102
    }
 
103
    if ( _pfd->revents & POLLNVAL )
 
104
    {
 
105
        _ioHandle = -1;
 
106
        _markedForDeletion = true;
 
107
    }
 
108
    return true;
 
109
}
 
110
 
 
111
// Send characters to client
 
112
int acceptItem::Send ( const char * buf, int count)
 
113
{
 
114
    // Makes no sense to send to the listening socket
 
115
    return true;
 
116
}