2 #include <sys/select.h>
8 #include "Exceptions.hpp"
10 Server::Server( const AuthPtr
& auth
)
11 : m_listener( new Listener
)
12 , m_sessionController( new SessionController
)
15 g_log
->Print( "Dumb FTP server" );
16 g_log
->Print( std::string("IP: ") + m_listener
->GetIPAddr() );
23 Server::Server( const AuthPtr
& auth
, const std::string
& ip
)
24 : m_listener( new Listener( ip
) )
25 , m_sessionController( new SessionController
)
28 g_log
->Print( "Dumb FTP server" );
29 g_log
->Print( std::string("IP: ") + m_listener
->GetIPAddr() );
38 g_log
->Print( "[Server] Shutting down" );
41 ServerPtr
Server::Create( const AuthPtr
& auth
)
43 ServerPtr
ret( new Server( auth
) );
51 ServerPtr
Server::Create( const AuthPtr
& auth
, const std::string
& ip
)
53 ServerPtr
ret( new Server( auth
, ip
) );
64 std::list
<int> fds
= m_sessionController
->GetFds();
65 int maxfd
= m_listener
->GetSock();
70 FD_SET( maxfd
, &read
);
72 for( std::list
<int>::const_iterator it
= fds
.begin(); it
!= fds
.end(); ++it
)
74 maxfd
= std::max( maxfd
, abs( *it
) );
79 FD_SET( -(*it
), &write
);
91 int ret
= select( maxfd
+ 1, &read
, &write
, NULL
, &tv
);
95 g_log
->Print( strerror( errno
) );
96 throw ServerCrashException
;
100 // Descriptors not ready
104 std::list
<int> activeFds
;
105 for( std::list
<int>::const_iterator it
= fds
.begin(); it
!= fds
.end(); ++it
)
107 if( ( *it
> 0 && FD_ISSET( *it
, &read
) ) ||
108 ( *it
< 0 && FD_ISSET( -(*it
), &write
) ) )
110 activeFds
.push_back( abs( *it
) );
114 if( activeFds
.size() != 0 )
116 m_sessionController
->Tick( activeFds
);
119 if( FD_ISSET( m_listener
->GetSock(), &read
) )
125 void Server::IncomingConnection( int sock
)
127 m_sessionController
->Add( Session::Create( sock
, m_sessionController
, m_auth
, m_listener
->GetIPAddr(), m_this
) );
130 void Server::InitListener()
132 m_listener
->SetServer( m_this
);
135 void Server::LoadWelcomeMessage()
137 FILE *f
= fopen( "welcome", "r" );
144 while( fgets( buf
, 256, f
) )
146 if( buf
[strlen( buf
) - 1] == '\n' )
148 buf
[strlen( buf
) - 1] = 0;
151 m_welcome
.push_back( buf
);