Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

bridge.cpp

Go to the documentation of this file.
00001 
00005 /* --------------------------------------------------------------
00006 Copyright (C) 2001 LifeLine Networks BV <soap2corba@lifeline.nl>
00007 
00008 This program is free software; you can redistribute it and/or
00009 modify it under the terms of the GNU General Public License
00010 as published by the Free Software Foundation; either
00011 version 2 of the License, or (at your option) any later
00012 version.
00013 
00014 This program is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 GNU General Public License for more details.
00018 
00019 You should have received a copy of the GNU General Public License
00020 along with this program; if not, write to the Free Software
00021 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00022 -------------------------------------------------------------- */
00023 
00024 #include "BridgeStub.h"
00025 #include <JTC/JTC.h>
00026 #include <sys/timeb.h>
00027 #include "anyconfig/AnyConfig.h"
00028 
00029 log4cpp::Category *soapBridgeLog;
00030 log4cpp::Category *corbaBridgeLog;
00031 log4cpp::Category *generalLog;
00032 
00033 static struct TheLevels {
00034     log4cpp::Priority::PriorityLevel level;
00035     char *name;
00036 } logLevels [] = {
00037     { log4cpp::Priority::EMERG , "EMERG" },
00038     { log4cpp::Priority::FATAL , "FATAL" },
00039     { log4cpp::Priority::ALERT , "ALERT" },
00040     { log4cpp::Priority::CRIT  , "CRIT" },
00041     { log4cpp::Priority::ERROR , "ERROR" },
00042     { log4cpp::Priority::WARN  , "WARN" },
00043     { log4cpp::Priority::NOTICE, "NOTICE" },
00044     { log4cpp::Priority::INFO  , "INFO" },
00045     { log4cpp::Priority::DEBUG , "DEBUG" },
00046     { log4cpp::Priority::NOTSET, NULL }
00047 };
00048 
00049 JTCMonitor CString_Monitor;
00051 JTCSynchronized *pCString_Lock = NULL;
00053 static void locker (void *pMon, bool setLock)
00054 {
00055     if (setLock) {
00056         JTCSynchronized *pSync = new JTCSynchronized (*((JTCMonitor *) pMon));
00057         pCString_Lock = pSync;
00058     } else {
00059         JTCSynchronized *pSync = pCString_Lock;
00060         pCString_Lock = NULL;
00061         if (pSync) delete pSync;
00062     }
00063 }
00064 
00066 ANYCFG_H sbInitialize (int argn, char *argv [])
00067 {
00068     JTCInitialize initialize;
00069     CString_init (&CString_Monitor, locker);
00070     ANYCFG_H hCfg = acfOpen (argn, argv);
00071     AnyConfig::AcConfiguration *pCfg = (AnyConfig::AcConfiguration *) hCfg;
00072     if (NULL != pCfg) {
00073         if (pCfg->keyExists ("ANYCFG_PRIMARY")) {
00074             AnyConfig::AcConfiguration *pRedundant = AnyConfig::loadRedundantConfig (pCfg, stderr);
00075             delete pCfg;
00076             pCfg = pRedundant;
00077         }
00078     }
00079     CString sysLogName      ("Soap2Corba");
00080     CString relayer         ("localhost");
00081     CString soapInstance    ("soapBridge");
00082     CString corbaInstance   ("corbaBridge");
00083     CString generalInstance ("general");
00084     CString logLevel        ("ERROR");
00085     log4cpp::Priority::PriorityLevel lev = log4cpp::Priority::ERROR;
00086 
00087     if (NULL == pCfg) {
00088         printf ("*** Warning: Could not obtain any configuration.\n");
00089     } else {
00090         pCfg->getSectionStr ("logging", "syslogname",      sysLogName     );
00091         pCfg->getSectionStr ("logging", "relayer",         relayer        );
00092         pCfg->getSectionStr ("logging", "soapinstance",    soapInstance   );
00093         pCfg->getSectionStr ("logging", "corbainstance",   corbaInstance  );
00094         pCfg->getSectionStr ("logging", "generalinstance", generalInstance);
00095         pCfg->getSectionStr ("logging", "loglevel",        logLevel       );
00096     }
00097     int lv;
00098     for (lv = 0; NULL != logLevels [lv].name; lv ++) {
00099         if (0 == logLevel.CompareNoCase (logLevels [lv].name)) {
00100             lev = logLevels [lv].level;
00101             break;
00102         }
00103     }
00104 
00105     log4cpp::Appender* appender;
00106     appender = new log4cpp::RemoteSyslogAppender("default", (const char *) sysLogName, (const char *) relayer);
00107     appender->setLayout(new log4cpp::BasicLayout());
00108     log4cpp::Category& root = log4cpp::Category::getRoot();
00109     root.setAppender(appender);
00110     root.setPriority(lev);
00111     soapBridgeLog = &log4cpp::Category::getInstance((const char *) soapInstance);
00112     corbaBridgeLog = &log4cpp::Category::getInstance((const char *) corbaInstance);
00113     generalLog = &log4cpp::Category::getInstance((const char *) generalInstance);
00114     return pCfg;
00115 }
00116 
00118 void sbTerminate (ANYCFG_H hdl)
00119 {
00120     delete (AnyConfig::AcConfiguration *) hdl;
00121     CString_term ();
00122 }
00123 
00125 static char * C = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
00126 
00134 void EncodeBase64 (const unsigned char *buf, const unsigned long csiz, CString &str, bool isSMTP)
00135 {
00136         unsigned long siz = csiz;
00137     unsigned long bit, c = 0;
00138     int xo = 0;
00139     unsigned char xb [100];
00140     str.Empty ();
00141 
00142     do {
00143         bit = 0;
00144         if (siz >= 3) {
00145             bit += buf [c ++]; bit <<= 8;
00146             bit += buf [c ++]; bit <<= 8;
00147             bit += buf [c ++];
00148             xb [xo ++] = C [(bit & 0xFC0000) >> 18]; //  11111100 00000000 00000000
00149             xb [xo ++] = C [(bit & 0x03F000) >> 12]; //  00000011 11110000 00000000
00150             xb [xo ++] = C [(bit & 0x000FC0) >>  6]; //  00000000 00001111 11000000
00151             xb [xo ++] = C [(bit & 0x00003F)      ]; //  00000000 00000000 00111111
00152             siz -= 3;
00153         } else if (siz == 2) {
00154             bit += buf [c ++]; bit <<= 8;
00155             bit += buf [c ++]; bit <<= 8;
00156             xb [xo ++] = C [(bit & 0xFC0000) >> 18]; //  11111100 00000000 00000000
00157             xb [xo ++] = C [(bit & 0x03F000) >> 12]; //  00000011 11110000 00000000
00158             xb [xo ++] = C [(bit & 0x000FC0) >>  6]; //  00000000 00001111 11000000
00159             xb [xo ++] = '=';
00160             siz = 0;
00161         } else if (siz == 1) {
00162             bit += buf [c ++]; bit <<= 16;
00163             xb [xo ++] = C [(bit & 0xFC0000) >> 18]; //  11111100 00000000 00000000
00164             xb [xo ++] = C [(bit & 0x03F000) >> 12]; //  00000011 11110000 00000000
00165             xb [xo ++] = '=';
00166             xb [xo ++] = '=';
00167             siz = 0;
00168         }
00169         if ((!siz && xo) || xo == 76) {
00170             if (isSMTP) {
00171                 xb [xo ++] = 13;
00172                 xb [xo ++] = 10;
00173             }
00174             xb [xo] = 0;
00175             str += (const char *) xb;
00176             xo = 0;
00177         }
00178     } while (siz > 0);
00179 }
00180 
00187 void DecodeBase64 (unsigned char *ou, unsigned long &nsiz, const char *buf)
00188 {
00189     unsigned long shift = 0, cnt, siz = strlen (buf);
00190     int bits = 0;
00191     nsiz = 0;
00192     
00193     for (cnt = 0; cnt < siz; cnt ++) {
00194         if (buf [cnt] == 13 || buf [cnt] == 10 || buf [cnt] == 32)
00195             continue;
00196         if (buf [cnt] == '=') {
00197             if (bits == 12) {
00198                 shift >>= 4;
00199                 ou [nsiz ++] = (unsigned char) (shift & 0xFF);
00200             } else if (bits == 18) {
00201                 shift >>= 2;
00202                 ou [nsiz ++] = (unsigned char) ((shift >> 8) & 0xFF);
00203                 ou [nsiz ++] = (unsigned char) (shift & 0xFF);
00204             }
00205             break;
00206         } else {
00207             unsigned long v;
00208             if (buf [cnt] >= 'A' && buf [cnt] <= 'Z') {
00209                 v = buf [cnt] - 'A';
00210             } else if (buf [cnt] >= 'a' && buf [cnt] <= 'z') {
00211                 v = buf [cnt] - 'a' + 26;
00212             } else if (buf [cnt] >= '0' && buf [cnt] <= '9') {
00213                 v = buf [cnt] - '0' + 52;
00214             } else if (buf [cnt] == '+') {
00215                 v = 62;
00216             } else if (buf [cnt] == '/') {
00217                 v = 63;
00218             } else {
00219                 // return X4M_MIME_BAD_BASE64;
00220                 // 19981124 changed, MS Outlook Express sometimes messes this up
00221                 // now pretend that the string WAS padded with ===
00222                 if (bits == 12) {
00223                     shift >>= 4;
00224                     ou [nsiz ++] = (unsigned char) (shift & 0xFF);
00225                 } else if (bits == 18) {
00226                     shift >>= 2;
00227                     ou [nsiz ++] = (unsigned char) ((shift >> 8) & 0xFF);
00228                     ou [nsiz ++] = (unsigned char) (shift & 0xFF);
00229                 } else {
00230                     shift <<= 2;
00231                     ou [nsiz ++] = (unsigned char) (shift & 0xFF);
00232                 }
00233                 break;
00234             }
00235             shift <<= 6;
00236             shift += v;
00237             bits += 6;
00238         }
00239         if (bits == 24) {
00240             ou [nsiz ++] = (unsigned char) (shift >> 16);
00241             ou [nsiz ++] = (unsigned char) ((shift >> 8) & 0xFF);
00242             ou [nsiz ++] = (unsigned char) (shift & 0xFF);
00243             shift = 0;
00244             bits = 0;
00245         }
00246     }
00247 }
00248 
00254 double sec_u (void)
00255 {
00256     struct timeb tb;
00257     ftime (&tb);
00258     double sec = tb.millitm;
00259     sec *= 0.001;
00260     sec += tb.time;
00261     return sec;
00262 }
00263 

This documentation is part of the "SOAP to CORBA bridge" project
Copyright © 2000 by Lifeline Networks bv.
All rights are reserved.