6 #include <sys/select.h>
15 #include "Exceptions.hpp"
17 Server::Server( const AuthPtr
& auth
)
18 : m_listener( new Listener
)
19 , m_sessionController( new SessionController
)
22 g_log
->Print( "Dumb FTP server" );
23 g_log
->Print( std::string("IP: ") + m_listener
->GetIPAddr() );
30 Server::Server( const AuthPtr
& auth
, const std::string
& ip
)
31 : m_listener( new Listener( ip
) )
32 , m_sessionController( new SessionController
)
35 g_log
->Print( "Dumb FTP server" );
36 g_log
->Print( std::string("IP: ") + m_listener
->GetIPAddr() );
45 g_log
->Print( "[Server] Shutting down" );
48 ServerPtr
Server::Create( const AuthPtr
& auth
)
50 ServerPtr
ret( new Server( auth
) );
58 ServerPtr
Server::Create( const AuthPtr
& auth
, const std::string
& ip
)
60 ServerPtr
ret( new Server( auth
, ip
) );
71 std::list
<int> fds
= m_sessionController
->GetFds();
72 int maxfd
= m_listener
->GetSock();
77 FD_SET( maxfd
, &read
);
79 for( std::list
<int>::const_iterator it
= fds
.begin(); it
!= fds
.end(); ++it
)
81 maxfd
= std::max( maxfd
, abs( *it
) );
86 FD_SET( -(*it
), &write
);
98 int ret
= select( maxfd
+ 1, &read
, &write
, NULL
, &tv
);
102 g_log
->Print( strerror( errno
) );
103 throw ServerCrashException
;
107 // Descriptors not ready
111 std::list
<int> activeFds
;
112 for( std::list
<int>::const_iterator it
= fds
.begin(); it
!= fds
.end(); ++it
)
114 if( ( *it
> 0 && FD_ISSET( *it
, &read
) ) ||
115 ( *it
< 0 && FD_ISSET( -(*it
), &write
) ) )
117 activeFds
.push_back( abs( *it
) );
121 if( activeFds
.size() != 0 )
123 m_sessionController
->Tick( activeFds
);
126 if( FD_ISSET( m_listener
->GetSock(), &read
) )
132 void Server::IncomingConnection( int sock
)
134 m_sessionController
->Add( Session::Create( sock
, m_sessionController
, m_auth
, m_listener
->GetIPAddr(), m_this
) );
137 void Server::InitListener()
139 m_listener
->SetServer( m_this
);
142 void Server::LoadWelcomeMessage()
144 FILE *f
= fopen( "welcome", "r" );
151 while( fgets( buf
, 256, f
) )
153 if( buf
[strlen( buf
) - 1] == '\n' )
155 buf
[strlen( buf
) - 1] = 0;
158 m_welcome
.push_back( buf
);