Back to index

im-sdk  12.3.91
IIIMProtocol.cpp
Go to the documentation of this file.
00001 #include <config.h>
00002 #include <stdio.h>
00003 #include <iiimp.h>
00004 #include "IIIMProtocol.hh"
00005 #include "IIIMP_IMState.hh"
00006 #include "IIIMPTrans.hh"
00007 
00008 bool
00009 IIIMProtocol::restart(
00010     IMSocketAddressVec &addrvec
00011 )
00012 {
00013     if (pimsl) delete pimsl;
00014 
00015     pimsl = new IMSocketListen();
00016     if (!pimsl) return false;
00017 
00018     return pimsl->listen(addrvec);
00019 }
00020 
00021 bool
00022 IIIMProtocol::receive_and_dispatch(
00023     IMState *pims,
00024     int flags
00025 )
00026 {
00027     IIIMP_IMState *pimst = (IIIMP_IMState*) pims; // we should use dynamic_cast.
00028     IIIMP_message *pmes;
00029 
00030     pmes = pimst->get_iiimptrans()->receive();
00031     if (pmes) {
00032        pimst->dispatch(pmes->im_id, pmes->ic_id, pmes);
00033        iiimp_message_delete(pimst->get_iiimptrans()->get_data_s(), pmes);
00034        return true;
00035     }
00036     if (!pimst->get_iiimptrans()->open_p()) {
00037        LOG_ERROR("Client shut down the connection owned by im_id(%d).",
00038                 pims->get_im_id());
00039        pims->destroy();
00040        return false;
00041     }
00042 
00043     return true;  // direct IMScheduler to maintain this IMState.
00044 }
00045 
00046 IMState*
00047 IIIMProtocol::accept(
00048     int flags
00049 )
00050 {
00051     IIIMPTrans *pimt;
00052     IIIMP_IMState *pims;
00053 
00054     if (!pimsl) {
00055        LOG_CRITICAL("I don't have any listening manner.  Immediately exit.");
00056        exit(255);
00057     }
00058 
00059     IMSocketTrans *pimst = pimsl->accept();
00060     if (!pimst) return NULL;
00061     pimt = new IIIMPTrans(pimst);
00062     if (!pimt) {
00063        delete pimst;
00064        return NULL;
00065     }
00066     if (!pimaccept->request_accept(pimt->get_fd())) {
00067        delete pimt;
00068        return NULL;
00069     }
00070     
00071     pims = new IIIMP_IMState(this, pimt,
00072                           iiimp_data_s_get_protocol_version(pimt->get_data_s()));
00073     // memory error.
00074     if (!pims) return NULL;
00075     add_imstate(pims);
00076 
00077     return pims;
00078 }
00079 
00080 bool
00081 IIIMProtocol::set_server_listen_address(
00082     IMSocketAddressVec &addrvec
00083 )
00084 {
00085     return restart(addrvec);
00086 }
00087 
00088 
00089 IIIMProtocol::IIIMProtocol(
00090     IMAccept *pima
00091 ) : IMProtocol(pima)
00092 {
00093     pimsl = NULL;
00094 }
00095 
00096 IIIMProtocol::~IIIMProtocol()
00097 {
00098     if (pimsl) delete pimsl;
00099 }
00100 
00101 /* Local Variables: */
00102 /* c-file-style: "iiim-project" */
00103 /* End: */