~ubuntu-branches/ubuntu/trusty/mongodb/trusty-proposed

« back to all changes in this revision

Viewing changes to util/message_server_port.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Antonin Kral
  • Date: 2010-01-29 19:48:45 UTC
  • Revision ID: james.westby@ubuntu.com-20100129194845-8wbmkf626fwcavc9
Tags: upstream-1.3.1
ImportĀ upstreamĀ versionĀ 1.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// message_server_port.cpp
 
2
 
 
3
/*    Copyright 2009 10gen Inc.
 
4
 *
 
5
 *    Licensed under the Apache License, Version 2.0 (the "License");
 
6
 *    you may not use this file except in compliance with the License.
 
7
 *    You may obtain a copy of the License at
 
8
 *
 
9
 *    http://www.apache.org/licenses/LICENSE-2.0
 
10
 *
 
11
 *    Unless required by applicable law or agreed to in writing, software
 
12
 *    distributed under the License is distributed on an "AS IS" BASIS,
 
13
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
14
 *    See the License for the specific language governing permissions and
 
15
 *    limitations under the License.
 
16
 */
 
17
 
 
18
#ifndef USE_ASIO
 
19
 
 
20
#include "message.h"
 
21
#include "message_server.h"
 
22
 
 
23
namespace mongo {
 
24
 
 
25
    namespace pms {
 
26
 
 
27
        MessagingPort * grab = 0;
 
28
        MessageHandler * handler;
 
29
 
 
30
        void threadRun(){
 
31
            assert( grab );
 
32
            MessagingPort * p = grab;
 
33
            grab = 0;
 
34
            
 
35
            Message m;
 
36
            try {
 
37
                while ( 1 ){
 
38
                    m.reset();
 
39
 
 
40
                    if ( ! p->recv(m) ) {
 
41
                        log() << "end connection " << p->farEnd.toString() << endl;
 
42
                        p->shutdown();
 
43
                        break;
 
44
                    }
 
45
                    
 
46
                    handler->process( m , p );
 
47
                }
 
48
            }
 
49
            catch ( ... ){
 
50
                problem() << "uncaught exception in PortMessageServer::threadRun, closing connection" << endl;
 
51
                delete p;
 
52
            }            
 
53
            
 
54
        }
 
55
 
 
56
    }
 
57
 
 
58
    class PortMessageServer : public MessageServer , public Listener {
 
59
    public:
 
60
        PortMessageServer( int port , MessageHandler * handler ) :
 
61
            MessageServer( port , handler ) , 
 
62
            Listener( "", port ){
 
63
            
 
64
            uassert( 10275 ,  "multiple PortMessageServer not supported" , ! pms::handler );
 
65
            pms::handler = handler;
 
66
        }
 
67
        
 
68
        virtual void accepted(MessagingPort * p) {
 
69
            assert( ! pms::grab );
 
70
            pms::grab = p;
 
71
            boost::thread thr( pms::threadRun );
 
72
            while ( pms::grab )
 
73
                sleepmillis(1);
 
74
        }
 
75
        
 
76
        void run(){
 
77
            assert( init() );
 
78
            listen();
 
79
        }
 
80
 
 
81
    };
 
82
 
 
83
 
 
84
    MessageServer * createServer( int port , MessageHandler * handler ){
 
85
        return new PortMessageServer( port , handler );
 
86
    }    
 
87
 
 
88
}
 
89
 
 
90
#endif