View Javadoc

1   /*
2    *  Copyright 2004-2006 Stefan Reuter
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *
16   */
17  package org.asteriskjava.live.internal;
18  
19  import java.util.Date;
20  
21  import org.asteriskjava.live.ManagerCommunicationException;
22  import org.asteriskjava.live.MeetMeUser;
23  import org.asteriskjava.live.MeetMeUserState;
24  import org.asteriskjava.manager.action.CommandAction;
25  
26  class MeetMeUserImpl extends AbstractLiveObject implements MeetMeUser
27  {
28      private static final String COMMAND_PREFIX = "meetme";
29      private static final String MUTE_COMMAND = "mute";
30      private static final String UNMUTE_COMMAND = "unmute";
31      private static final String KICK_COMMAND = "kick";
32  
33      private final MeetMeRoomImpl room;
34      private final Integer userNumber;
35      private final AsteriskChannelImpl channel;
36      private final Date dateJoined;
37  
38      private Date dateLeft;
39      private MeetMeUserState state;
40      private boolean talking;
41      private boolean muted;
42  
43      MeetMeUserImpl(AsteriskServerImpl server, MeetMeRoomImpl room, Integer userNumber,
44              AsteriskChannelImpl channel, Date dateJoined)
45      {
46          super(server);
47          this.room = room;
48          this.userNumber = userNumber;
49          this.channel = channel;
50          this.dateJoined = dateJoined;
51          this.state = MeetMeUserState.JOINED;
52      }
53  
54      public MeetMeRoomImpl getRoom()
55      {
56          return room;
57      }
58  
59      public Integer getUserNumber()
60      {
61          return userNumber;
62      }
63  
64      public AsteriskChannelImpl getChannel()
65      {
66          return channel;
67      }
68  
69      public Date getDateJoined()
70      {
71          return dateJoined;
72      }
73  
74      public Date getDateLeft()
75      {
76          return dateLeft;
77      }
78  
79      /**
80       * Sets the status to {@link MeetMeUserState#LEFT} and dateLeft to the given date.
81       * 
82       * @param dateLeft the date this user left the room.
83       */
84      void left(Date dateLeft)
85      {
86          MeetMeUserState oldState;
87          synchronized (this)
88          {
89              oldState = this.state;
90              this.dateLeft = dateLeft;
91              this.state = MeetMeUserState.LEFT;
92          }
93          firePropertyChange(PROPERTY_STATE, oldState, state);
94      }
95  
96      public MeetMeUserState getState()
97      {
98          return state;
99      }
100 
101     public boolean isTalking()
102     {
103         return talking;
104     }
105 
106     void setTalking(boolean talking)
107     {
108         boolean oldTalking = this.talking;
109         this.talking = talking;
110         firePropertyChange(PROPERTY_TALKING, oldTalking, talking);
111     }
112 
113     public boolean isMuted()
114     {
115         return muted;
116     }
117 
118     void setMuted(boolean muted)
119     {
120         boolean oldMuted = this.muted;
121         this.muted = muted;
122         firePropertyChange(PROPERTY_MUTED, oldMuted, muted);
123     }
124 
125     // action methods
126 
127     public void kick() throws ManagerCommunicationException
128     {
129         sendMeetMeUserCommand(KICK_COMMAND);
130     }
131 
132     public void mute() throws ManagerCommunicationException
133     {
134         sendMeetMeUserCommand(MUTE_COMMAND);
135     }
136 
137     public void unmute() throws ManagerCommunicationException
138     {
139         sendMeetMeUserCommand(UNMUTE_COMMAND);
140     }
141 
142     private void sendMeetMeUserCommand(String command) throws ManagerCommunicationException
143     {
144         StringBuffer sb = new StringBuffer();
145         sb.append(COMMAND_PREFIX);
146         sb.append(" ");
147         sb.append(command);
148         sb.append(" ");
149         sb.append(room.getRoomNumber());
150         sb.append(" ");
151         sb.append(userNumber);
152 
153         server.sendAction(new CommandAction(sb.toString()));
154     }
155     
156     @Override
157    public String toString()
158     {
159         StringBuffer sb;
160         int systemHashcode;
161 
162         sb = new StringBuffer("MeetMeUser[");
163 
164         synchronized (this)
165         {
166             sb.append("dateJoined='").append(getDateJoined()).append("',");
167             sb.append("dateLeft='").append(getDateLeft()).append("',");
168             sb.append("talking=").append(isTalking()).append(",");
169             sb.append("muted=").append(isMuted()).append(",");
170             sb.append("room=").append(room).append(",");
171             systemHashcode = System.identityHashCode(this);
172         }
173         sb.append("channel=AsteriskChannel[");
174         synchronized (channel)
175         {
176             sb.append("id='").append(channel.getId()).append("',");
177             sb.append("name='").append(channel.getName()).append("'],");
178         }
179         sb.append("systemHashcode=").append(systemHashcode);
180         sb.append("]");
181 
182         return sb.toString();
183     }
184 }