Exemple #1
0
JNIEXPORT void JNICALL Java_nativeinterfaces_DefaultNativeInterface_sendQuoteRequest
  (JNIEnv *, jobject, jobjectArray){
	// create a CThostFtdcMdApi instance
	CThostFtdcMdApi *pUserApi = CThostFtdcMdApi::CreateFtdcMdApi();
	CThostFtdcTraderApi *pTraderApi = CThostFtdcTraderApi::CreateFtdcTraderApi();
	// create an event handler instance
	quotationeventhandler sh(pUserApi);
	TraderEventHandler th(pTraderApi);
	// register an event handler instance
	
	pTraderApi->RegisterSpi(&th);
	// register the CTP front address and port
	
	//pTraderApi ->RegisterFront("tcp://218.1.96.8:41205");
	pTraderApi->SubscribePrivateTopic(THOST_TERT_RESUME);
	pTraderApi->SubscribePublicTopic(THOST_TERT_RESUME);
	
	// start the connection between client and CTP server
	
	//pTraderApi->Init();
	// waiting for the quotation data
	//WaitForSingleObject(g_hEvent, INFINITE);
	// release API instance

	//pTraderApi -> Release();

	pUserApi->RegisterSpi(&sh);
	pUserApi->RegisterFront("tcp://218.1.96.8:41213");
	
	pUserApi->Init();
	WaitForSingleObject(g_hEvent, INFINITE);
	pUserApi->Release();
}
int main(int argc, char* argv[])
{
	// 创建实例
	CThostFtdcMdApi* pUserApi = CThostFtdcMdApi::CreateFtdcMdApi("");

	// 创建spi实例
	CMdUserApiSample* pSpi = new CMdUserApiSample(pUserApi, "", "80002", "123456");

	// 注册spi实例
	pUserApi->RegisterSpi(pSpi);

	// 注册前置机	
	pUserApi->RegisterFront("tcp://10.253.117.107:13163");
	//pUserApi->RegisterFront("udp://10.253.117.107:20518");
	//CThostFtdcFensUserInfoField FensUserInfo = {0};
	//strncpy(FensUserInfo.UserID, "201301", sizeof(FensUserInfo.UserID)-1);
	//FensUserInfo.LoginMode = 'E';
	//pUserApi->RegisterFensUserInfo(&FensUserInfo);

	// 初始化
	pUserApi->Init();

	// 等待信号
	//WaitForSingleObject(g_hEvent, INFINITE);

	printf ("\npress return to quit...\n");

	getchar();

	// 初始化
	pUserApi->Release();

	return 0;
}
Exemple #3
0
int main(int argc, char *argv[])
{
    signal(SIGABRT, &sighandler);
    signal(SIGTERM, &sighandler);
    signal(SIGINT, &sighandler);
    ILog4zManager::GetInstance()->Start();
    LOGI("MD Application Start...");
    LOGI("Read params from config file");
    Config cfg("trader.config",0);
    g_strFlowPath = cfg.pString("MD_FLOW_PATH");
    g_strBrokerID = cfg.pString("BROKER_ID");
    g_strUserID = cfg.pString("USER_ID");
    g_strPassword = cfg.pString("PASSWORD");
    g_strLogPath = cfg.pString("LOG_PATH");
    g_strFrontURL = cfg.pString("MD_URL");

    // create a CThostFtdcMdApi instance
    CThostFtdcMdApi *pUserApi = CThostFtdcMdApi::CreateFtdcMdApi(g_strFlowPath.c_str());
    // create an event handler instance
    CSimpleHandler sh(pUserApi);
    try{
        LOGI("register an event handler instance");
        pUserApi->RegisterSpi(&sh);
        LOGI("register the CTP front address and port");
        char ch_URL[100];   
        strcpy(ch_URL, g_strFrontURL.c_str());
        pUserApi->RegisterFront(ch_URL);
        LOGFMTI("start to connect CTP server : %s", ch_URL);
        pUserApi->Init();
        LOGI("waiting for the quotation data");
        pthread_cond_wait(&cond, &mutex);
        LOGI("release API & SPI instance");
    }
    catch(exception& e)
    {
        LOGE("exception caught:"<< e.what());
    }
    if (pUserApi){
        pUserApi->RegisterSpi(NULL);
        pUserApi->Release();
        pUserApi = NULL;
    }
    if (&sh) {
//        delete &sh;
        sh = NULL;
    }
    LOGI("MD Application quit!");
    return 0;
}
  CTPWRAPPER_API int Process(void *instance,CTP_REQUEST_TYPE type,int p1,char *p2)
  {

    printf("Process %s","Process");

    switch(type)
    {

    case TraderApiCreate:
      {
        //C#回调函数
        CTPResponseCallback callback = (CTPResponseCallback)instance;

        //创建交易接口
        CThostFtdcTraderApi* pTrader = CThostFtdcTraderApi::CreateFtdcTraderApi(p2);
        //创建交易回调
        CTPTraderSpi *spi = new CTPTraderSpi(callback);

        //注册回调
        pTrader->RegisterSpi((CThostFtdcTraderSpi*)spi);

        //订阅流
        pTrader->SubscribePublicTopic((THOST_TE_RESUME_TYPE)p1);
        pTrader->SubscribePrivateTopic((THOST_TE_RESUME_TYPE)p1);

        return (int)pTrader;
      }


      ///删除接口对象本身
      ///@remark 不再使用本接口对象时,调用该函数删除接口对象
    case TraderApiRelease:
      {

        CThostFtdcTraderApi* pTrader = (CThostFtdcTraderApi *)instance;

        pTrader->RegisterSpi(NULL);
        pTrader->Release();

        //delete pTrader;
        instance = NULL;

        return 0;
      }

      ///初始化
      ///@remark 初始化运行环境,只有调用后,接口才开始工作
    case  TraderApiInit:
      {
        ((CThostFtdcTraderApi *)instance)->Init();
        return 0;
      }

      ///等待接口线程结束运行
      ///@return 线程退出代码
    case TraderApiJoin:
      {
        return ((CThostFtdcTraderApi *)instance)->Join();
      }

      ///获取当前交易日
      ///@retrun 获取到的交易日
      ///@remark 只有登录成功后,才能得到正确的交易日
    case TraderApiGetTradingDay:
      {
        const char* date = ((CThostFtdcTraderApi *)instance)->GetTradingDay();
        memcpy(p2,date,sizeof(date));
        return 0;
      }

      ///注册前置机网络地址
      ///@param pszFrontAddress:前置机网络地址。
      ///@remark 网络地址的格式为:“protocol://ipaddress:port”,如:”tcp://127.0.0.1:17001”。 
      ///@remark “tcp”代表传输协议,“127.0.0.1”代表服务器地址。”17001”代表服务器端口号。
    case TraderApiRegisterFront:
      {
        ((CThostFtdcTraderApi *)instance)->RegisterFront(p2);
        return 0;
      }

      ///注册前置机网络地址
      ///@param pszFrontAddress:前置机网络地址。
      ///@remark 网络地址的格式为:“protocol://ipaddress:port”,如:”tcp://127.0.0.1:17001”。 
      ///@remark “tcp”代表传输协议,“127.0.0.1”代表服务器地址。”17001”代表服务器端口号。
    case TraderApiRegisterNameServer:
      {
        ((CThostFtdcTraderApi *)instance)->RegisterNameServer(p2);
        return 0;
      }

      ///注册回调接口
      ///@param pSpi 派生自回调接口类的实例
    case TraderApiRegisterSpi:
      {
        ((CThostFtdcTraderApi *)instance)->RegisterSpi((CThostFtdcTraderSpi*)p1);
        return 0;
      }

      ///订阅私有流。
      ///@param nResumeType 私有流重传方式  
      ///        THOST_TERT_RESTART:从本交易日开始重传
      ///        THOST_TERT_RESUME:从上次收到的续传
      ///        THOST_TERT_QUICK:只传送登录后私有流的内容
      ///@remark 该方法要在Init方法前调用。若不调用则不会收到私有流的数据。
    case TraderApiSubscribePrivateTopic:
      {
        ((CThostFtdcTraderApi *)instance)->SubscribePrivateTopic((THOST_TE_RESUME_TYPE)p1);
        return 0;
      }

      ///订阅公共流。
      ///@param nResumeType 公共流重传方式  
      ///        THOST_TERT_RESTART:从本交易日开始重传
      ///        THOST_TERT_RESUME:从上次收到的续传
      ///        THOST_TERT_QUICK:只传送登录后公共流的内容
      ///@remark 该方法要在Init方法前调用。若不调用则不会收到公共流的数据。
    case TraderApiSubscribePublicTopic:
      {
        ((CThostFtdcTraderApi *)instance)->SubscribePublicTopic((THOST_TE_RESUME_TYPE)p1);
        return 0;
      }


    case MarketDataCreate:
      {
        //C#回调函数
        CTPResponseCallback callback = (CTPResponseCallback)instance;

        //创建行情接口
        CThostFtdcMdApi* pMdApi = CThostFtdcMdApi::CreateFtdcMdApi(p2);
        //创建交易回调
        CTPMarketDataSpi *spi = new CTPMarketDataSpi(callback);

        //注册回调
        pMdApi->RegisterSpi(spi);
        
        //订阅流
        //pMdApi->SubscribePublicTopic((THOST_TE_RESUME_TYPE)p1);
        //pMdApi->SubscribePrivateTopic((THOST_TE_RESUME_TYPE)p1);

        return (int)pMdApi;
      }

    case MarketDataRelease:
      {
        CThostFtdcMdApi* pMarketData = (CThostFtdcMdApi *)instance;

        pMarketData->RegisterSpi(NULL);
        pMarketData->Release();

        //delete instance;
        instance = NULL;

        return 0;
      }

    case MarketDataInit:
      {
        ((CThostFtdcMdApi *)instance)->Init();
        return 0;
      }

    case MarketDataRegisterFront:
      {
        ((CThostFtdcMdApi *)instance)->RegisterFront(p2);
        return 0;
      }

    case MarketDataRegisterNameServer:
      {
        ((CThostFtdcMdApi *)instance)->RegisterNameServer(p2);
        return 0;
      }
      
       ///订阅行情
    case MarketDataSubscribeMarketData:
      {
        char** p = (char**)calloc(p1, sizeof(char*));

        p = (char**)p2;

        p[0] = *p;

        for(int i=1;i<p1-1;i++)
        {
          p[i] = p[i-1] + strlen(p[i-1]) + 1;
        }

        return ((CThostFtdcMdApi*)instance)->SubscribeMarketData(p, p1);
      }

       ///退订行情
    case MarketDataUnSubscribeMarketData:
      {
        char** p = (char**)calloc(p1, sizeof(char*));

        

        //p = (char**)p2;
        OutputDebugStringA(p2);
        p[0] = p2;

        for(int i=1;i<p1-1;i++)
        {
          p[i] = p[i-1] + strlen(p[i-1]) + 1;
        }

        return ((CThostFtdcMdApi*)instance)->UnSubscribeMarketData(p, p1);
      }
    }
  }