Decode.java
资源名称:SIPPhone.zip [点击查看]
上传用户:pdtewte
上传日期:2021-10-16
资源大小:13k
文件大小:7k
源码类别:
IP电话/视频会议
开发平台:
Java
- import java.net.*;
- import java.io.*;
- import java.util.*;
- class Decode
- {
- public String sPacketType;
- public String sURL;
- public String sVia;
- public String sBranch;
- public String sCallID;
- public String sFROM;
- public String sTag;
- public String sTO;
- public String sCSeq;
- public String sContact;
- public String sContactUsername;
- public String sContactIP;
- public String sContactPort;
- public String sURLIP;
- public String sURLPort;
- public String sURLUsername;
- public String sUserAgent;
- public String sExpires;
- public String sMediaIP;
- public String sMediaPort;
- public Decode(String packet)
- {
- this.sURL = GetURL(packet);
- this.sVia = GetVIA(packet);
- this.sPacketType = GetPacketType(packet);
- this.sBranch = GetBranch(packet);
- this.sCallID = GetCallID(packet);
- this.sContact = GetContact(packet);
- this.sFROM = GetFrom(packet);
- this.sTO = GetTo(packet);
- this.sCSeq = GetCSeq(packet);
- this.sExpires = GetExpires(packet);
- this.sMediaIP = GetMedia(packet);
- }
- public String GetMedia(String message)
- {
- this.sMediaIP = "";
- this.sMediaPort = "";
- int indexBeginOfurl = message.indexOf("m=audio");
- if ( -1 == indexBeginOfurl )
- {
- return "";
- }
- int indexEndOfurl = message.indexOf("rn",indexBeginOfurl);
- String Media = message.substring(indexBeginOfurl, indexEndOfurl);
- Media = Media.substring("m=audio ".length());
- StringTokenizer token = new StringTokenizer(Media, " ");
- this.sMediaPort = token.nextToken();
- indexBeginOfurl = message.indexOf("c=IN IP4 ");
- indexEndOfurl = message.indexOf("rn",indexBeginOfurl);
- if ( -1 == indexBeginOfurl || indexEndOfurl == -1 )
- {
- return "";
- }
- Media = message.substring(indexBeginOfurl, indexEndOfurl);
- Media = Media.substring("c=IN IP4 ".length());
- token = new StringTokenizer(Media, " ");
- this.sMediaIP = token.nextToken();
- return this.sMediaIP;
- }
- public String GetPacketType(String message)
- {
- StringTokenizer s = new StringTokenizer(message);
- if ( -1 == message.indexOf(" ") )
- {
- return "";
- }
- return s.nextToken();
- }
- public String GetBranch(String message)
- {
- int indexBeginOfbranch = message.indexOf("branch");
- int indexEndOfbranch = message.indexOf("rn", indexBeginOfbranch);
- if ( -1 == indexBeginOfbranch || -1 == indexEndOfbranch )
- {
- return "";
- }
- String branch = message.substring(indexBeginOfbranch, indexEndOfbranch);
- branch = branch.substring("branch=".length());
- return branch;
- }
- public String GetURL(String message)
- {
- int indexBeginOfurl = message.indexOf("sip");
- int indexEndOfurl = message.indexOf("SIP");
- if ( -1 == indexBeginOfurl || -1 == indexEndOfurl )
- {
- return "";
- }
- String url = message.substring(indexBeginOfurl, indexEndOfurl);
- url = url.substring("sip:".length());
- StringTokenizer token = new StringTokenizer(url, "<> :@");
- this.sURLUsername = token.nextToken();
- this.sURLIP = token.nextToken();
- if (token.hasMoreTokens())
- {
- this.sURLPort = token.nextToken();
- }
- else
- {
- this.sURLPort = "5060";
- }
- return url;
- }
- public String GetVIA(String message)
- {
- int indexBeginOfVia = message.indexOf("Via");
- int indexEndOfVia = message.indexOf(";");
- if ( -1 == indexBeginOfVia || -1 == indexEndOfVia )
- {
- return "";
- }
- String via = message.substring(indexBeginOfVia, indexEndOfVia);
- via = via.substring("Via: SIP/2.0/UDP ".length());
- return via;
- }
- public String GetExpires(String message)
- {
- int indexBeginOfExpires = message.indexOf("Expires: ");
- if (indexBeginOfExpires == -1)
- {
- //System.out.println("Expires: ");
- return " ";
- }
- int indexEndOfExpires = message.indexOf("rn", indexBeginOfExpires);
- String Expires = message.substring(indexBeginOfExpires, indexEndOfExpires);
- Expires = Expires.substring("Expires: ".length());
- //System.out.println("Expires: " + Expires);
- return Expires;
- }
- public String GetCallID(String message)
- {
- int indexBeginOfCallID = message.indexOf("Call-ID:");
- int indexEndOfCallID = message.indexOf("rn", indexBeginOfCallID);
- if ( -1 == indexBeginOfCallID || -1 == indexEndOfCallID )
- {
- return "";
- }
- String CallID = message.substring(indexBeginOfCallID, indexEndOfCallID);
- CallID = CallID.substring("Call-ID: ".length());
- CallID.trim();
- return CallID;
- }
- public String GetContact(String message)
- {
- int indexBeginOfContact = message.indexOf("Contact:");
- if (indexBeginOfContact == -1)
- {
- return "";
- }
- int indexEndOfContact = message.indexOf("rn", indexBeginOfContact);
- String Contact = "";
- Contact = message.substring(indexBeginOfContact, indexEndOfContact);
- Contact = Contact.substring("Contact: ".length());
- Contact.trim();
- String tmp = Contact;
- int indexOfUser = tmp.indexOf("sip:");
- int indexOfAt = tmp.indexOf("@");
- this.sContactUsername = tmp.substring(indexOfUser + "sip:".length(), indexOfAt);
- int indexOfLastColon = tmp.lastIndexOf(":");
- int indexOfFirstColon = tmp.indexOf(":");
- if (indexOfLastColon == indexOfFirstColon)
- {
- this.sContactIP = tmp.substring(indexOfAt + 1, tmp.indexOf(">"));
- this.sContactPort = "5060";
- }
- else
- {
- this.sContactIP = tmp.substring(indexOfAt + 1, indexOfLastColon );
- this.sContactPort = tmp.substring(indexOfLastColon + 1, tmp.indexOf(">") );
- }
- return Contact;
- }
- public String GetFrom(String message)
- {
- int indexBeginOfFrom = message.indexOf("From:");
- int indexEndOfFrom = message.indexOf("rn", indexBeginOfFrom);
- if ( -1 == indexBeginOfFrom || -1 == indexEndOfFrom )
- {
- return "";
- }
- String From = message.substring(indexBeginOfFrom, indexEndOfFrom);
- From = From.substring("From: ".length());
- From.trim();
- return From;
- }
- public String GetTo(String message)
- {
- int indexBeginOfTo = message.indexOf("To:");
- int indexEndOfTo = message.indexOf("rn", indexBeginOfTo);
- if ( -1 == indexBeginOfTo || -1 == indexEndOfTo )
- {
- return "";
- }
- String To = message.substring(indexBeginOfTo, indexEndOfTo);
- To = To.substring("To: ".length());
- To.trim();
- return To;
- }
- public String GetCSeq(String message)
- {
- int indexBeginOfCSeq = message.indexOf("CSeq:");
- int indexEndOfCSeq = message.indexOf("rn", indexBeginOfCSeq);
- if ( -1 == indexBeginOfCSeq || -1 == indexEndOfCSeq )
- {
- return "";
- }
- String CSeq = message.substring(indexBeginOfCSeq, indexEndOfCSeq);
- CSeq = CSeq.substring("CSeq: ".length());
- CSeq.trim();
- return CSeq;
- }
- }