Back to index

im-sdk  12.3.91
IMSvr.cpp
Go to the documentation of this file.
00001 #include <config.h>
00002 #include <stdio.h>
00003 #include "IMSvr.hh"
00004 #include "IMSvrCfg.hh"
00005 #include "IMConnection.hh"
00006 #include "LEMgr.hh"
00007 #include "IIIMProtocol.hh"
00008 #include "IMScheduler.hh"
00009 
00010 bool
00011 IMSvr::config_lepath(
00012     const char *lepath
00013 )
00014 {
00015     LEMgr *plm = new LEMgr(lepath);
00016     if (!plm) return false;
00017     if (plemgr) delete plemgr;
00018     plemgr = plm;
00019     return true;
00020 }
00021 
00022 bool
00023 IMSvr::config_le (
00024     const char *lepath,
00025     IIIMLEXMLConf &xml,
00026     bool preferred_loading
00027 )
00028 {
00029     LEMgr *plm = new LEMgr (lepath, xml, preferred_loading);
00030 
00031     if (!plm)
00032        return false;
00033     if (plemgr)
00034        delete plemgr;
00035     plemgr = plm;
00036 
00037     return true;
00038 }
00039 
00040 bool
00041 IMSvr::set_nsmap_config (
00042     IMNsMapStruct *nsmp,
00043     int num_entries
00044 )
00045 {
00046     plemgr->set_nsmap_config(nsmp, num_entries);
00047     return true;
00048 }
00049 
00050 bool
00051 IMSvr::config_listenaddress(
00052     IMSocketAddressVec& addrvec
00053 )
00054 {
00055     IIIMProtocol *pimp = new IIIMProtocol(this);
00056     if (!pimp) return false;
00057 
00058     pimp->set_server_listen_address(addrvec);
00059     if (pimprotocol)
00060        delete pimprotocol;
00061     pimprotocol = pimp;
00062 
00063     return true;
00064 }
00065 
00066 IMKeySpecList
00067 IMSvr::get_conversionkeys() const
00068 {
00069     return IMKeyParser(pcfg->get_strval(IMSvrCfg::CONVERSIONKEYS)).get_parsedkeys();
00070 }
00071 
00072 int
00073 IMSvr::start()
00074 {
00075     if (!pcfg->configure(this)) return IMSVR_CONFIG_ERROR;
00076     ASSERT(plemgr);
00077     ASSERT(pimprotocol);
00078 
00079     for(;;) {
00080         IMState *pims = pimprotocol->accept();
00081         if (pims) {
00082             IMScheduler::get_instance()->start(this, pimprotocol, pims);
00083         }
00084         if (signal_flag.test(CAUGHT_SIGHUP)) {
00085             // received HUP signal
00086             plemgr->reload();
00087         }
00088         if (signal_flag.test(CAUGHT_SIGTERM)) {
00089             // received TERM signal
00090             break;
00091         }
00092         signal_flag.reset();
00093     }
00094 
00095     return IMSVR_EXIT_SUCCESS;
00096 }
00097 
00098 void
00099 IMSvr::reload()
00100 {
00101     // NOTICE:
00102     // this function is called by signal handler.
00103     // so we must use signal-safe function in it.
00104     signal_flag.set(CAUGHT_SIGHUP, true);
00105 }
00106 
00107 void
00108 IMSvr::terminate()
00109 {
00110     // NOTICE:
00111     // this function is called by signal handler.
00112     // so we must use signal-safe function in it.
00113     signal_flag.set(CAUGHT_SIGTERM, true);
00114     return;
00115 }
00116 
00117 bool
00118 IMSvr::request_accept(
00119     int fd
00120 )
00121 {
00122     if (usermgr.fd_ok(fd)) return true;
00123 
00124     return false;
00125 }
00126 
00127 IMConnection*
00128 IMSvr::create_imconnection(
00129     IMUser *puser,
00130     const string &hostname
00131 )
00132 {
00133     IMConnection* pc;
00134 
00135     pc = new IMConnection(this, puser, hostname);
00136     if (!pc) return NULL;
00137 
00138     return pc;
00139 }
00140 
00141 IMHandler*
00142 IMSvr::request_connect(
00143     int fd,
00144     const u16string &username,
00145     const string &hostname,
00146     const u16string &password,
00147     const IMAuthList &authlist
00148 )
00149 {
00150     IMUser *puser;
00151     IMConnection *pimc;
00152 
00153     // TODO
00154     if (!username.get_charstr()) return NULL;
00155     puser = usermgr.create_imuser(fd, username.get_charstr(), password.get_charstr());
00156     if (!puser) return NULL;
00157     pimc = create_imconnection(puser, hostname);
00158     if (!pimc) return NULL;
00159 
00160     return pimc;
00161 }
00162 
00163 IMSvr::IMSvr(
00164     IMSvrCfg *p_cfg
00165 ) :
00166     pcfg(p_cfg)
00167 {
00168     plemgr = NULL;
00169     pimprotocol = NULL;
00170 }
00171 
00172 IMSvr::~IMSvr()
00173 {
00174     if (plemgr) delete plemgr;
00175     if (pimprotocol) delete pimprotocol;
00176 }
00177 
00178 /* Local Variables: */
00179 /* c-file-style: "iiim-project" */
00180 /* End: */