View Javadoc

1   package org.asteriskjava.live.internal;
2   
3   import java.util.Date;
4   
5   import org.asteriskjava.live.AsteriskQueueEntry;
6   import org.asteriskjava.live.QueueEntryState;
7   
8   /**
9    * Default implementation of the AsteriskQueueEntry interface.
10   *
11   * @author gmi
12   */
13  class AsteriskQueueEntryImpl extends AbstractLiveObject implements AsteriskQueueEntry
14  {
15      private final AsteriskQueueImpl queue;
16      private final AsteriskChannelImpl channel;
17      private final Date dateJoined;
18  
19      private Date dateLeft;
20      private QueueEntryState state;
21  
22      // the position as given by asterisk in the queue entry or join event.
23      // we cannot work reliably with it because asterisk doesn't tell us when it shifts the entries.
24      private int reportedPosition;
25  
26      // The position of this entry in our representation of the queue. Will be set
27      // and maintained by the respective queue when the entry is added/removed/shifted
28      private int position = POSITION_UNDETERMINED;
29  
30      AsteriskQueueEntryImpl(AsteriskServerImpl server, AsteriskQueueImpl queue,
31                             AsteriskChannelImpl channel, int reportedPosition, Date dateJoined)
32      {
33          super(server);
34          this.queue = queue;
35          this.channel = channel;
36          this.dateJoined = dateJoined;
37          this.state = QueueEntryState.JOINED;
38          this.reportedPosition = reportedPosition;
39      }
40  
41      public String getChannelName()
42      {
43          return channel.getName();
44      }
45  
46      public AsteriskQueueImpl getQueue()
47      {
48          return queue;
49      }
50  
51      public AsteriskChannelImpl getChannel()
52      {
53          return channel;
54      }
55  
56      public Date getDateJoined()
57      {
58          return dateJoined;
59      }
60  
61      public Date getDateLeft()
62      {
63          return dateLeft;
64      }
65  
66      /**
67       * Sets the status to {@link QueueEntryState#LEFT} and dateLeft to the given date.
68       *
69       * @param dateLeft the date this member left the queue.
70       */
71      void left(Date dateLeft)
72      {
73          QueueEntryState oldState;
74          synchronized (this)
75          {
76              oldState = this.state;
77              this.dateLeft = dateLeft;
78              this.state = QueueEntryState.LEFT;
79          }
80          firePropertyChange(PROPERTY_STATE, oldState, state);
81      }
82  
83      public QueueEntryState getState()
84      {
85          return state;
86      }
87  
88      /**
89       * Gets the position as reported by Asterisk when the entry was created.
90       * Currently we don't update this property as the entry shifts through the queue,
91       * see getPosition() instead.
92       *
93       * @return the position of the entry in the respective queue, starting at 1
94       */
95      public int getReportedPosition()
96      {
97          return reportedPosition;
98      }
99  
100     /**
101      * Gets the position in the queue based on the queue's internal list
102      * <p/>
103      * As Asterisk doesn't send events when it shifts entries in the queue
104      * we'll base our positions on our internal queue entries ordered list.
105      * It should be coherent as entries are always added at the end of the queue
106      * and we don't mind if it is different from asterisk's view as long as the
107      * relative order stays the same. Most of the time the position will be the same
108      * but right after asterisk removes an entry it could differ as the shift occurs
109      * asynchronously in asterisk queues. As a consequence we might have temporary holes
110      * in the asterisk numbering.
111      *
112      * @return the position of the entry in the respective queue, starting at 1
113      */
114     public int getPosition()
115     {
116         return position;
117     }
118 
119     void setPosition(int position)
120     {
121         int oldPosition = this.position;
122         this.position = position;
123         firePropertyChange(PROPERTY_POSITION, oldPosition, position);
124     }
125 
126     void setReportedPosition(int reportedPosition)
127     {
128         int oldPosition = this.reportedPosition;
129         this.reportedPosition = reportedPosition;
130         firePropertyChange(PROPERTY_REPORTED_POSITION, oldPosition, reportedPosition);
131     }
132 
133     @Override
134     public String toString()
135     {
136         StringBuffer sb;
137         int systemHashcode;
138 
139         sb = new StringBuffer("AsteriskQueueEntry[");
140 
141         synchronized (this)
142         {
143             sb.append("dateJoined=").append(getDateJoined()).append(",");
144             sb.append("postition=").append(getPosition()).append(",");
145             sb.append("dateLeft=").append(getDateLeft()).append(",");
146             systemHashcode = System.identityHashCode(this);
147         }
148         if (channel != null)
149         {
150             sb.append("channel=AsteriskChannel[");
151             synchronized (channel)
152             {
153                 sb.append("id='").append(channel.getId()).append("',");
154                 sb.append("name='").append(channel.getName()).append("'],");
155             }
156         }
157         else
158         {
159             sb.append("channel=null,");
160         }
161         sb.append("systemHashcode=").append(systemHashcode);
162         sb.append("]");
163 
164         return sb.toString();
165     }
166 }