/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sip.address.Address;
import javax.sip.address.AddressFactory;
import javax.sip.address.URI;
import javax.sip.header.AllowHeader;
import javax.sip.header.CSeqHeader;
import javax.sip.header.CallIdHeader;
import javax.sip.header.ContactHeader;
import javax.sip.header.ExpiresHeader;
import javax.sip.header.FromHeader;
import javax.sip.header.HeaderFactory;
import javax.sip.header.MaxForwardsHeader;
import javax.sip.header.ToHeader;
import javax.sip.header.ViaHeader;
import javax.sip.message.MessageFactory;
import javax.sip.message.Request;
import logger.OhmLogger;

/**
 *
 * @author Jan
 */
public class SIPRegister
{
  private Request request;
  private static final Logger lgRegister = OhmLogger.getLogger();
  
  public SIPRegister(String proxyAddress, String myTag, String myIPAddress, int myPort,
                     int branch, AddressFactory addressFactory, HeaderFactory headerFactory,
                     MessageFactory messageFactory, ContactHeader contactHeader, CallIdHeader callID)
  {
    try
    {
      Address addressto = addressFactory.createAddress("sip:" + proxyAddress); //evtl. mit Port: sip:192.168.100.11:5060
      ToHeader to = headerFactory.createToHeader(addressto, null); // Integer.toString(myTag)  address + myTag
      URI requestURI = addressto.getURI();
      FromHeader from = headerFactory.createFromHeader(addressto, myTag); //this.contactAddress
      ArrayList via = new ArrayList(); //Via needs a List as input
      ViaHeader viaheader = headerFactory.createViaHeader(myIPAddress, myPort, "UDP", String.valueOf(branch));
      via.add(viaheader);
      CSeqHeader cSeq = headerFactory.createCSeqHeader(1L, "REGISTER");
      MaxForwardsHeader maxForwards = headerFactory.createMaxForwardsHeader(70);
      AllowHeader allow = headerFactory.createAllowHeader("NOTIFY,INVITE,ACK,CANCEL,BYE,REFER,INFO,OPTIONS,MESSAGE");
      ExpiresHeader expire = headerFactory.createExpiresHeader(3600); //int value 
      request = messageFactory.createRequest(requestURI, "REGISTER", callID, cSeq, from, to, via, maxForwards);
      request.addHeader(contactHeader);
      request.addHeader(allow);
      request.addHeader(expire);
      lgRegister.info("Register Header erstellt");
    }
    catch (Exception ex)
    {
      lgRegister.getLogger(SIPmodel.class.getName()).log(Level.SEVERE, null, ex);
    }
  }

  /**
   * @return the request
   */
  public Request getRequest()
  {

    return request;
  }
}
