SIP Register ตอนจบ

SIP DeRegister

หลังจากที่เราได้ทำการ Register SIP Client ไปยัง SIP Server แล้ว หากอยู่ดี ๆ เราปิดโปรแกรมไปดื้อ ๆ เลย เจ้า SIP Server มันจะน้อยใจ ส่ง message Unreachable มาอยู่เรื่อย ๆ ทำให้เปลือง bandwidth  ดังนั้นเราควรจะแจ้งให้ SIP Server ทราบ หลังจากที่เราต้องการ DeRegister วิธีการ ก็เหมือนกับการ Register เพียงแต่ต่างกันตรงที่ การกำหนดค่า expire ใน contact ให้มีค่าเป็น 0 เป็นอันเรียบร้อยครับ จากนั้นเราสามารถใช้ authentication header ที่ได้มาจากตอนที่ทำ Register ครั้งแรก เอากลับมาใช้ต่อได้เลยครับ ไม่จำเป็นต้องคำนวนใหม่  ส่วน CSeq ก็ให้นับต่อจาก Register ครั้งที่แล้วได้เลย SIP Header ที่เหลือก็เอามาจาก Register header อันเก่าได้ทั้งหมดครับ

ส่วนการจบโปรแกรม เราก็ควรจะจัดการ clear ค่าตัวแปรต่าง ๆ ดังนี้ครับ

   private void shutDown() {
        try {
            try {
                Thread.sleep(3000);
            }
            catch (InterruptedException e) {    e.printStackTrace();  }

            System.out.println("nulling reference");
            sipStack.deleteListeningPoint(udpListeningPoint);

            // This will close down the stack and exit all threads
            sipProvider.removeSipListener(this);
            while (true) {
              try {
                  sipStack.deleteSipProvider(sipProvider);
                  break;
                } catch (ObjectInUseException  ex)  {
                    ex.printStackTrace();
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        continue;
                    }
               }
            }
            sipStack = null;
            sipProvider = null;
            this.optionsTid  = null;
            this.inviteTid = null;
            this.contactHeader = null;
            addressFactory = null;
            headerFactory = null;
            messageFactory = null;
            this.udpListeningPoint = null;
            this.invco = 0;
            System.gc();
            }
           catch (Exception ex) { ex.printStackTrace(); }
 }

 เวลาจะทำ DeRegister หรือการที่จะ Shutdown เราต้องแน่ใจก่อนนะครับ ว่า Process Register นั้นทำเสร็จเรียบร้อยแล้ว เพราะถ้าสั่งติด ๆ กัน มันจะทำซ้อนกัน (พร้อมกัน) มันจะมีปัญหาครับ  ผมให้ระบบรอ โดยการใช้การรอรับค่าจาก keyboard แบบง่าย ๆ ดังนี้ครับ     

  public void shell() {

       int method;
       Scanner in = new Scanner(System.in);

       System.out.print("Press 0 to deRegister: ");
       // Reads a integer from the console
       method=in.nextInt();      

       if ( method == 0 )     {
            System.out.println("Begin to de Register");
            this.callRegister(0);  // 0 = deRegister
       }

       System.out.print("Press 1 to Shutdown SIP Client: ");
       // Reads a integer from the console
       // and stores into age variable
       method=in.nextInt();

       if ( method == 1 )     {
            System.out.println("Shutdown Clip Client");
            this.shutDown();  // 0 = Shutdown SIP Client
       }
       in.close();
 }

    ถ้าใครถนัด GUI ก็ใช้ GUI ไปเลยดีกว่าครับ  :-)