Package rtpi

Source Code of rtpi.RtpiSourceInfo

/* The Java RTP/I library, Version 0.1 alpha.
* This library provides the functionality of RTP/I as it is specified in
* Internet Draft draft-mauve-rtpi-00.txt.
*
* Copyright (C) 2000 Martin Mauve
* University of Mannheim / Germany
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
* Martin Mauve
*
* e-mail:
* mauve@informatik.uni-mannheim.de
*
* paper mail:
* Martin Mauve
* University of Mannheim
* Lehrstuhl Praktische Informatik IV
* L15, 16
* 68131 Mannheim
* Germany
*/

package rtpi;

import java.io.DataOutputStream;
import java.io.DataInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;

import java.util.Hashtable;

import rtpi.packets.SourceDescriptionItem;

/**
* This class holds the information about local and remote participants.
* It is thread safe.
*/

public final class RtpiSourceInfo {


    private int participantID = 0;
    private SourceDescriptionItem[] sdesItems = new SourceDescriptionItem[7];
    private Hashtable privateItems = null;
    private long lastTimeHeardFrom=System.currentTimeMillis();

    /*
     * Used by the application to create the entry for the
     * local source and used by RTCP/I to create a remote
     * source.
     *
     * @param pid The ID of the participant.
     * @param cName  The canonical name of the new source.
     */

    public RtpiSourceInfo(int pid, String cName) throws IllegalValueException {
  setCName(cName);
  participantID=pid;
    }

    /**
     * Used by the RTP/I session to create new remote source.
     * The RTP/I session does not know the cname.
     *
     * @param ssrcID The SSRC ID of the new source.
     */

    RtpiSourceInfo(int pid) {
  participantID=pid;
    }

    synchronized void heardFrom() {
  lastTimeHeardFrom=System.currentTimeMillis();
    }

    synchronized boolean timedOut(int interval) {
  if (lastTimeHeardFrom+interval<System.currentTimeMillis()) {
      return true;
  }
  return false;
    }

    // fast access to the SDES items

    synchronized SourceDescriptionItem getStandardItem(int type) throws IllegalValueException {
  if (type < 1 || type > 7) {
      throw new IllegalValueException("RtpiSourceInfo: could not get standard item of type: "+type);
  }
  return sdesItems[type-1];
    }

    synchronized void setStandardItem(SourceDescriptionItem item) throws IllegalValueException {
  int type=item.getType();
  if (type < 1 || type > 7) {
      throw new IllegalValueException("RtpiSourceInfo: could not compare standard items of type: "+type);
  }
  sdesItems[type-1]=item;
    }

    synchronized boolean checkForItemValueChange(SourceDescriptionItem item) throws IllegalValueException {
  int type=item.getType();
  if (type < 1 || type > 7) {
      throw new IllegalValueException("RtpiSourceInfo: could not compare standard items of type: "+type);
  }
  SourceDescriptionItem oldItem = sdesItems[type-1];

  if (oldItem==null) {
      if (item.getData()==null || item.getData().length==0) {
    return false;
      } else {
    return true;
      }
  }

  if (item.getData()==null || item.getData().length==0) {
      if (oldItem==null) {
    return false;
      } else {
    return true;
      }
  }
 
  byte[] data = item.getData();
  byte[] oldData = oldItem.getData();

  if (data.length != oldData.length) {
      return true;
  }

  for (int i=0; i<data.length;i++) {
      if (data[i]!=oldData[i]) {
    return true;
      }
  }

  return false;
    }
 
    /**
     * This sets the CNAME item of the participant.
     *
     * @param cName the CNAME (e.g. mauve@pandorra.cs.um.de)
     */

    public synchronized void setCName(String cName) throws IllegalValueException {
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  DataOutputStream dos = new DataOutputStream(bos);
  byte[] cNameBytes;
  try {
      dos.writeUTF(cName);
      dos.flush();
      bos.flush();
      cNameBytes = bos.toByteArray();
      dos.close();
      bos.close();
  } catch (Exception ex) {
      throw new IllegalValueException("RtcpiSourceInfo: Error while convertig cName to UTF");
  }
  sdesItems[0] = new SourceDescriptionItem(SourceDescriptionItem.CNAME,cNameBytes);
    }

    /**
     * This returns the CNAME item of the participant.
     *
     * @return The CNAME.
     */

    public synchronized String getCName() throws IllegalValueException {
  if(sdesItems[0]==null) {
      return null;
  }
  String cName;
  byte[] cNameBytes=sdesItems[0].getData();
  ByteArrayInputStream bis = new ByteArrayInputStream(cNameBytes);
  DataInputStream dis = new DataInputStream(bis);
  try {
      cName=dis.readUTF();
      dis.close();
      bis.close();
  } catch (Exception ex) {
      throw new IllegalValueException("RtcpiSourceInfo: Error while convertig cName to UTF");
  }
  return cName;
    }

   
    /**
     * This sets the NAME item of the participant.
     *
     * @param name the NAME (e.g. Martin Mauve (University of Mannheim))
     */

    public synchronized void setName(String name) throws IllegalValueException {
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  DataOutputStream dos = new DataOutputStream(bos);
  byte[] nameBytes;
  try {
      dos.writeUTF(name);
      dos.flush();
      bos.flush();
      nameBytes = bos.toByteArray();
      dos.close();
      bos.close();
  } catch (Exception ex) {
      throw new IllegalValueException("RtcpiSourceInfo: Error while convertig Name to UTF");
  }
  sdesItems[1] = new SourceDescriptionItem(SourceDescriptionItem.NAME,nameBytes);
    }

    /**
     * This clears the item.
     */

    public synchronized void clearName() {
  sdesItems[1]=null;
    }
  
    /**
     * This returns the NAME item of the participant.
     *
     * @return The NAME.
     */

    public synchronized String getName() throws IllegalValueException {
  if(sdesItems[1]==null) {
      return null;
  }
  String name;
  byte[] nameBytes=sdesItems[1].getData();
  ByteArrayInputStream bis = new ByteArrayInputStream(nameBytes);
  DataInputStream dis = new DataInputStream(bis);
  try {
      name=dis.readUTF();
      dis.close();
      bis.close();
  } catch (Exception ex) {
      throw new IllegalValueException("RtcpiSourceInfo: Error while convertig name to UTF");
  }
  return name;
    }

    /**
     * This sets the EMAIL item of the participant.
     *
     * @param email The EMAIL.
     */

    public synchronized void setEmail(String email) throws IllegalValueException {
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  DataOutputStream dos = new DataOutputStream(bos);
  byte[] emailBytes;
  try {
      dos.writeUTF(email);
      dos.flush();
      bos.flush();
      emailBytes = bos.toByteArray();
      dos.close();
      bos.close();
  } catch (Exception ex) {
      throw new IllegalValueException("RtcpiSourceInfo: Error while convertig email to UTF");
  }
  sdesItems[2] = new SourceDescriptionItem(SourceDescriptionItem.EMAIL,emailBytes);
    }

    /**
     * This clears the item.
     */

    public synchronized void clearEmail() {
  sdesItems[2]=null;
    }
  
    /**
     * This returns the EMAIL item of the participant.
     *
     * @return The EMAIL.
     */

    public synchronized String getEmail() throws IllegalValueException {
  if(sdesItems[2]==null) {
      return null;
  }
  String email;
  byte[] emailBytes=sdesItems[2].getData();
  ByteArrayInputStream bis = new ByteArrayInputStream(emailBytes);
  DataInputStream dis = new DataInputStream(bis);
  try {
      email=dis.readUTF();
      dis.close();
      bis.close();
  } catch (Exception ex) {
      throw new IllegalValueException("RtcpiSourceInfo: Error while convertig email to UTF");
  }
  return email;
    }

    /**
     * This sets the PHONE item of the participant.
     *
     * @param phone The PHONE.
     */

    public synchronized void setPhone(String phone) throws IllegalValueException {
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  DataOutputStream dos = new DataOutputStream(bos);
  byte[] phoneBytes;
  try {
      dos.writeUTF(phone);
      dos.flush();
      bos.flush();
      phoneBytes = bos.toByteArray();
      dos.close();
      bos.close();
  } catch (Exception ex) {
      throw new IllegalValueException("RtcpiSourceInfo: Error while convertig phone to UTF");
  }
  sdesItems[3] = new SourceDescriptionItem(SourceDescriptionItem.PHONE,phoneBytes);
    }

    /**
     * This clears the item.
     */

    public synchronized void clearPhone() {
  sdesItems[3]=null;
    }
  
    /**
     * This returns the PHONE item of the participant.
     *
     * @return The PHONE.
     */

    public synchronized String getPhone() throws IllegalValueException {
  if(sdesItems[3]==null) {
      return null;
  }
  String phone;
  byte[] phoneBytes=sdesItems[3].getData();
  ByteArrayInputStream bis = new ByteArrayInputStream(phoneBytes);
  DataInputStream dis = new DataInputStream(bis);
  try {
      phone=dis.readUTF();
      dis.close();
      bis.close();
  } catch (Exception ex) {
      throw new IllegalValueException("RtcpiSourceInfo: Error while convertig phone to UTF");
  }
  return phone;
    }

    /**
     * This sets the LOC item of the participant.
     *
     * @param loc The location.
     */

    public synchronized void setLoc(String loc) throws IllegalValueException {
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  DataOutputStream dos = new DataOutputStream(bos);
  byte[] locBytes;
  try {
      dos.writeUTF(loc);
      dos.flush();
      bos.flush();
      locBytes = bos.toByteArray();
      dos.close();
      bos.close();
  } catch (Exception ex) {
      throw new IllegalValueException("RtcpiSourceInfo: Error while convertig loc to UTF");
  }
  sdesItems[4] = new SourceDescriptionItem(SourceDescriptionItem.LOC,locBytes);
    }

    /**
     * This clears the item.
     */

    public synchronized void clearLoc() {
  sdesItems[4]=null;
    }
  
    /**
     * This returns the LOC item of the participant.
     *
     * @return The location.
     */

    public synchronized String getLoc() throws IllegalValueException {
  if(sdesItems[4]==null) {
      return null;
  }
  String loc;
  byte[] locBytes=sdesItems[4].getData();
  ByteArrayInputStream bis = new ByteArrayInputStream(locBytes);
  DataInputStream dis = new DataInputStream(bis);
  try {
      loc=dis.readUTF();
      dis.close();
      bis.close();
  } catch (Exception ex) {
      throw new IllegalValueException("RtcpiSourceInfo: Error while convertig loc to UTF");
  }
  return loc;
    }

    /**
     * This sets the TOOL item of the participant.
     *
     * @param tool The TOOL.
     */

    public synchronized void setTool(String tool) throws IllegalValueException {
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  DataOutputStream dos = new DataOutputStream(bos);
  byte[] toolBytes;
  try {
      dos.writeUTF(tool);
      dos.flush();
      bos.flush();
      toolBytes = bos.toByteArray();
      dos.close();
      bos.close();
  } catch (Exception ex) {
      throw new IllegalValueException("RtcpiSourceInfo: Error while convertig tool to UTF");
  }
  sdesItems[5] = new SourceDescriptionItem(SourceDescriptionItem.TOOL,toolBytes);
    }

    /**
     * This clears the item.
     */

    public synchronized void clearTool() {
  sdesItems[5]=null;
    }
  
    /**
     * This returns the TOOL item of the participant.
     *
     * @return The TOOL.
     */

    public synchronized String getTool() throws IllegalValueException {
  if(sdesItems[5]==null) {
      return null;
  }
  String tool;
  byte[] toolBytes=sdesItems[5].getData();
  ByteArrayInputStream bis = new ByteArrayInputStream(toolBytes);
  DataInputStream dis = new DataInputStream(bis);
  try {
      tool=dis.readUTF();
      dis.close();
      bis.close();
  } catch (Exception ex) {
      throw new IllegalValueException("RtcpiSourceInfo: Error while convertig tool to UTF");
  }
  return tool;
    }

    /**
     * This sets the NOTE item of the participant. Please notice that the NOTE item
     * is transmitted with high priority. It should be cleared as soon as the NOTE
     * is no longer needed. The value of the note item for a participant should
     * be presented on the user interface.
     *
     * @param note The NOTE.
     */

    public synchronized void setNote(String note) throws IllegalValueException {
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  DataOutputStream dos = new DataOutputStream(bos);
  byte[] noteBytes;
  try {
      dos.writeUTF(note);
      dos.flush();
      bos.flush();
      noteBytes = bos.toByteArray();
      dos.close();
      bos.close();
  } catch (Exception ex) {
      throw new IllegalValueException("RtcpiSourceInfo: Error while convertig tool to UTF");
  }
  sdesItems[6] = new SourceDescriptionItem(SourceDescriptionItem.NOTE,noteBytes);
    }

    /**
     * This clears the item.
     */

    public synchronized void clearNote() {
  sdesItems[6]=null;
    }
  
    /**
     * This returns the NOTE item of the participant.
     *
     * @return The NOTE.
     */

    public synchronized String getNote() throws IllegalValueException {
  if(sdesItems[6]==null) {
      return null;
  }
  String note;
  byte[] noteBytes=sdesItems[6].getData();
  ByteArrayInputStream bis = new ByteArrayInputStream(noteBytes);
  DataInputStream dis = new DataInputStream(bis);
  try {
      note=dis.readUTF();
      dis.close();
      bis.close();
  } catch (Exception ex) {
      throw new IllegalValueException("RtcpiSourceInfo: Error while convertig note to UTF");
  }
  return note;
    }

    /**
     * This returns the ID of the participant.
     *
     * @return The participant ID.
     */

    public synchronized int getParticipantID() {
  return participantID;
    }
}

TOP

Related Classes of rtpi.RtpiSourceInfo

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.