FaxServer.c++
上传用户:weiyuanprp
上传日期:2020-05-20
资源大小:1169k
文件大小:8k
源码类别:

传真(Fax)编程

开发平台:

C/C++

  1. /* $Id: FaxServer.c++,v 1.4 2008/01/12 20:00:18 faxguy Exp $ */
  2. /*
  3.  * Copyright (c) 1990-1996 Sam Leffler
  4.  * Copyright (c) 1991-1996 Silicon Graphics, Inc.
  5.  * HylaFAX is a trademark of Silicon Graphics
  6.  *
  7.  * Permission to use, copy, modify, distribute, and sell this software and 
  8.  * its documentation for any purpose is hereby granted without fee, provided
  9.  * that (i) the above copyright notices and this permission notice appear in
  10.  * all copies of the software and related documentation, and (ii) the names of
  11.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  12.  * publicity relating to the software without the specific, prior written
  13.  * permission of Sam Leffler and Silicon Graphics.
  14.  * 
  15.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  16.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  17.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  18.  * 
  19.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  20.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  21.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  22.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  23.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  24.  * OF THIS SOFTWARE.
  25.  */
  26. #include <unistd.h>
  27. #include <sys/param.h>
  28. #include "Sys.h"
  29. #include "FaxServer.h"
  30. #include "Class0.h"
  31. #include "Class1Ersatz.h"
  32. #include "Class10.h"
  33. #include "Class2Ersatz.h"
  34. #include "Class20.h"
  35. #include "Class21.h"
  36. #include "config.h"
  37. #ifndef MAXHOSTNAMELEN
  38. #define MAXHOSTNAMELEN 64
  39. #endif
  40. fxIMPLEMENT_ObjArray(FaxRecvInfoArray, FaxRecvInfo)
  41. /*
  42.  * HylaFAX Fax Modem Server.
  43.  */
  44. FaxServer::FaxServer(const fxStr& devName, const fxStr& devID)
  45.     : ModemServer(devName, devID)
  46. {
  47.     modem = NULL;
  48. }
  49. FaxServer::~FaxServer()
  50. {
  51. }
  52. void
  53. FaxServer::initialize(int argc, char** argv)
  54. {
  55.     ModemServer::initialize(argc, argv);
  56.     hostname.resize(MAXHOSTNAMELEN);
  57.     char buff[MAXHOSTNAMELEN];
  58.     if (Sys::gethostname(buff, MAXHOSTNAMELEN) == 0) {
  59.         hostname = buff;
  60.         hostname.resize(strlen(hostname));
  61.     }
  62. }
  63. time_t FaxServer::getConnectTime() const
  64.     { return (connTime); }
  65. time_t FaxServer::getFileTransferTime() const
  66.     { return (Sys::now() - fileStart); }
  67. time_t FaxServer::getPageTransferTime() const
  68.     { return (pageTransferTime); }
  69. time_t FaxServer::setPageTransferTime()
  70. {
  71.     pageTransferTime = Sys::now() - pageStart;
  72.     pageStart = Sys::now(); // reset
  73.     return (pageTransferTime);
  74. }
  75. const Class2Params& FaxServer::getClientParams() const
  76.     { return (clientParams); }
  77. void FaxServer::notifyCallPlaced(const FaxRequest&) {}
  78. void FaxServer::notifyConnected(const FaxRequest&) {}
  79. /*
  80.  * Setup the modem; if needed.  Note that we reread
  81.  * the configuration file if it's been changed prior
  82.  * to setting up the modem.  This makes it easy to
  83.  * swap modems that need different configurations
  84.  * just by yanking the cable and then swapping the
  85.  * config file before hooking up the new modem.
  86.  */
  87. bool
  88. FaxServer::setupModem(bool isSend)
  89. {
  90.     modem = NULL;
  91.     if (!ModemServer::setupModem(isSend))
  92. return (false);
  93.     if (getModem()->isFaxModem()) {
  94. modem = (FaxModem*) ModemServer::getModem();
  95. modem->setLID(localIdentifier);
  96.     }
  97.     return (true);
  98. }
  99. void
  100. FaxServer::discardModem(bool dropDTR)
  101. {
  102.     ModemServer::discardModem(dropDTR);
  103.     modem = NULL;
  104. }
  105. /*
  106.  * Deduce the type of modem supplied to the server
  107.  * and return an instance of the appropriate modem
  108.  * driver class.
  109.  */
  110. ClassModem*
  111. FaxServer::deduceModem(bool isSend)
  112. {
  113.     fxStr h(type);
  114.     h.raisecase();
  115.     /*
  116.      * Probe for modem using type, if specified; otherwise
  117.      * try Class 2.1, Class 2.0, Class 2, Class 1.0, Class 1, and then Class 0 types.
  118.      */
  119.     u_int modemServices = 0;    // fax classes to try (bitmask)
  120.     ClassModem* modem;
  121.     if (h == "UNKNOWN"){
  122. modem = new Class0Modem(*this, *this);
  123. if (modem) {
  124.     if (modem->setupModem(isSend)){
  125.                 modemServices = modem->getModemServices();
  126.                 fxStr mfr = modem->getManufacturer();
  127.                 mfr.raisecase();
  128.                 if( mfr.find(0,"ROBOTICS") < mfr.length() ||
  129.                     mfr.find(0,"3COM")     < mfr.length() ){
  130.                     /*
  131.                      * Don't use Class2.0 with USR modems if not explicitly
  132.                      * specified in the config file (it doesn't work reliably) -- dbely
  133.                      */
  134.                     modem->serverTrace("USR/3COM modem: disable Class 2.0");
  135.                     modemServices &= ~SERVICE_CLASS20;
  136.                 }
  137.             }
  138.     delete modem;
  139. }
  140.     }
  141.     else if (h == "CLASS2.1")
  142.         modemServices |= SERVICE_CLASS21;
  143.     else if (h == "CLASS2.0")
  144.         modemServices |= SERVICE_CLASS20;
  145.     else if (h == "CLASS2")
  146.         modemServices |= SERVICE_CLASS2;
  147.     else if (h == "CLASS1.0")
  148.         modemServices |= SERVICE_CLASS10;
  149.     else if (h == "CLASS1")
  150.         modemServices |= SERVICE_CLASS1;
  151.     if (modemServices & SERVICE_CLASS21) {
  152. modem = new Class21Modem(*this, *this);
  153. if (modem) {
  154.     if (modem->setupModem(isSend))
  155. return modem;
  156.     delete modem;
  157. }
  158.     }
  159.     if (modemServices & SERVICE_CLASS20) {
  160. modem = new Class20Modem(*this, *this);
  161. if (modem) {
  162.     if (modem->setupModem(isSend))
  163. return modem;
  164.     delete modem;
  165. }
  166.     }
  167.     if (modemServices & SERVICE_CLASS2) {
  168. modem = new Class2ErsatzModem(*this, *this);
  169. if (modem) {
  170.     if (modem->setupModem(isSend))
  171. return modem;
  172.     delete modem;
  173. }
  174.     }
  175.     if (modemServices & SERVICE_CLASS10) {
  176. modem = new Class10Modem(*this, *this);
  177. if (modem) {
  178.     if (modem->setupModem(isSend))
  179. return modem;
  180.     delete modem;
  181. }
  182.     }
  183.     if (modemServices & SERVICE_CLASS1) {
  184. modem = new Class1ErsatzModem(*this, *this);
  185. if (modem) {
  186.     if (modem->setupModem(isSend))
  187. return modem;
  188.     delete modem;
  189. }
  190.     }
  191.     return (NULL);
  192. }
  193. /*
  194.  * Modem support interfaces.  Note that the values
  195.  * returned when we don't have a handle on the modem
  196.  * are selected so that any imaged facsimile should
  197.  * still be sendable.
  198.  */
  199. bool FaxServer::modemSupports2D() const
  200.     { return modem ? modem->supports2D() : false; }
  201. bool FaxServer::modemSupportsMMR() const
  202.     { return modem ? modem->supportsMMR() : false; }
  203. bool FaxServer::modemSupportsEOLPadding() const
  204.     { return modem ? modem->supportsEOLPadding() : false; }
  205. bool FaxServer::modemSupportsVRes(float res) const
  206.     { return modem ? modem->supportsVRes(res) : true; }
  207. bool FaxServer::modemSupportsPageLength(u_int l) const
  208.     { return modem ? modem->supportsPageLength(l) : true; }
  209. bool FaxServer::modemSupportsPolling() const
  210.     { return modem ? modem->supportsPolling() : false; }
  211. fxStr
  212. FaxServer::getModemCapabilities() const
  213. {
  214.     return fxStr::format("%c%08x"
  215. , modem->supportsPolling() ? 'P' : 'p'
  216. , modem->getCapabilities()
  217.     );
  218. }
  219. /*
  220.  * Server configuration support.
  221.  */
  222. /*
  223.  * Read a configuration file.  Note that we suppress
  224.  * dial string rules setup while reading so that the
  225.  * order of related parameters is not important.  We
  226.  * also setup the local identifier from the fax number
  227.  * if nothing is specified in the config file (for
  228.  * backwards compatibility).
  229.  */
  230. void
  231. FaxServer::readConfig(const fxStr& filename)
  232. {
  233.     ModemServer::readConfig(filename);
  234.     if (localIdentifier == "")
  235. setLocalIdentifier(canonicalizePhoneNumber(FAXNumber));
  236. }
  237. /*
  238.  * Set local identifier and if modem is setup
  239.  * pass in so that it can be installed in the
  240.  * modem (e.g. for Class 2-style modems).
  241.  */
  242. void
  243. FaxServer::setLocalIdentifier(const fxStr& lid)
  244. {
  245.     ServerConfig::setLocalIdentifier(lid);
  246.     if (modem)
  247. modem->setLID(lid);
  248. }
  249. const fxStr& FaxServer::getLocalIdentifier() const { return (localIdentifier); }