当前位置: 首页 > 工具软件 > jdiameter > 使用案例 >

利用Jdiameter与OCS进行交互

麹高义
2023-12-01

一.工作流程:

客户端发送cer命令去请求连接服务器(OCS),当ocs返回成功的cea命令,则表明与ocs的链接建立成功;

为了保证客户端与服务器连接正常,需每隔一段时间发送dwr命令检测链路;

有了以上的保证,客户端可以放心的发送ccr命令与服务器进行数据交互

 

二.操作步骤

1.初始化(与ocs连接和心跳响应被jdiameter内部实现,开发者可以不考虑构造和发送cer命令和dwr命令)

init方法:

   ClientConfiguration clientConfiguration = new ClientConfiguration();
   Stack stack = new org.jdiameter.client.impl.StackImpl();

   SessionFactory factory = stack.init(clientConfiguration);

    Loggers.FSM.logger().setLevel(Level.ALL);
    Loggers.Stack.logger().setLevel(Level.ALL);
    ConsoleHandler fh = new ConsoleHandler();
    fh.setLevel(Level.ALL);
    stack.getLogger().addHandler(fh);
    stack.getLogger().setUseParentHandlers(false);
    // start(Mode mode, long timeout, java.util.concurrent.TimeUnit
    // unit)
    stack.start(Mode.ALL_PEERS, stackTimeOut, TimeUnit.SECONDS);
ClientConfiguration类:

public static class ClientConfiguration extends EmptyConfiguration {
  public ClientConfiguration() {
   super();
   log.info("加载配置文件开始");
   AppConfiguration appConfiguration = AppConfiguration.getInstance();
   add(OwnDiameterURI, "aaa://" + AvpContants.ORIGIN_HOST + ":"
     + appConfiguration.getClientPort());
   log.info("OwnDiameterURI:" + "aaa://" + AvpContants.ORIGIN_HOST
     + ":" + appConfiguration.getClientPort());
   add(OwnIPAddress, appConfiguration.getClientIp());
   add(OwnRealm, hostRealm);
   add(OwnVendorID, vendorId);
   add(ApplicationId,
   // AppId 1
     getInstance().add(VendorId, vendorId).add(AcctApplId,
       acctApplId));
   add(DwaTimeOut, (long) appConfiguration.getAvpDwrTimeout());
   // dwr 间隔时间
   add(IacTimeOut, (long) appConfiguration.getAvpDwrTimeout());
   add(CeaTimeOut, (long) appConfiguration.getAvpCerTimeout());
   add(RecTimeOut, (long) appConfiguration.getAvpCerTimeout());

   // peerTable 用来管理peer
   // 一个Peer对象表示一个远程的Diameter客户端,服务器或者代理。
   add(PeerTable,
   // Peer 1
     getInstance().add(
       PeerName,
       "aaa://" + appConfiguration.getServerIp() + ":"
         + appConfiguration.getServerPort()));// 连接ip+port
   log.info("PeerName___" + "aaa://" + appConfiguration.getServerIp()
     + ":" + appConfiguration.getServerPort());
   // realm 域信息
   add(RealmTable,
   // Realm 1
     getInstance().add(
       RealmEntry,
       hostRealm + ":" + AvpContants.ORIGIN_HOST + ","
         + appConfiguration.getServerIp()));
   log.info("RealmEntry___" + hostRealm + ":"
     + AvpContants.ORIGIN_HOST + ","
     + appConfiguration.getServerIp());
   
   org.jdiameter.client.impl.helpers.AppConfiguration extensionPoints = (org.jdiameter.client.impl.helpers.AppConfiguration) getChildren(Extensions.ordinal())[0];
            extensionPoints.add(InternalPeerController, "com.sntele.surfing.ability.ocs.test.MyPeerTable");
  }
 }

 

2.构造命令ccr与发送命令到ocs

Session session = factory.getNewSession();
  // createRequest(int commandCode, ApplicationId appId, java.lang.String
  // destRealm, java.lang.String destHost)
  Request request = session.createRequest(command,
    org.jdiameter.api.ApplicationId.createByAccAppId(vendorId,
      acctApplId), hostRealm);

AvpSet avpSet = request.getAvps();

//  {Auth-Application-Id} DCCA      4
  avpSet.addAvp(AvpCode.AUTH_APPLICATION_ID,4,true,false);
//  {Service-Context-Id}
  avpSet.addAvp(AvpCode.SERVICE_CONTEXR_ID,AvpContants.SERVICE_CONTEXT_ID_ISMP,true,false,false);
//  {CC-Request-Type} 4 - EVENT_REQUEST
  avpSet.addAvp(AvpCode.CC_REQUEST_TYPE,4,true,false);
//  {CC-Request-Number} 0
  avpSet.addAvp(AvpCode.CC_REQUEST_NUMBER,0,true,false);

...........................................

Future<Message> future = session.send(request, timeOut,
    TimeUnit.SECONDS);

(Answer) future.get();

3.解析ocs返回的cca信息

  log.info("验证OCS请求返回信息  start");
  int commandCode = answer.getCommandCode();//命令码
  log.info("返回的命令编码:" + commandCode);

  int resultCode = answer.getResultCode().getInteger32();//结果码

  AvpSet avpSet = answer.getAvps();//返回数据集合

  

三.如果在dwr命令中断时,由于心跳连接(dwr)被封装在jdiameter内,可通过以下方法得知连接什么时候断开

在ClientConfiguration类加入:

org.jdiameter.client.impl.helpers.AppConfiguration extensionPoints = (org.jdiameter.client.impl.helpers.AppConfiguration) getChildren(Extensions.ordinal())[0];
            extensionPoints.add(InternalPeerController, "test.MyPeerTable");

 

创建MyPeerTable类(继承与PeerTableImpl),并重写ActionContext的sendDwrMessage()方法即可

 

 类似资料: