Imports System.Net.Sockets
Imports System.Threading
Imports System.Text

Public Class Form1
    Inherits System.Windows.Forms.Form
    Private XMLFileName As String = "connect_info.xml"
    Private ConPortNumber As Integer = GetXmlValue("portnumber") '8777
#Region " Windows フォーム デザイナで生成されたコード "

    Public Sub New()
        MyBase.New()

        ' この呼び出しは Windows フォーム デザイナで必要です。
        InitializeComponent()

        ' InitializeComponent() 呼び出しの後に初期化を追加します。

    End Sub

    ' Form は dispose をオーバーライドしてコンポーネント一覧を消去します。
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    ' Windows フォーム デザイナで必要です。
    Private components As System.ComponentModel.IContainer

    ' メモ : 以下のプロシージャは、Windows フォーム デザイナで必要です。
    ' Windows フォーム デザイナを使って変更してください。  
    ' コード エディタは使用しないでください。
    Friend WithEvents txtHost As System.Windows.Forms.TextBox
    Friend WithEvents Label5 As System.Windows.Forms.Label
    Friend WithEvents btnConnect As System.Windows.Forms.Button
    Friend WithEvents txtInfo As System.Windows.Forms.TextBox
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents txtPort As System.Windows.Forms.TextBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents txtDir As System.Windows.Forms.TextBox
    Friend WithEvents lblStatus As System.Windows.Forms.Label
    Friend WithEvents btnSend As System.Windows.Forms.Button
    Friend WithEvents btnStart As System.Windows.Forms.Button
    Friend WithEvents btnStop As System.Windows.Forms.Button
    Friend WithEvents btnClear As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.txtHost = New System.Windows.Forms.TextBox()
        Me.Label5 = New System.Windows.Forms.Label()
        Me.btnConnect = New System.Windows.Forms.Button()
        Me.txtInfo = New System.Windows.Forms.TextBox()
        Me.GroupBox1 = New System.Windows.Forms.GroupBox()
        Me.lblStatus = New System.Windows.Forms.Label()
        Me.btnSend = New System.Windows.Forms.Button()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.txtDir = New System.Windows.Forms.TextBox()
        Me.Label4 = New System.Windows.Forms.Label()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.txtPort = New System.Windows.Forms.TextBox()
        Me.btnStart = New System.Windows.Forms.Button()
        Me.btnStop = New System.Windows.Forms.Button()
        Me.btnClear = New System.Windows.Forms.Button()
        Me.GroupBox1.SuspendLayout()
        Me.SuspendLayout()
        '
        'txtHost
        '
        Me.txtHost.Location = New System.Drawing.Point(56, 8)
        Me.txtHost.Name = "txtHost"
        Me.txtHost.Size = New System.Drawing.Size(72, 19)
        Me.txtHost.TabIndex = 23
        Me.txtHost.Text = "Localhost"
        '
        'Label5
        '
        Me.Label5.Location = New System.Drawing.Point(8, 8)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(48, 16)
        Me.Label5.TabIndex = 24
        Me.Label5.Text = "アドレス"
        '
        'btnConnect
        '
        Me.btnConnect.Location = New System.Drawing.Point(144, 8)
        Me.btnConnect.Name = "btnConnect"
        Me.btnConnect.Size = New System.Drawing.Size(136, 24)
        Me.btnConnect.TabIndex = 1
        Me.btnConnect.Text = "サーバー情報の取得"
        '
        'txtInfo
        '
        Me.txtInfo.Location = New System.Drawing.Point(8, 232)
        Me.txtInfo.Multiline = True
        Me.txtInfo.Name = "txtInfo"
        Me.txtInfo.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.txtInfo.Size = New System.Drawing.Size(280, 80)
        Me.txtInfo.TabIndex = 26
        Me.txtInfo.Text = ""
        '
        'GroupBox1
        '
        Me.GroupBox1.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblStatus, Me.btnSend, Me.Label1, Me.txtDir, Me.Label4, Me.Label2, Me.txtPort})
        Me.GroupBox1.Location = New System.Drawing.Point(8, 32)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(280, 160)
        Me.GroupBox1.TabIndex = 31
        Me.GroupBox1.TabStop = False
        '
        'lblStatus
        '
        Me.lblStatus.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.lblStatus.Location = New System.Drawing.Point(8, 136)
        Me.lblStatus.Name = "lblStatus"
        Me.lblStatus.Size = New System.Drawing.Size(120, 16)
        Me.lblStatus.TabIndex = 33
        Me.lblStatus.Text = "停止しています"
        '
        'btnSend
        '
        Me.btnSend.Location = New System.Drawing.Point(168, 128)
        Me.btnSend.Name = "btnSend"
        Me.btnSend.Size = New System.Drawing.Size(104, 24)
        Me.btnSend.TabIndex = 32
        Me.btnSend.Text = "設定を反映"
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(8, 88)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(120, 16)
        Me.Label1.TabIndex = 31
        Me.Label1.Text = "Webで公開するフォルダ"
        '
        'txtDir
        '
        Me.txtDir.Location = New System.Drawing.Point(8, 104)
        Me.txtDir.Name = "txtDir"
        Me.txtDir.Size = New System.Drawing.Size(264, 19)
        Me.txtDir.TabIndex = 30
        Me.txtDir.Text = ""
        '
        'Label4
        '
        Me.Label4.Font = New System.Drawing.Font("MS UI Gothic", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
        Me.Label4.Location = New System.Drawing.Point(8, 40)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(264, 32)
        Me.Label4.TabIndex = 24
        Me.Label4.Text = "ポート番号を変更した場合、Webサーバーのプログラムを完全にシャットダウンして再起動する必要があります。"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(8, 16)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(64, 16)
        Me.Label2.TabIndex = 23
        Me.Label2.Text = "ポート番号"
        '
        'txtPort
        '
        Me.txtPort.Location = New System.Drawing.Point(72, 16)
        Me.txtPort.Name = "txtPort"
        Me.txtPort.Size = New System.Drawing.Size(40, 19)
        Me.txtPort.TabIndex = 22
        Me.txtPort.Text = ""
        Me.txtPort.TextAlign = System.Windows.Forms.HorizontalAlignment.Right
        '
        'btnStart
        '
        Me.btnStart.Location = New System.Drawing.Point(64, 200)
        Me.btnStart.Name = "btnStart"
        Me.btnStart.Size = New System.Drawing.Size(64, 24)
        Me.btnStart.TabIndex = 2
        Me.btnStart.Text = "開始"
        '
        'btnStop
        '
        Me.btnStop.Location = New System.Drawing.Point(160, 200)
        Me.btnStop.Name = "btnStop"
        Me.btnStop.Size = New System.Drawing.Size(64, 24)
        Me.btnStop.TabIndex = 3
        Me.btnStop.Text = "停止"
        '
        'btnClear
        '
        Me.btnClear.Location = New System.Drawing.Point(216, 312)
        Me.btnClear.Name = "btnClear"
        Me.btnClear.Size = New System.Drawing.Size(72, 24)
        Me.btnClear.TabIndex = 32
        Me.btnClear.Text = "表示のクリア"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12)
        Me.ClientSize = New System.Drawing.Size(296, 333)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnClear, Me.btnStop, Me.btnStart, Me.GroupBox1, Me.txtInfo, Me.btnConnect, Me.txtHost, Me.Label5})
        Me.MaximizeBox = False
        Me.Name = "Form1"
        Me.Text = "Webサーバーマネージャ"
        Me.GroupBox1.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        btnSend.Enabled = False
        txtHost.Text = GetXmlValue("hostname")
        txtPort.Text = ""
        txtDir.Text = ""
        lblStatus.Text = ""
    End Sub

    Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
        'サーバーへ接続
        Dim TcpHost As New TcpClient()
        TcpHost.Connect(txtHost.Text, ConPortNumber)

        Dim nStream As NetworkStream = TcpHost.GetStream()
        Dim strSendData As String = "system_message /getinfo/"
        Dim sendBytes As [Byte]() = Encoding.Default.GetBytes(strSendData)
        nStream.Write(sendBytes, 0, sendBytes.Length)
        Dim sBuf As String
        Dim ByteData(TcpHost.ReceiveBufferSize) As Byte
        nStream.Read(ByteData, 0, CInt(TcpHost.ReceiveBufferSize)) 'サーバーからの受信
        sBuf = Encoding.Default.GetString(ByteData)
        If sBuf <> "" Then
            txtInfo.Text = txtInfo.Text & vbCrLf & sBuf
            If InStr(sBuf, "<?xml") > 0 Then
                txtPort.Text = GetXml(sBuf, "portnumber")
                txtDir.Text = GetXml(sBuf, "dir")
                If GetXml(sBuf, "status") = 1 Then
                    lblStatus.Text = "開始しています"
                Else
                    lblStatus.Text = "停止しています"
                End If
            End If
        End If
        TcpHost.Close()
        WriteXML("hostname", txtHost.Text)
        btnSend.Enabled = True
    End Sub

    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
        If MessageBox.Show("変更はWebサーバーを再起動したときに反映されます。", "Webサーバーマネージャ", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = DialogResult.Cancel Then Exit Sub

        'サーバーへ接続
        Dim TcpHost As New TcpClient()
        TcpHost.Connect(txtHost.Text, ConPortNumber)

        Dim strParam As String
        strParam = "<?xml version=""1.0""?>" & "<webserver>"
        strParam = strParam & "<portnumber>" & txtPort.Text & "</portnumber>"
        strParam = strParam & "<dir>" & txtDir.Text & "</dir>"
        strParam = strParam & "</webserver>"

        'サーバーへ送信
        Dim nStream As NetworkStream = TcpHost.GetStream()
        Dim strSendData As String = "system_message /send/" & strParam
        Dim sendBytes As [Byte]() = Encoding.Default.GetBytes(strSendData)
        If Not nStream.CanWrite Then Exit Sub
        nStream.Write(sendBytes, 0, sendBytes.Length)
        Dim sBuf As String
        Dim ByteData(TcpHost.ReceiveBufferSize) As Byte
        nStream.Read(ByteData, 0, CInt(TcpHost.ReceiveBufferSize)) 'サーバーからの受信
        sBuf = Encoding.Default.GetString(ByteData)
        If sBuf <> "" Then
            txtInfo.Text = txtInfo.Text & vbCrLf & sBuf
        End If
        TcpHost.Close()

    End Sub
    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        'サーバーへ接続
        Dim TcpHost As New TcpClient()
        TcpHost.Connect(txtHost.Text, ConPortNumber)

        'サーバーへ送信
        Dim nStream As NetworkStream = TcpHost.GetStream()
        Dim strSendData As String = "system_message /start/"
        Dim sendBytes As [Byte]() = Encoding.Default.GetBytes(strSendData)
        If Not nStream.CanWrite Then Exit Sub
        nStream.Write(sendBytes, 0, sendBytes.Length)
        Dim sBuf As String
        Dim ByteData(TcpHost.ReceiveBufferSize) As Byte
        nStream.Read(ByteData, 0, CInt(TcpHost.ReceiveBufferSize)) 'サーバーからの受信
        sBuf = Encoding.Default.GetString(ByteData)
        If sBuf <> "" Then
            txtInfo.Text = txtInfo.Text & vbCrLf & sBuf
        End If
        TcpHost.Close()

        lblStatus.Text = "開始しています"
    End Sub
    Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
        'サーバーへ接続
        Dim TcpHost As New TcpClient()
        TcpHost.Connect(txtHost.Text, ConPortNumber)

        'サーバーへ送信
        Dim nStream As NetworkStream = TcpHost.GetStream()
        Dim strSendData As String = "system_message /stop/"
        Dim sendBytes As [Byte]() = Encoding.Default.GetBytes(strSendData)
        If Not nStream.CanWrite Then Exit Sub
        nStream.Write(sendBytes, 0, sendBytes.Length)
        Dim sBuf As String
        Dim ByteData(TcpHost.ReceiveBufferSize) As Byte
        nStream.Read(ByteData, 0, CInt(TcpHost.ReceiveBufferSize)) 'サーバーからの受信
        sBuf = Encoding.Default.GetString(ByteData)
        If sBuf <> "" Then
            txtInfo.Text = txtInfo.Text & vbCrLf & sBuf
        End If
        TcpHost.Close()

        lblStatus.Text = "停止しています"

    End Sub

    Public Function GetXmlValue(ByVal sTagName As String) As String
        Dim xmlDoc As New Xml.XmlDocument()
        xmlDoc.Load(XMLFileName)
        Dim xmlNList As Xml.XmlNodeList = xmlDoc.GetElementsByTagName(sTagName)
        GetXmlValue = ""
        If xmlNList.Count > 0 Then
            If Not (xmlNList(0).FirstChild Is Nothing) Then
                GetXmlValue = xmlNList(0).FirstChild.Value
            End If
        End If
    End Function
    Private Function GetXml(ByVal sXMLString As String, ByVal sTagName As String) As String
        Dim xmlDoc As New Xml.XmlDocument()
        xmlDoc.LoadXml(sXMLString)
        Dim xmlNList As Xml.XmlNodeList = xmlDoc.GetElementsByTagName(sTagName)
        GetXml = ""
        If xmlNList.Count > 0 Then
            If Not (xmlNList(0).FirstChild Is Nothing) Then
                GetXml = xmlNList(0).FirstChild.Value
            End If
        End If
    End Function
    Private Sub WriteXML(ByVal sTagName As String, ByVal sTagValue As String)
        Dim xmlDoc As Xml.XmlDocument
        Dim xmlChd As Xml.XmlElement
        Dim xmlTxt As Xml.XmlText
        If sTagName = "" Or sTagValue = "" Then Exit Sub

        '*** xmlファイルに書き込み
        xmlDoc = New Xml.XmlDocument()
        xmlDoc.Load(XMLFileName)
        Dim xmlNList As Xml.XmlNodeList = xmlDoc.GetElementsByTagName(sTagName)
        If xmlNList.Count > 0 Then
            xmlChd = xmlNList(0)
            xmlTxt = xmlDoc.CreateTextNode(sTagValue)
            xmlChd.RemoveAll()
            xmlChd.AppendChild(xmlTxt)
        Else
            xmlChd = xmlDoc.CreateElement(sTagName)
            xmlTxt = xmlDoc.CreateTextNode(sTagValue)
            xmlChd.AppendChild(xmlTxt)
            Dim xmlDocEle As Xml.XmlElement = xmlDoc.DocumentElement
            xmlDocEle.AppendChild(xmlChd)
        End If
        xmlDoc.Save(XMLFileName)
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        txtInfo.Text = ""
    End Sub

End Class
