private string MessageToContextualData(Message m)
 {
     return m.ToTranscriptString() + ";;";
 }
        internal void OnMediaTranscriptRecorderError(Message m)
        {
            NonBlockingConsole.WriteLine("Error message loged: " + m.ToString());

            _messages.Add(m);

            // TODO: Logic to handle errors that can happen in indiviual recorders/modality calls
        }
        internal void OnMediaTranscriptRecorderTerminated(MediaTranscriptRecorder terminatedRecorder)
        {
            // Add shutdown message for MediaTranscriptRecorder
            Message m = new Message("MediaTranscriptRecorder shutdown for MediaType: " + terminatedRecorder.RecorderType.ToString(), this._sessionId.ToString());
            this.OnMessageReceived(m);

            if (_transcriptRecorders.Contains(terminatedRecorder))
            {
                _transcriptRecorders.Remove(terminatedRecorder);
            }

            if ((_transcriptRecorders.Count == 0) || (terminatedRecorder is ConversationTranscriptRecorder)
            || (terminatedRecorder is ConferenceTranscriptRecorder))
            {
                this.Shutdown();
            }
        }
        internal void RaiseTranscriptRecorderSessionShutdown(TranscriptRecorderSessionShutdownEventArgs args)
        {
            NonBlockingConsole.WriteLine("Raising TranscriptRecorderSessionShutdown event. SessionId: {0}. ConversationId: {1}. ConferenceUri: {2}",
                args.SessionId.ToString(), (args.Conversation == null) ? "null" : args.Conversation.Id, 
                (args.Conference == null) ? "null" : args.Conference.ConferenceUri);

            Message m = new Message("Raising TranscriptRecorderSessionShutdown event. SessionId: " + _sessionId + ".",
                MessageType.Info,
                (args.Conversation == null) ? "null" : args.Conversation.Id,
                (args.Conference == null) ? "null" : args.Conference.ConferenceUri);
            this.OnMessageReceived(m);

            try
            {
                if (this.TranscriptRecorderSessionShutdown != null)
                {
                    this.TranscriptRecorderSessionShutdown.Invoke(this, args);
                }
            }
            catch (Exception e)
            {
                NonBlockingConsole.WriteLine("Error: Exception occured in RaiseTranscriptRecorderSessionShutdown(): {0}.",
                    e.ToString());
            }
        }
        internal void OnMessageReceived(Message m, string appId = null)
        {
            NonBlockingConsole.WriteLine("Message logged: " + m.ToString());

            _messages.Add(m);
            if (_localConvContextChannel != null)
            {
                ContentType contentType = new ContentType();
                _localConvContextChannel.BeginSendData(contentType, Constants.GetBytes(MessageToContextualData(m)), this.BeginContextSendDataCB, _localConvContextChannel);
            }
            if (_convContextChannel != null)
            {
                ContentType contentType = new ContentType();
                _convContextChannel.BeginSendData(contentType, Constants.GetBytes(MessageToContextualData(m)), this.BeginContextSendDataCB, _convContextChannel);
            }
        }
        public void Shutdown()
        {
            if (_state == TranscriptRecorderState.Terminated)
            {
                return;
            }
            _state = TranscriptRecorderState.Terminated;

            TranscriptRecorderSessionShutdownEventArgs shutdownArgs = null;
            lock (_transcriptRecorders)
            {
                foreach (MediaTranscriptRecorder m in _transcriptRecorders)
                {
                    m.Shutdown();
                }
                _transcriptRecorders.Clear();
            }

            try
            {
                Message shutdownMessage = new Message("TranscriptRecorderSession is shutting down. SessionId: ", _sessionId.ToString());           
                this.OnMessageReceived(shutdownMessage);

                shutdownArgs = new TranscriptRecorderSessionShutdownEventArgs(this);

                if (_conversation != null &&
                    ((_conversation.State != ConversationState.Terminating) || (_conversation.State != ConversationState.Terminated)))
                {
                    Message m = new Message("Conversation shutting down.", _conversation.LocalParticipant.DisplayName,
                        _conversation.LocalParticipant.UserAtHost, _conversation.LocalParticipant.Uri, DateTime.Now,
                        _conversation.Id, (this.Conference == null) ? "null" : this.Conference.ConferenceUri,
                        MessageType.ConversationInfo, MessageDirection.Outgoing);
                   this.OnMessageReceived(m);

                    _conversation.BeginTerminate(EndTerminateConversation, _conversation);
                    //_waitForConversationTerminated.WaitOne();
                    _conversation = null;
                }
                else
                {
                    _waitForConversationTerminated.Set();
                }

                ShutdownConversationContext();
            }
            catch (Exception e)
            {
                NonBlockingConsole.WriteLine("TranscriptRecorderSession.Shutdown(). Exception occured during conversation shutdown: {0}",
                    e.ToString());
            }
            finally
            {
                // Raise Shutdown event to MeetingTranscriptSessionManager
                this.RaiseTranscriptRecorderSessionShutdown(shutdownArgs);
            }
        }
        public void AddIMIncomingCall(CallReceivedEventArgs<InstantMessagingCall> e, CancellationTokenSource cts = null)
        {
            if (_state != TranscriptRecorderState.Active)
            {
                NonBlockingConsole.WriteLine("Warn: AddIMIncomingCall in unexpected TranscriptRecorderSession state: " + _state.ToString());
            }

            IMTranscriptRecorder i = new IMTranscriptRecorder(this);
            ConversationParticipant caller = e.RemoteParticipant;

            Message m = new Message("InstantMessaging Conversation Participant Added.", caller.DisplayName, 
                caller.UserAtHost, caller.Uri, DateTime.Now,
                _conversation.Id, _conversation.ConferenceSession.ConferenceUri,
                MessageType.ConversationInfo, MessageDirection.Outgoing);
            this.OnMessageReceived(m);

            _transcriptRecorders.Add(i);
            _conversationToCallTranscriptMapping[_conversationTranscriptRecorder].Add(i);

            i.InstantMessagingCall_Received(e);
        }
        public void AddIncomingInvitedConference(ConferenceInvitationReceivedEventArgs e, CancellationTokenSource cts = null)
        {
            if (_state != TranscriptRecorderState.Active)
            {
                NonBlockingConsole.WriteLine("Warn: AddIncomingInvitedConferece in unexpected TranscriptRecorderSession state: " + _state.ToString());
            }

            // Log Conference Invitation Recv
            ConversationParticipant caller = e.RemoteParticipant;

            Message m = new Message("Conference Started Invite Accept Started.", caller.DisplayName, caller.UserAtHost, caller.Uri, DateTime.Now,
                "null", e.Invitation.ConferenceUri,
                MessageType.ConferenceInfo, MessageDirection.Outgoing);
            this.OnMessageReceived(m);

            e.Invitation.BeginAccept(ConferenceInvitation_AcceptCompleted, e.Invitation);
        }
        public TranscriptRecorderSession(CallReceivedEventArgs<InstantMessagingCall> e, CancellationTokenSource cts = null)
        {
            _sessionId = Constants.NextGuid();
            _transcriptRecorders = new List<MediaTranscriptRecorder>();
            _conversationToCallTranscriptMapping = new Dictionary<ConversationTranscriptRecorder, List<MediaTranscriptRecorder>>();
            _messages = new List<Message>();
            _state = TranscriptRecorderState.Active;
            _conversation = e.Call.Conversation;
            _conversationTranscriptRecorder = new ConversationTranscriptRecorder(this, _conversation);
            _transcriptRecorders.Add(_conversationTranscriptRecorder);
            _conversationToCallTranscriptMapping.Add(_conversationTranscriptRecorder, new List<MediaTranscriptRecorder>());
            ConversationParticipant caller = e.RemoteParticipant;

            // Log IM conversation started
            Message m = new Message("InstantMessaging Conversation/Conference Started.", caller.DisplayName, caller.UserAtHost, caller.Uri, DateTime.Now,
                _conversation.Id, (_conversation.ConferenceSession == null) ? "null" : _conversation.ConferenceSession.ConferenceUri,
                MessageType.ConferenceInfo, MessageDirection.Outgoing);
            this.OnMessageReceived(m);

            AddIMIncomingCall(e);
        }
        public TranscriptRecorderSession(ConferenceInvitationReceivedEventArgs e, CancellationTokenSource cts = null)
        {
            _sessionId = Constants.NextGuid();
            _transcriptRecorders = new List<MediaTranscriptRecorder>();
            _conversationToCallTranscriptMapping = new Dictionary<ConversationTranscriptRecorder, List<MediaTranscriptRecorder>>();
            _messages = new List<Message>();
            _state = TranscriptRecorderState.Active;
            ConversationParticipant caller = e.RemoteParticipant;

            // Log Conference Invitation Recv
            Message m = new Message("Conference Started Invitation Recieved for ConferenceUri: ", caller.DisplayName, caller.UserAtHost, caller.Uri, DateTime.Now,
               "null", e.Invitation.ConferenceUri,
                MessageType.ConferenceInfo, MessageDirection.Outgoing);
            this.OnMessageReceived(m);

            AddIncomingInvitedConference(e);
        }