database.sarang.net
UserID
Passwd
Database
DBMS
ㆍMySQL
PostgreSQL
Firebird
Oracle
Informix
Sybase
MS-SQL
DB2
Cache
CUBRID
LDAP
ALTIBASE
Tibero
DB 문서들
스터디
Community
공지사항
자유게시판
구인|구직
DSN 갤러리
도움주신분들
Admin
운영게시판
최근게시물
MySQL Devel 21119 게시물 읽기
 News | Q&A | Columns | Tutorials | Devel | Files | Links
No. 21119
MySQL UDF -- corba_string
작성자
정재익(advance)
작성일
2004-02-24 09:56:56
조회수
6,335

You can access CORBA implementations from MySQL using this function: select corba_string("localhost","900","test.my_context/so me.Object");

 

==================

clt.cc

==================

 

// eg3_clt.cc - This is the source code of example 3 used in Chapter 2
//              "The Basics" of the omniORB2 user guide.
//
//              This is the client. It uses the COSS naming service
//              to obtain the object reference.
//
// Usage: eg3_clt
//
//
//        On startup, the client lookup the object reference from the
//        COS naming service.
//
//        The name which the object is bound to is as follows:
//              root  [context]
//               |
//              text  [context] kind [my_context]
//               |
//              Echo  [object]  kind [Object]
//

#include <iostream.h>
#include "mysql_corba.hh"

#include "greeting.cc"

extern void hello(CORBA::Object_ptr obj);

static CORBA::Object_ptr getObjectReference(CORBA::ORB_ptr orb);

int
main (int argc, char **argv)
{
  CORBA::ORB_ptr orb = CORBA::ORB_init(argc,argv,"omniORB2");
  CORBA::BOA_ptr boa = orb->BOA_init(argc,argv,"omniORB2_BOA");

  try {
    CORBA::Object_var obj = getObjectReference(orb);
    hello(obj);
  }
  catch(CORBA::COMM_FAILURE& ex) {
    cerr << "Caught system exception COMM_FAILURE, unable to contact the "
         << "object." << endl;
  }
  catch(omniORB::fatalException& ex) {
    cerr << "Caught omniORB2 fatalException. This indicates a bug is caught "
         << "within omniORB2.\nPlease send a bug report.\n"
         << "The exception was thrown in file: " << ex.file() << "\n"
         << "                            line: " << ex.line() << "\n"
         << "The error message is: " << ex.errmsg() << endl;
  }
  catch(...) {
    cerr << "Caught a system exception." << endl;
  }

  return 0;
}

static
CORBA::Object_ptr
getObjectReference(CORBA::ORB_ptr orb)
{
  CosNaming::NamingContext_var rootContext;
 
  try {
    // Obtain a reference to the root context of the Name service:
    CORBA::Object_var initServ;
    initServ = orb->resolve_initial_references("NameService");

    // Narrow the object returned by resolve_initial_references()
    // to a CosNaming::NamingContext object:
    rootContext = CosNaming::NamingContext::_narrow(initServ);
    if (CORBA::is_nil(rootContext))
      {
        cerr << "Failed to narrow naming context." << endl;
        return CORBA::Object::_nil();
      }
  }
  catch(CORBA::ORB::InvalidName& ex) {
    cerr << "Service required is invalid [does not exist]." << endl;
    return CORBA::Object::_nil();
  }


  // Create a name object, containing the name test/context:
  CosNaming::Name name;
  name.length(2);

  name[0].id   = (const char*) "test";       // string copied
  name[0].kind = (const char*) "my_context"; // string copied
  name[1].id   = (const char*) "mysql_corba";
  name[1].kind = (const char*) "Object";
  // Note on kind: The kind field is used to indicate the type
  // of the object. This is to avoid conventions such as that used
  // by files (name.type -- e.g. test.ps = postscript etc.)

 
  CORBA::Object_ptr obj;
  try {
    // Resolve the name to an object reference, and assign the reference
    // returned to a CORBA::Object:
    obj = rootContext->resolve(name);
  }
  catch(CosNaming::NamingContext::NotFound& ex)
    {
      // This exception is thrown if any of the components of the
      // path [contexts or the object] aren't found:
      cerr << "Context not found." << endl;
      return CORBA::Object::_nil();
    }
  catch (CORBA::COMM_FAILURE& ex) {
    cerr << "Caught system exception COMM_FAILURE, unable to contact the "
         << "naming service." << endl;
    return CORBA::Object::_nil();
  }
  catch(omniORB::fatalException& ex) {
    throw;
  }
  catch (...) {
    cerr << "Caught a system exception while using the naming service."<< endl;
    return CORBA::Object::_nil();
  }
  return obj;
}

===========================

impl.cc

===========================

 

// eg3_clt.cc - This is the source code of example 3 used in Chapter 2
//              "The Basics" of the omniORB2 user guide.
//
//              This is the client. It uses the COSS naming service
//              to obtain the object reference.
//
// Usage: eg3_clt
//
//
//        On startup, the client lookup the object reference from the
//        COS naming service.
//
//        The name which the object is bound to is as follows:
//              root  [context]
//               |
//              text  [context] kind [my_context]
//               |
//              Echo  [object]  kind [Object]
//

#include <iostream.h>
#include "mysql_corba.hh"

#include "greeting.cc"

extern void hello(CORBA::Object_ptr obj);

static CORBA::Object_ptr getObjectReference(CORBA::ORB_ptr orb);

int
main (int argc, char **argv)
{
  CORBA::ORB_ptr orb = CORBA::ORB_init(argc,argv,"omniORB2");
  CORBA::BOA_ptr boa = orb->BOA_init(argc,argv,"omniORB2_BOA");

  try {
    CORBA::Object_var obj = getObjectReference(orb);
    hello(obj);
  }
  catch(CORBA::COMM_FAILURE& ex) {
    cerr << "Caught system exception COMM_FAILURE, unable to contact the "
         << "object." << endl;
  }
  catch(omniORB::fatalException& ex) {
    cerr << "Caught omniORB2 fatalException. This indicates a bug is caught "
         << "within omniORB2.\nPlease send a bug report.\n"
         << "The exception was thrown in file: " << ex.file() << "\n"
         << "                            line: " << ex.line() << "\n"
         << "The error message is: " << ex.errmsg() << endl;
  }
  catch(...) {
    cerr << "Caught a system exception." << endl;
  }

  return 0;
}

static
CORBA::Object_ptr
getObjectReference(CORBA::ORB_ptr orb)
{
  CosNaming::NamingContext_var rootContext;
 
  try {
    // Obtain a reference to the root context of the Name service:
    CORBA::Object_var initServ;
    initServ = orb->resolve_initial_references("NameService");

    // Narrow the object returned by resolve_initial_references()
    // to a CosNaming::NamingContext object:
    rootContext = CosNaming::NamingContext::_narrow(initServ);
    if (CORBA::is_nil(rootContext))
      {
        cerr << "Failed to narrow naming context." << endl;
        return CORBA::Object::_nil();
      }
  }
  catch(CORBA::ORB::InvalidName& ex) {
    cerr << "Service required is invalid [does not exist]." << endl;
    return CORBA::Object::_nil();
  }


  // Create a name object, containing the name test/context:
  CosNaming::Name name;
  name.length(2);

  name[0].id   = (const char*) "test";       // string copied
  name[0].kind = (const char*) "my_context"; // string copied
  name[1].id   = (const char*) "mysql_corba";
  name[1].kind = (const char*) "Object";
  // Note on kind: The kind field is used to indicate the type
  // of the object. This is to avoid conventions such as that used
  // by files (name.type -- e.g. test.ps = postscript etc.)

 
  CORBA::Object_ptr obj;
  try {
    // Resolve the name to an object reference, and assign the reference
    // returned to a CORBA::Object:
    obj = rootContext->resolve(name);
  }
  catch(CosNaming::NamingContext::NotFound& ex)
    {
      // This exception is thrown if any of the components of the
      // path [contexts or the object] aren't found:
      cerr << "Context not found." << endl;
      return CORBA::Object::_nil();
    }
  catch (CORBA::COMM_FAILURE& ex) {
    cerr << "Caught system exception COMM_FAILURE, unable to contact the "
         << "naming service." << endl;
    return CORBA::Object::_nil();
  }
  catch(omniORB::fatalException& ex) {
    throw;
  }
  catch (...) {
    cerr << "Caught a system exception while using the naming service."<< endl;
    return CORBA::Object::_nil();
  }
  return obj;
}

=========================

mysql_corba_i.cc

=========================

// echo_i.cc - This source code demonstrates an implmentation of the
//             object interface mysql_corba. It is part of the three examples
//             used in Chapter 2 "The Basics" of the omniORB2 user guide.
//
#include <string.h>
#include "mysql_corba.hh"

class mysql_corba_i : public virtual _sk_mysql_corba {
public:
  mysql_corba_i() {}
  virtual ~mysql_corba_i() {}
  virtual char * String(const char *mesg);
};

char *
mysql_corba_i::String(const char *mesg) {
  cerr << "WOW! I Got the message: \"" << mesg << "\" from the client." << endl;
  char *p = CORBA::string_dup("It's a test message");
  cerr << "Sending message: \"" << p << "\" to the client." << endl;
  return p;
}

 

================

udf_corba.cc

================

 

/* 
   Copyright Abandoned 1998 TCX DataKonsult AB & Monty Program KB & Detron HB &
   Alexis Mikhailov <root@medinf.chuvashia.su>
   This file is public domain and comes with NO WARRANTY of any kind

   Heavily modified udf_example.cc for udf_corba.cc by tonu@mysql.com
   Many code snaps in original or modified form are from omniORB 2.7.1
   source distribution (LGPL, ftp.cam-orl.co.uk)

   Yes, it is the CORBA client function for MySQL.
   This is just some experimental version for demo purposes.
   Don't use it for production work because it is absolutely untested
   and 99% subject to change in near future. We release it to public only
   for collecting ideas.

   Please send comments and feature requests to tonu@mysql.com!

*/

//#ifdef STANDARD
#include <stdio.h>
#include <string.h>
//#else
#include <global.h>
#include <my_sys.h>
//#endif
#include <mysql.h>
#include <m_ctype.h>
#include <iostream.h>
#include "mysql_corba.hh"

#ifdef HAVE_DLOPEN

extern "C" {
  my_bool corba_string_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
  void corba_string_deinit(UDF_INIT *initid);
  char *corba_string(UDF_INIT *initid, UDF_ARGS *args, char *result,
         unsigned long *length, char *is_null, char *error);
}


// StringToCosNamingName() turns a string into a CosNaming::Name.
// Name components are separated by '/', the id from the kind by '.'.  A '\'
// quotes the next character.  An extra '/' at the beginning or end is ignored.
// An empty string denotes a name with no components - use "//" if you want a
// name component with both id and kind empty.
//

static void
StringToCosNamingName(const char* arg, CosNaming::Name& name)
{
  int n = 0;
  char* str = CORBA::string_dup(arg);
  int len = strlen(str);
  char* id = str;
  char* kind = "";

  for (int i = 0; i < len; i++) {
    if (str[i] == '\\') {
      memmove(&str[i], &str[i+1], len-i);
      len--;

    } else if (str[i] == '.') {
      str[i] = '\0';
      kind = &str[i+1];

    } else if (str[i] == '/') {
      str[i] = '\0';
      if (i != 0) {
        name.length(n+1);
        name[n].id   = (const char*) id;
        name[n].kind = (const char*) kind;
        n++;
      }
      id = &str[i+1];
      kind = "";
    }
  }

  if (id[0] != '\0' || kind[0] != '\0') {
    name.length(n+1);
    name[n].id   = (const char*) id;
    name[n].kind = (const char*) kind;
    n++;
  }

  CORBA::string_free(str);
}


/*************************************************************************
** Init routine
**************************************************************************/


static CORBA::Object_ptr getObjectReference(CORBA::ORB_ptr orb, char *);

#define MAXCORBARESPONSE 8

my_bool corba_string_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
  if (args->arg_count == 0) {
    strcpy(message,"Usage: corba_string('IP',port,'method')");
    return 1;
  } else if (args->arg_count == 3 && (args->arg_type[0] != STRING_RESULT
      || args->arg_type[1] != STRING_RESULT
      || args->arg_type[2] != STRING_RESULT)) {
    strcpy(message,"Wrong arguments to corba_string");
    return 1;
  }

  int argc=7;
  char * argv[]={"mysqld",
                 "-ORBtraceLevel","1",
                 "-ORBInitialHost",args->args[0],
                 "-ORBInitialPort",args->args[1]};
  int i;

  CORBA::ORB_ptr orb = CORBA::ORB_init(argc,argv,"omniORB2");
  CORBA::BOA_ptr boa = orb->BOA_init(argc,argv,"omniORB2_BOA");

  try {

    CORBA::Object_var obj = getObjectReference(orb,args->args[2]);
    if (CORBA::is_nil(obj)) {
      strcpy(message,"cannot invoke on a nil object reference");
      return 1;
    }
    CORBA::String_var kala = orb->object_to_string(obj);

    mysql_corba_var e = mysql_corba::_narrow(obj);

    kala = orb->object_to_string(e);

    if (CORBA::is_nil(e)) {
      strcpy(message,"Cannot invoke on a nil object reference");
      return 1;
    }

    CORBA::String_var src = (const char*) "Foo!";
    CORBA::String_var dest;

    dest = e->String(src);
    initid->max_length=MAXCORBARESPONSE;
    initid->ptr = CORBA::string_dup(dest);
    return 0;
  }
  catch(CORBA::COMM_FAILURE& ex) {
    strcpy(message,"Caught system exception COMM_FAILURE, unable to contact the  object.");
    return 1;
  }
  catch(omniORB::fatalException& ex) {
    strcpy(message,"Caught omniORB2 fatalException");
    return 1;
  }
  catch(...) {
    strcpy(message,"Caught a system exception.");
    return 1;
  }
}


static
CORBA::Object_ptr
getObjectReference(CORBA::ORB_ptr orb, char * namestring)
{
  CosNaming::NamingContext_var rootContext;
 
  try {
    // Obtain a reference to the root context of the Name service:
    CORBA::Object_var initServ;
    initServ = orb->resolve_initial_references("NameService");

    // Narrow the object returned by resolve_initial_references()
    // to a CosNaming::NamingContext object:
    rootContext = CosNaming::NamingContext::_narrow(initServ);
    if (CORBA::is_nil(rootContext))
      {
        cerr << "Failed to narrow naming context." << endl;
        return CORBA::Object::_nil();
      }
  }
  catch(CORBA::ORB::InvalidName& ex) {
    cerr << "Service required is invalid [does not exist]." << endl;
    return CORBA::Object::_nil();
  }


  try {
    CosNaming::Name name;
    StringToCosNamingName(namestring, name);
    CORBA::Object_ptr obj;
    obj = rootContext->resolve(name);
    return obj;
  }
  catch(CosNaming::NamingContext::NotFound& ex)
    {
      cerr << "Context not found." << endl;
      return CORBA::Object::_nil();
    }
  catch (CORBA::COMM_FAILURE& ex) {
    cerr << "Caught system exception COMM_FAILURE, unable to contact the  naming service." << endl;
    return CORBA::Object::_nil();
  }
  catch(omniORB::fatalException& ex) {
    throw;
  }
  catch (...) {
    cerr << "Caught a system exception while using the naming service."<< endl;
    return CORBA::Object::_nil();
  }
}

void corba_string_deinit(UDF_INIT *initid) {
}

char *corba_string(UDF_INIT *initid, UDF_ARGS *args, char *result,
        unsigned long *length, char *is_null, char *error)
{
  *length=(long unsigned)strlen(initid->ptr);
  // I know, this is slow and ugly but it works.
  return initid->ptr;
}
#endif /* HAVE_DLOPEN */

[Top]
No.
제목
작성자
작성일
조회
21129MySQL UDF -- ip lookup
정재익
2004-02-24
8464
21128MySQL UDF -- unprefix
정재익
2004-02-24
8052
21125MySQL UDF -- ip address manipulation
정재익
2004-02-24
6789
21119MySQL UDF -- corba_string
정재익
2004-02-24
6335
21117MySQL UDF -- valid_date
정재익
2004-02-24
6625
21114MySQL UDF - il2_to_cp1250
정재익
2004-02-24
5085
21113MySQL UDF - inet_ntoa() 의 구현
정재익
2004-02-24
5328
Valid XHTML 1.0!
All about the DATABASE... Copyleft 1999-2017 DSN, All rights reserved.
작업시간: 0.264초, 이곳 서비스는
	PostgreSQL v9.6.2으로 자료를 관리합니다