Line data Source code
1 :
2 : /* Copyright (c) 2010-2015, Stefan Eilemann <eile@eyescale.ch>
3 : * 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 24 : Client::Client()
42 24 : : _impl( 0 )
43 : {
44 24 : }
45 :
46 48 : Client::~Client()
47 : {
48 24 : delete _impl;
49 24 : LBASSERT( isClosed( ));
50 24 : }
51 :
52 24 : bool Client::connectServer( co::NodePtr server )
53 : {
54 24 : if( server->isConnected( ))
55 0 : return true;
56 :
57 24 : if( !server->getConnectionDescriptions().empty( ))
58 0 : return connect( server );
59 :
60 24 : co::ConnectionDescriptionPtr connDesc( new co::ConnectionDescription );
61 24 : connDesc->port = EQ_DEFAULT_PORT;
62 24 : const std::string& globalServer = Global::getServer();
63 24 : const char* envServer = getenv( "EQ_SERVER" );
64 24 : std::string address = !globalServer.empty() ? globalServer :
65 48 : envServer ? envServer : "localhost";
66 24 : if( !connDesc->fromString( address ))
67 : {
68 0 : LBWARN << "Can't parse server address " << address << std::endl;
69 0 : return false;
70 : }
71 24 : LBDEBUG << "Connecting server " << connDesc->toString() << std::endl;
72 24 : server->addConnectionDescription( connDesc );
73 :
74 24 : if( connect( server ))
75 0 : return true;
76 :
77 24 : server->removeConnectionDescription( connDesc );
78 48 : return false;
79 : }
80 :
81 0 : bool Client::disconnectServer( co::NodePtr server )
82 : {
83 0 : if( !server->isConnected( ))
84 : {
85 0 : LBWARN << "Trying to disconnect unconnected server" << std::endl;
86 0 : return false;
87 : }
88 :
89 0 : if( disconnect( server ))
90 0 : return true;
91 :
92 0 : LBWARN << "Server disconnect failed" << std::endl;
93 0 : return false;
94 : }
95 :
96 74 : void Client::processCommand( const uint32_t timeout )
97 : {
98 74 : co::CommandQueue* queue = getMainThreadQueue();
99 74 : LBASSERT( queue );
100 74 : co::ICommand command = queue->pop( timeout );
101 74 : if( !command.isValid( )) // wakeup() or timeout delivers invalid command
102 74 : return;
103 :
104 74 : LBCHECK( command( ));
105 : }
106 :
107 612 : bool Client::dispatchCommand( co::ICommand& command )
108 : {
109 612 : LBVERB << "dispatch " << command << std::endl;
110 :
111 696 : if( command.getCommand() >= co::CMD_NODE_CUSTOM &&
112 84 : command.getCommand() < CMD_SERVER_CUSTOM )
113 : {
114 60 : co::NodePtr node = command.getRemoteNode();
115 60 : return node->co::Dispatcher::dispatchCommand( command );
116 : }
117 :
118 552 : return co::LocalNode::dispatchCommand( command );
119 : }
120 :
121 : }
122 84 : }
|