© BigSpeed Computing Inc. - Mastering algorithms


Public Class CFMain

  'Visual Basic 2005 sample of secure video chat client
  'To keep this VB.NET example simple, we are going to support
  'only the default user account (Guest)



Public Peers As New Collection
Friend ThePeer, SelPeer As CPeer

Dim FSettings As CFSettings
Dim FConnect As CFConnect
Dim FAlert As CFAlert
Dim FMakeCall As CFMakeCall
Dim FAcceptCall As CFAcceptCall
Dim FLocCam As CFLocCam
Dim FRemCam As CFRemCam



'Add a message to the log
Private Sub LogMsg(ByVal aText As String)
    Dim Line As String
    If txtLog.Text > "" Then
      Line = Chr(13) & Chr(10)
    Else
      Line = ""
    End If
    Line = Line & "[" & Now.ToLongTimeString & "] : "
    Line = Line & aText
    txtLog.SelectionStart = Len(txtLog.Text)
    txtLog.SelectedText = Line
End Sub



Friend Function PeerFromHandle(ByVal aHandle As Long) As CPeer
  Dim Pr As CPeer

  PeerFromHandle = Nothing
  For Each Pr In Peers   ' Iterate through elements.
    If Pr.Handle = aHandle Then
      PeerFromHandle = Pr
      Exit For   ' Exit loop.
   End If
Next
End Function



Public Function PickPeer(ByVal aHandle As Long) As Boolean
  ThePeer = PeerFromHandle(aHandle)
  PickPeer = Not (ThePeer Is Nothing)
End Function


Public Function SelectPeer() As Boolean
  SelectPeer = False
  If lvPeers.SelectedItems.Count = 0 Then Exit Function
  SelPeer = lvPeers.SelectedItems(0).Tag
  SelectPeer = Not (SelPeer Is Nothing)
End Function



'Update the button state and the status text
Private Sub UpdateStatus()
  If VidCln.SessionOpen Then
    'already connected
    btnConnect.Enabled = False
    btnCancel.Enabled = False
    btnDisconnect.Enabled = True
    StatusBar.Panels(0).Text = " Session open"
    StatusBar.Panels(1).Text = " Server: " + VidCln.ServerAddress + ":" + Str(VidCln.ServerPort)
    If VidCln.Username > "" Then
      StatusBar.Panels(2).Text = " User: " + VidCln.Username
    Else
      StatusBar.Panels(2).Text = " User: Guest"
    End If
  Else
    StatusBar.Panels(1).Text = ""
    StatusBar.Panels(2).Text = ""

    If VidCln.SessionOpening Then
      'now connecting
      btnConnect.Enabled = False
      btnCancel.Enabled = True
      btnDisconnect.Enabled = False
      StatusBar.Panels(0).Text = " Connecting to the server"
    Else
      'not connected
      btnConnect.Enabled = True
      btnCancel.Enabled = False
      btnDisconnect.Enabled = False
      StatusBar.Panels(0).Text = " Session closed"
    End If
  End If

  UpdateButtons()
End Sub


Private Sub UpdateButtons()
  btnAlert.Enabled = lvPeers.SelectedItems.Count > 0
  btnChat.Enabled = lvPeers.SelectedItems.Count > 0
  btnRemove.Enabled = lvPeers.SelectedItems.Count > 0

  btnMakeCall.Enabled = False
  If lvPeers.SelectedItems.Count > 0 Then
    If (VidCln.GetCallingPeer = 0) And (VidCln.GetCallPeer = 0) Then
      btnMakeCall.Enabled = True
    End If
  End If

  btnCancelCalling.Enabled = VidCln.GetCallingPeer <> 0
  btnEndCall.Enabled = VidCln.GetCallPeer <> 0
  chkMuteRecording.Enabled = btnEndCall.Enabled
  chkMutePlayback.Enabled = btnEndCall.Enabled
End Sub


  'Set the component properties
  Private Sub SetSettings()

    If FSettings.radSecret.Checked Then
      VidCln.SecurityMode = 1
    Else
      If FSettings.radPublic.Checked Then
        VidCln.SecurityMode = 2
      Else
        VidCln.SecurityMode = 0
      End If
    End If

    VidCln.SecretKey = FSettings.txtSecret.Text

    VidCln.PublicKey = FSettings.txtPublic.Text
    VidCln.PrivateKey = FSettings.txtPrivate.Text
    VidCln.Fingerprints = FSettings.txtFingerprints.Text

    VidCln.ListeningPort = Val(FSettings.txtPort.Text)
    VidCln.BindAddress = FSettings.txtAddr.Text

  End Sub




Sub DeletePeer(ByVal aPeer As CPeer)
  If aPeer Is Nothing Then Exit Sub
  Peers.Remove(Str(aPeer.Handle))
  VidCln.DisconnectPeer(aPeer.Handle)
  lvPeers.Items.Remove(aPeer.ListItem)
End Sub







  'Initializations
  Private Sub fMain_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
    FSettings = New CFSettings   'create the settings form
    FSettings.FMain = Me
    FConnect = New CFConnect     'create the connection form
    FMakeCall = New CFMakeCall
    FAcceptCall = New CFAcceptCall
    SetSettings()
    FAlert = New CFAlert         'create the alert form
    UpdateStatus()
  End Sub



Private Sub lvPeers_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvPeers.SelectedIndexChanged
  UpdateStatus()
End Sub


'Connecting to the server
Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
    If FConnect.ShowDialog <> DialogResult.OK Then Exit Sub

    If Not VidCln.OpenSession(FConnect.txtSrvAddr.Text, Val(FConnect.txtSrvPort.Text), FConnect.txtUsername.Text, FConnect.txtPassword.Text) Then
      MsgBox("Cannot initiate a new session: " + Str(VidCln.LastError))
    End If

    UpdateStatus()
End Sub


'Disconnect from the server
Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
    VidCln.CloseSession()
End Sub



Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
    If FSettings.ShowDialog <> DialogResult.OK Then Exit Sub
    SetSettings()
    UpdateStatus()
End Sub




Private Sub btnSettings_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSettings.Click
    If FSettings.ShowDialog <> DialogResult.OK Then Exit Sub
    SetSettings()
    UpdateStatus()
End Sub


Private Sub btnIP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIP.Click
    MsgBox(VidCln.LocalIPList, , "My IP address")
End Sub


Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
    End
End Sub



  ' Events



  'Just connected to the server
  Private Sub VidCln_OnSessionOpen(ByVal sender As System.Object, ByVal e As System.EventArgs)
    UpdateStatus()
    LogMsg("Connected to the server " + VidCln.ServerAddress + ":" + Str(VidCln.ServerPort))
  End Sub


  'Unsuccessful connection to the server
  Private Sub VidCln_OnSessionRejected(ByVal sender As System.Object, ByVal e As System.EventArgs)
    LogMsg("Cannot open a session to" + FConnect.txtSrvAddr.Text + ":" + FConnect.txtSrvPort.Text + "   Error- " + ErrorText(VidCln.LastError))
    MsgBox("Cannot open the session!  Error: " + ErrorText(VidCln.LastError))
    UpdateStatus()
  End Sub


  'Just disconnected from the server
  Private Sub VidCln_OnSessionClosed(ByVal sender As System.Object, ByVal e As System.EventArgs)
    UpdateStatus()
    LogMsg("Disconnected from the server")
  End Sub











' *** Alert


  Private Sub btnAlert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAlert.Click

  If Not SelectPeer() Then Exit Sub

  FAlert.txtMsg.Text = ""
  If lvPeers.SelectedItems.Count > 0 Then
    FAlert.radSingle.Text = "User " + lvPeers.SelectedItems(0).Text
    FAlert.radSingle.Enabled = True
    FAlert.radSingle.Checked = True
  Else
    FAlert.radAll.Checked = True
    FAlert.radSingle.Text = "Single user"
    FAlert.radSingle.Enabled = False
  End If

  If FAlert.ShowDialog <> DialogResult.OK Then Exit Sub

  If FAlert.radSingle.Checked Then
    VidCln.SendAlertMessage(SelPeer.Handle, FAlert.txtMsg.Text)
  Else
    For Each Pr As CPeer In Peers
      VidCln.SendAlertMessage(Pr.Handle, FAlert.txtMsg.Text)
    Next
  End If

  End Sub




' *** Chat

  Private Sub btnChat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChat.Click
    If Not SelectPeer() Then Exit Sub
    ShowForm(SelPeer.FChat)
  End Sub



Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
    If Not SelectPeer() Then Exit Sub
    DeletePeer(SelPeer)
End Sub


Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
  txtLog.Clear()
End Sub
















Private Sub chkMuteRecording_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkMuteRecording.CheckedChanged
  VidCln.MuteRecording = chkMuteRecording.Checked
End Sub


Private Sub chkMutePlayback_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkMutePlayback.CheckedChanged
  VidCln.MutePlayback = chkMutePlayback.Checked
End Sub



Private Sub btnMakeCall_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMakeCall.Click
  If Not SelectPeer() Then Exit Sub
  If FMakeCall.ShowDialog <> DialogResult.OK Then Exit Sub

  If VidCln.MakeCall(SelPeer.Handle, FMakeCall.chkVoice.Checked, FMakeCall.chkVideo.Checked) Then
    lvPeers.SelectedItems(0).SubItems(3).Text = "Calling"
  Else
    MsgBox("Cannot make a call, code: " + Str(VidCln.LastError))
  End If
  UpdateButtons()
End Sub


Private Sub btnEndCall_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEndCall.Click

  VidCln.TerminateCall(0)  'end the conversation with all peers
  lblCaller.Text = ""

  Dim i As Long
  For i = 0 To lvPeers.Items.Count - 1
    lvPeers.Items.Item(0).SubItems(3).Text = "Online"
  Next i
  UpdateButtons()
End Sub



  'A new peer is just connected
  Private Sub VidCln_OnPeerConnected(ByVal sender As System.Object, ByVal e As AxbsVidChatCln.IbsVidChatClnXEvents_OnPeerConnectedEvent) Handles VidCln.OnPeerConnected
    Dim Pr As New CPeer
    Dim LI As ListViewItem

    Pr.Handle = e.aHandle
    Peers.Add(Pr, Str(Pr.Handle))
    Pr.Username = NiceName(VidCln.GetPeerName(Pr.Handle))

    'create the chat form
    Pr.FChat = New CFChat
    Pr.FChat.FMain = Me
    Pr.FChat.Peer = Pr
    Pr.FChat.Text = "Chat with " + Pr.Username

    LI = lvPeers.Items.Add(NiceName(VidCln.GetPeerName(e.aHandle)))        'Name
    LI.SubItems.Add(VidCln.GetPeerAddress(e.aHandle))                      'Address
    LI.SubItems.Add(VidCln.GetPeerPort(e.aHandle))                      'port
    LI.SubItems.Add("Online")                                          'status
    LI.Tag = Pr
    Pr.ListItem = LI

    If lvPeers.SelectedItems.Count = 0 Then
      LI.Selected = True
    End If

    UpdateStatus()
    LogMsg("New peer at " + LI.SubItems(1).Text + ":" + LI.SubItems(2).Text)
  End Sub


  'A peer is just disconnected
Private Sub VidCln_OnPeerDisconnected(ByVal sender As System.Object, ByVal e As AxbsVidChatCln.IbsVidChatClnXEvents_OnPeerDisconnectedEvent) Handles VidCln.OnPeerDisconnected
  Dim Pr As CPeer
  Pr = PeerFromHandle(e.aHandle)
  If Pr Is Nothing Then Exit Sub
  LogMsg("Disconnected peer " + Pr.ListItem.Text + " " + Pr.ListItem.SubItems(1).Text + ":" + Pr.ListItem.SubItems(2).Text)
  DeletePeer(Pr)
End Sub



  'An alert message is available
  Private Sub VidCln_OnHaveAlertMessage(ByVal sender As System.Object, ByVal e As AxbsVidChatCln.IbsVidChatClnXEvents_OnHaveAlertMessageEvent) Handles VidCln.OnHaveAlertMessage
  'An alert message is available
    If Not PickPeer(e.aHandle) Then Exit Sub
    MsgBox(e.aText, , "Alert from " + ThePeer.Username)
  End Sub

  'A chat message is available
  Private Sub VidCln_OnHaveChatMessage(ByVal sender As System.Object, ByVal e As AxbsVidChatCln.IbsVidChatClnXEvents_OnHaveChatMessageEvent) Handles VidCln.OnHaveChatMessage
    If Not PickPeer(e.aHandle) Then Exit Sub
    ThePeer.FChat.HaveChatMessage(e.aText)
  End Sub



Private Sub VidCln_OnCallRequested(ByVal sender As System.Object, ByVal e As AxbsVidChatCln.IbsVidChatClnXEvents_OnCallRequestedEvent) Handles VidCln.OnCallRequested
  Dim Pr As CPeer
  Pr = PeerFromHandle(e.aHandle)
  If Pr Is Nothing Then Exit Sub
  LogMsg("Call is requested from " + Pr.Username + " " + Pr.ListItem.SubItems(1).Text + ":" + Pr.ListItem.SubItems(2).Text)

  FAcceptCall.Text = "Call is requested from " + Pr.Username
  If FAcceptCall.ShowDialog = DialogResult.OK Then
    e.aOkay = True
    e.aVoice = FAcceptCall.chkVoice.Checked
    e.aVideo = FAcceptCall.chkVideo.Checked
  End If
End Sub



Private Sub VidCln_OnCallTaken(ByVal sender As System.Object, ByVal e As AxbsVidChatCln.IbsVidChatClnXEvents_OnCallTakenEvent) Handles VidCln.OnCallTaken
  Dim Pr As CPeer
  Pr = PeerFromHandle(e.aHandle)
  If Pr Is Nothing Then Exit Sub
  Pr.ListItem.SubItems(3).Text = "Talking"
  lblCaller.Text = Pr.ListItem.Text
  VidCln.TriggerLevel = trkTrigger.Value
  FLocCam = Nothing
  FRemCam = Nothing
  UpdateButtons()
  LogMsg("Call from " + Pr.Username + " is taken")
End Sub



Private Sub VidCln_OnCallAccepted(ByVal sender As System.Object, ByVal e As AxbsVidChatCln.IbsVidChatClnXEvents_OnCallAcceptedEvent) Handles VidCln.OnCallAccepted
  Dim Pr As CPeer
  Pr = PeerFromHandle(e.aHandle)
  If Pr Is Nothing Then Exit Sub
  Pr.ListItem.SubItems(3).Text = "Talking"
  lblCaller.Text = Pr.ListItem.Text
  VidCln.TriggerLevel = trkTrigger.Value
  FLocCam = Nothing
  FRemCam = Nothing
  UpdateButtons()
  LogMsg("Call from " + Pr.Username + " is taken")
End Sub



Private Sub VidCln_OnCallRejected(ByVal sender As System.Object, ByVal e As AxbsVidChatCln.IbsVidChatClnXEvents_OnCallRejectedEvent) Handles VidCln.OnCallRejected
  Dim Pr As CPeer
  Pr = PeerFromHandle(e.aHandle)
  If Pr Is Nothing Then Exit Sub
  Pr.ListItem.SubItems(3).Text = "Online"
  UpdateButtons()
  MsgBox("Call from " + Pr.Username + " is rejected")
  LogMsg("Call from " + Pr.Username + " is rejected")
End Sub



Private Sub VidCln_OnCallTerminated(ByVal sender As System.Object, ByVal e As AxbsVidChatCln.IbsVidChatClnXEvents_OnCallTerminatedEvent) Handles VidCln.OnCallTerminated
  Dim Pr As CPeer
  Pr = PeerFromHandle(e.aHandle)
  If Pr Is Nothing Then Exit Sub
  lblCaller.Text = ""
  Pr.ListItem.SubItems(3).Text = "Online"
  UpdateButtons()
  MsgBox("Call from " + Pr.Username + " is terminated")
  LogMsg("Call from " + Pr.Username + " is terminated")
End Sub




Private Sub VidCln_OnLocalImage(ByVal sender As System.Object, ByVal e As AxbsVidChatCln.IbsVidChatClnXEvents_OnLocalImageEvent) Handles VidCln.OnLocalImage
  If FLocCam Is Nothing Then
    FLocCam = New CFLocCam
    FLocCam.Show()
  End If

  Dim buf As Byte()
  buf = e.aData
  Dim ms As MemoryStream
  ms = New MemoryStream(buf)
  FLocCam.PicBox.Image = New Bitmap(ms)
  If FLocCam.Width <> FLocCam.PicBox.Image.Width Then
    FLocCam.Width = FLocCam.PicBox.Image.Width
  End If
  If FLocCam.Height <> FLocCam.PicBox.Image.Height Then
    FLocCam.Height = FLocCam.PicBox.Image.Height
  End If
End Sub


Private Sub VidCln_OnRemoteImage(ByVal sender As System.Object, ByVal e As AxbsVidChatCln.IbsVidChatClnXEvents_OnRemoteImageEvent) Handles VidCln.OnRemoteImage

  If FRemCam Is Nothing Then
    FRemCam = New CFRemCam
    CFRemCam.Show()
  End If

  Dim buf As Byte()
  buf = e.aData
  Dim ms As MemoryStream
  ms = New MemoryStream(buf)
  CFRemCam.PicBox.Image = New Bitmap(ms)
  If CFRemCam.Width <> CFRemCam.PicBox.Image.Width Then
    CFRemCam.Width = CFRemCam.PicBox.Image.Width
  End If
  If CFRemCam.Height <> CFRemCam.PicBox.Image.Height Then
    CFRemCam.Height = CFRemCam.PicBox.Image.Height
  End If
End Sub







Private Sub VidCln_OnStartRecording_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VidCln.OnStartRecording
  lblRecording.Visible = True
End Sub


Private Sub VidCln_OnStopRecording_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VidCln.OnStopRecording
  lblRecording.Visible = False
End Sub


Private Sub trkTrigger_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles trkTrigger.ValueChanged
  If Not Me.Visible Then Exit Sub
  If VidCln Is Nothing Then Exit Sub
  If VidCln.GetCallPeer = 0 Then Exit Sub
  VidCln.TriggerLevel = trkTrigger.Value
End Sub

Private Sub cmbSampling_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbSampling.TextChanged
  If Not Me.Visible Then Exit Sub
  If VidCln Is Nothing Then Exit Sub
  VidCln.SamplingRate = Val(cmbSampling.Text)
End Sub

Private Sub trkRefresh_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles trkRefresh.ValueChanged
  If Not Me.Visible Then Exit Sub
  If VidCln Is Nothing Then Exit Sub
  VidCln.RefreshRate = trkRefresh.Value
End Sub

Private Sub trkQuality_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles trkQuality.ValueChanged
  If Not Me.Visible Then Exit Sub
  If VidCln Is Nothing Then Exit Sub
  VidCln.ImageQuality = trkQuality.Value
End Sub

Private Sub btnLocalCam_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLocalCam.Click
  If Not FLocCam Is Nothing Then
    FLocCam.Dispose()
  End If
  FLocCam = New CFLocCam
  FLocCam.Show()
End Sub


Private Sub btnRemoteCam_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemoteCam.Click
  If Not FRemCam Is Nothing Then
    FRemCam.Dispose()
  End If
  FRemCam = New CFRemCam
  FRemCam.Show()
End Sub

End Class


© BigSpeed Computing Inc. - Mastering algorithms