天天看點

mdsplus之通過C和C++遠端通路

首先當然需要将遠端mdsplus伺服器防火牆的相應端口開放,或者關閉防火牆。

說明,下面中本地IP為202.127.202.142

                         遠端mdsplus伺服器IP為202.127.204.220

eastpower樹位于202.127.204.220

一、C語言中的遠端通路mdsplus

1.在程式中先connect伺服器,再open打開相應樹,并且這種情況下在本地可以配置要打開tree的環境變量(export或在envsyms裡面配置),且任意配置不受影響,代碼如下:

//mdsplus_read.c
#include<mdslib.h>
#include<mdsshr.h>
#include<stdio.h>
#include<string.h>
#define statusOk(status) ((status)&1)
int getSize(char*sigName);
int main(int argc,char** argv){
	int socket=-1;
	int status=-1;
	int shot=90000;
	char tName[]="eastpower";
	char ip[]="202.127.204.220:8000";
	int size=0;
	socket=MdsConnect(ip);//:8001");
	if(-1==socket){
		fprintf(stderr,"Error connecting to mdsip server.\n");
//		return -1;
	}
	status=MdsOpen(tName,&shot);
	if(!statusOk(status)){
		fprintf(stderr,"Error opening tree for shot %d:%s.\n",shot,MdsGetMsg(status));
		return -1;
	}else{
		fprintf(stderr,"opening tree for shot %d:%s Successful.\n",shot,MdsGetMsg(status));
	}//
	size=getSize("f010_S");
	printf("size=%d\n",size);
	if(size<1){
		fprintf(stderr,"Error retrieving length of signal\n");
		return -1;
	}//if
	return 0;
}
int getSize(char*sigName){
	int dtypelong=DTYPE_LONG;
	int retSize;
	int lenDescr;
	int null=0;
	int status;
	int effNum=0;
	char expression[1024];
	sprintf(expression,"SIZE(%s)",sigName);
	lenDescr=descr(&dtypelong,&retSize,&null);
	status=MdsValue(expression,&lenDescr,&null,&effNum);
	if(!statusOk(status)){
		fprintf(stderr,"Error getting the size of %s.\n",sigName);
		return -1;
	}//if
	return retSize;
}//getSize()
           

Makefile如下:

cc=gcc
mdsplus_read:mdsplus_read.o
        cc mdsplus_read.o -L/usr/local/mdsplus/lib -lMdsLib -lMdsShr -lc -o mdsplus_read -std=c99
mdsplus_read.o:mdsplus_read.c
        cc -c mdsplus_read.c -o mdsplus_read.o -I/usr/local/mdsplus/include -std=c99
           

2.通過在本地envsyms中配置相應樹的環境變量(修改了這個配置檔案記得重新打開下終端加載環境變量),但是這種情況下,我隻對8000端口測試成功,并且需要在envsyms省略不寫8000端口,如下面:

eastpower_path 202.127.204.220::/var/ftp/pub/easttest
           

代碼中不再需要connect遠端mdsplus伺服器,可以像通路本地mdsplus一樣,如下隻給出了main函數,其他同上:

int main(int argc,char** argv){
        int status=-1;
        int shot=90000;
        char tName[]="eastpower";
        int size;
        status=MdsOpen(tName,&shot);
        if(!statusOk(status)){
                fprintf(stderr,"Error opening tree for shot %d:%s.\n",shot,MdsGetMsg(status));
                return -1;
        }else{
                fprintf(stderr,"opening tree for shot %d:%s Successful.\n",shot,MdsGetMsg(status));
        }//
        size=getSize("f010_S");
        printf("size=%d\n",size);
        if(size<1){
                fprintf(stderr,"Error retrieving length of signal\n");
                return -1;
        }//if
        return 0;
}
           

二、C++語言中的遠端通路mdsplus

1.類似于在C程式中先connect伺服器,再open打開相應樹:

//mdsplus_c++_sample2.cpp
#include<mdsobjects.h>
#include<cstdlib>
#include<iostream>
#include<string>
inline char * convert(const std::string & str){
        return const_cast<char *> (str.c_str());
}
int main(int argc,char** argv){
        std::string  treeName("eastpower");
        std::string nodeName("F010_S");
        try{
                MDSplus::Connection *conn = new MDSplus::Connection(convert("202.127.204.220"));
                conn->openTree(convert(treeName),9000);
                conn->closeTree(convert(treeName),9000);
        }catch(MDSplus::MdsException*exc){
                std::cout<<"Error appending segments:"<<exc->what()<<std::endl;;
        }
        return EXIT_SUCCESS;
}
           

Makefile如下:

cc=g++
mdsplus_c++_sample2:mdsplus_c++_sample2.o
        $(cc) mdsplus_c++_sample2.o -o mdsplus_c++_sample2 -L/usr/local/mdsplus/lib -lMdsObjectsCppShr
mdsplus_c++_sample2.o:mdsplus_c++_sample2.cpp
        $(cc) mdsplus_c++_sample2.cpp -c -o mdsplus_c++_sample2.o -I/usr/local/mdsplus/include -g
           

 2.通過在本地envsyms中配置相應樹的環境變量,代碼如下:

//mdsplus_c++_sample2.cpp
#include<mdsobjects.h>
#include<cstdlib>
#include<iostream>
#include<string>
inline char * convert(const std::string & str){
        return const_cast<char *> (str.c_str());
}
int main(int argc,char** argv){
        std::string  treeName("eastpower");
        std::string nodeName("F010_S");
        try{
                MDSplus::Tree * t= new MDSplus::Tree(convert(treeName),90000,convert("edit"));
                MDSplus::TreeNode * node=t->getNode(convert(nodeName));
                t->quit();
        }catch(MDSplus::MdsException*exc){
                std::cout<<"Error appending segments:"<<exc->what()<<std::endl;;
        }
        return EXIT_SUCCESS;
}
           

通過在本地envsyms中配置相應樹的環境變量情況下,mdstcl和jScope工具都能像通路本地樹一樣打開遠端mdsplus伺服器上的樹。

繼續閱讀