Line data Source code
1 :
2 : /* Copyright (c) 2010-2012, Stefan Eilemann <eile@eyescale.ch>
3 : * 2012, Daniel Nachbaur <danielnachbaur@gmail.com>
4 : *
5 : * This library is free software; you can redistribute it and/or modify it under
6 : * the terms of the GNU Lesser General Public License version 2.1 as published
7 : * by the Free Software Foundation.
8 : *
9 : * This library is distributed in the hope that it will be useful, but WITHOUT
10 : * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 : * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12 : * details.
13 : *
14 : * You should have received a copy of the GNU Lesser General Public License
15 : * along with this library; if not, write to the Free Software Foundation, Inc.,
16 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 : */
18 :
19 : #include "client.h"
20 :
21 : #include "global.h"
22 : #include "nodeType.h"
23 :
24 : #include <eq/fabric/commands.h>
25 :
26 : #include <co/iCommand.h>
27 : #include <co/commandQueue.h>
28 : #include <co/connection.h>
29 : #include <co/connectionDescription.h>
30 : #include <lunchbox/dso.h>
31 :
32 : namespace eq
33 : {
34 : namespace fabric
35 : {
36 : namespace detail
37 : {
38 : class Client {};
39 : }
40 :
41 15 : Client::Client()
42 15 : : _impl( 0 )
43 : {
44 15 : }
45 :
46 30 : Client::~Client()
47 : {
48 15 : delete _impl;
49 15 : LBASSERT( isClosed( ));
50 15 : }
51 :
52 16 : bool Client::connectServer( co::NodePtr server )
53 : {
54 16 : if( server->isConnected( ))
55 0 : return true;
56 :
57 16 : co::ConnectionDescriptionPtr connDesc;
58 16 : if( server->getConnectionDescriptions().empty( ))
59 : {
60 16 : connDesc = new co::ConnectionDescription;
61 16 : connDesc->port = EQ_DEFAULT_PORT;
62 :
63 16 : const std::string globalServer = Global::getServer();
64 16 : const char* envServer = getenv( "EQ_SERVER" );
65 16 : std::string address = !globalServer.empty() ? globalServer :
66 32 : envServer ? envServer : "localhost";
67 :
68 16 : if( !connDesc->fromString( address ))
69 0 : LBWARN << "Can't parse server address " << address << std::endl;
70 16 : LBASSERT( address.empty( ));
71 16 : LBINFO << "Connecting server " << connDesc->toString() << std::endl;
72 :
73 32 : server->addConnectionDescription( connDesc );
74 : }
75 :
76 16 : if( connect( server ))
77 0 : return true;
78 :
79 16 : if( connDesc ) // clean up
80 16 : server->removeConnectionDescription( connDesc );
81 :
82 16 : return false;
83 : }
84 :
85 0 : bool Client::disconnectServer( co::NodePtr server )
86 : {
87 0 : if( !server->isConnected( ))
88 : {
89 0 : LBWARN << "Trying to disconnect unconnected server" << std::endl;
90 0 : return false;
91 : }
92 :
93 0 : if( co::LocalNode::disconnect( server ))
94 0 : return true;
95 :
96 0 : LBWARN << "Server disconnect failed" << std::endl;
97 0 : return false;
98 : }
99 :
100 169 : void Client::processCommand( const uint32_t timeout )
101 : {
102 169 : co::CommandQueue* queue = getMainThreadQueue();
103 169 : LBASSERT( queue );
104 169 : co::ICommand command = queue->pop( timeout );
105 169 : if( !command.isValid( )) // just a wakeup()
106 174 : return;
107 :
108 164 : LBCHECK( command( ));
109 : }
110 :
111 8795 : bool Client::dispatchCommand( co::ICommand& command )
112 : {
113 8795 : LBVERB << "dispatch " << command << std::endl;
114 :
115 8877 : if( command.getCommand() >= co::CMD_NODE_CUSTOM &&
116 82 : command.getCommand() < CMD_SERVER_CUSTOM )
117 : {
118 66 : co::NodePtr node = command.getRemoteNode();
119 66 : return node->co::Dispatcher::dispatchCommand( command );
120 : }
121 :
122 8729 : return co::LocalNode::dispatchCommand( command );
123 : }
124 :
125 : }
126 45 : }
|