Skip to content

Commit

Permalink
Communicator: refactoring, cleaning and unification (Serg-Norseman/GE…
Browse files Browse the repository at this point in the history
  • Loading branch information
Serg-Norseman committed Apr 20, 2018
1 parent d9436a0 commit c3e6f9c
Show file tree
Hide file tree
Showing 51 changed files with 1,242 additions and 1,237 deletions.
11 changes: 5 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@
/GKNetPNRP/bin/
/GKNetPNRP/obj/

/GKSimpleChat/bin/
/GKSimpleChat/obj/
/GKSimpleChat/temp/
/GKCommunicatorApp/bin/
/GKCommunicatorApp/obj/

/GKCommunicatorPlugin/bin/
/GKCommunicatorPlugin/obj/

/DHTSample/bin/
/DHTSample/obj/

/TCPChatTest/bin/
/TCPChatTest/obj/

/temp/

testrun_git.mswin.cmd
16 changes: 4 additions & 12 deletions DHTSample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
using System;
using System.IO;
using System.Net;
using BencodeNET.Objects;
using GKNet.Core;
using GKNet.DHT;
using GKNet.Protocol;

namespace DHTSample
{
class Program : ILogger
{
public const string NETWORK_SIGN = "GEDKEEPER NETWORK";

static int Main(string[] args)
{
var program = new Program();
Expand All @@ -20,18 +18,13 @@ static int Main(string[] args)
port = DHTClient.PublicDHTPort;
}

BDictionary subnetKey = new BDictionary();
subnetKey.Add("info", NETWORK_SIGN);
var snkInfoHash = DHTHelper.CalculateInfoHashBytes(subnetKey);
Console.ForegroundColor = ConsoleColor.White;
var snkInfoHash = ProtocolHelper.CreateSignInfoKey();
program.WriteLog("Search for: " + snkInfoHash.ToHexString());

var dhtClient = new DHTClient(IPAddress.Any, port, program);
dhtClient.PeersFound += delegate (object sender, PeersFoundEventArgs e) {
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Found peers: {0}", e.Peers.Count);
Console.WriteLine("Peers[0]: {0}", e.Peers[0].ToString());
Console.ResetColor();
program.WriteLog(string.Format("Found peers: {0}", e.Peers.Count));
program.WriteLog(string.Format("Peers[0]: {0}", e.Peers[0].ToString()));
};

Console.CancelKeyPress += delegate (object sender, ConsoleCancelEventArgs e) {
Expand All @@ -51,7 +44,6 @@ public void WriteLog(string str, bool display = true)
{
if (display) {
Console.WriteLine(str);
Console.ResetColor();
}

var fswriter = new StreamWriter(new FileStream("./logFile", FileMode.Append));
Expand Down
339 changes: 339 additions & 0 deletions GKCommunicatorApp/ChatForm.Designer.cs

Large diffs are not rendered by default.

43 changes: 32 additions & 11 deletions GKSimpleChat/ChatForm.cs → GKCommunicatorApp/ChatForm.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
using System;
/*
* "GKCommunicator", the chat and bulletin board of the genealogical network.
* Copyright (C) 2018 by Sergey V. Zhdanovskih.
*
* This file is part of "GEDKeeper".
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.ComponentModel;
using System.ServiceModel;
using System.ServiceModel.PeerResolvers;
Expand Down Expand Up @@ -69,7 +89,7 @@ public void Join(string Member)
{
Invoke(
(MethodInvoker)delegate {
lstChatMsgs.Items.Add(Member + " joined the chatroom.");
lstChatMsgs.AppendText(Member + " joined the chatroom.");

// this will retrieve any new members that have joined before the current user
fChannel.SynchronizeMemberList(fMemberName);
Expand All @@ -80,7 +100,7 @@ public void Chat(string Member, string Message)
{
Invoke(
(MethodInvoker)delegate {
lstChatMsgs.Items.Add(Member + " says: " + Message);
lstChatMsgs.AppendText(Member + " says: " + Message);
});
}

Expand All @@ -98,7 +118,7 @@ public void Whisper(string Member, string MemberTo, string Message)
//that in the future but for now i'm too busy with other things to mess with it hence it's
//left as an exercise for the reader.
if (fMemberName.Equals(Member) || fMemberName.Equals(MemberTo)) {
lstChatMsgs.Items.Add(Member + " whispers: " + Message);
lstChatMsgs.AppendText(Member + " whispers: " + Message);
}
});
}
Expand All @@ -112,7 +132,7 @@ public void Leave(string Member)
{
Invoke(
(MethodInvoker)delegate {
lstChatMsgs.Items.Add(Member + " left the chatroom.");
lstChatMsgs.AppendText(Member + " left the chatroom.");
});
}

Expand Down Expand Up @@ -146,7 +166,7 @@ private void btnConnect_Click(object sender, EventArgs e)
executor.BeginInvoke(null, null);
}

private void btnChat_Click(object sender, EventArgs e)
private void btnSendToAll_Click(object sender, EventArgs e)
{
// broadcast the chat message to the peer mesh and clear the box
if (!String.IsNullOrEmpty(txtChatMsg.Text)) {
Expand All @@ -156,7 +176,7 @@ private void btnChat_Click(object sender, EventArgs e)
}
}

private void btnWhisper_Click(object sender, EventArgs e)
private void btnSend_Click(object sender, EventArgs e)
{
// broadcast the chat message to the peer mesh with the member name it is intended for
if ((!String.IsNullOrEmpty(txtChatMsg.Text)) && (lstMembers.SelectedIndex >= 0)) {
Expand Down Expand Up @@ -191,13 +211,14 @@ void ostat_Online(object sender, EventArgs e)
});
}

[STAThread]
static void Main()
private void miDHTLog_Click(object sender, EventArgs e)
{
Application.Run(new ChatForm());
using (var dlg = new DHTLogDlg()) {
dlg.ShowDialog();
}
}

private void btnSysInfo_Click(object sender, EventArgs e)
private void miSysInfo_Click(object sender, EventArgs e)
{
using (var dlgSysInfo = new SysInfoWin()) {
dlgSysInfo.ShowDialog();
Expand Down
64 changes: 64 additions & 0 deletions GKCommunicatorApp/DHTLogDlg.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 74 additions & 0 deletions GKCommunicatorApp/DHTLogDlg.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* "GKCommunicator", the chat and bulletin board of the genealogical network.
* Copyright (C) 2018 by Sergey V. Zhdanovskih.
*
* This file is part of "GEDKeeper".
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

using System.Drawing;
using System.IO;
using System.Net;
using System.Windows.Forms;
using GKNet.Core;
using GKNet.DHT;
using GKNet.Protocol;

namespace GKSimpleChat
{
public partial class DHTLogDlg : Form, ILogger
{
private DHTClient fDHTClient;

public DHTLogDlg()
{
InitializeComponent();

int port = DHTClient.PublicDHTPort;
fDHTClient = new DHTClient(IPAddress.Any, port, this);
fDHTClient.PeersFound += delegate (object sender, PeersFoundEventArgs e) {
WriteLog(string.Format("Found peers: {0}", e.Peers.Count));
WriteLog(string.Format("Peers[0]: {0}", e.Peers[0].ToString()));
};
}

private void DHTLogDlg_Load(object sender, System.EventArgs e)
{
var snkInfoHash = ProtocolHelper.CreateSignInfoKey();
WriteLog("Search for: " + snkInfoHash.ToHexString());

fDHTClient.Run();
fDHTClient.JoinNetwork();
fDHTClient.SearchNodes(snkInfoHash);
}

private void DHTLogDlg_FormClosed(object sender, FormClosedEventArgs e)
{
fDHTClient.StopSearch();
}

public void WriteLog(string str, bool display = true)
{
Invoke((MethodInvoker)delegate {
rtbLog.AppendText(str + "\r\n");
});

var fswriter = new StreamWriter(new FileStream("./dht.log", FileMode.Append));
fswriter.WriteLine(str);
fswriter.Flush();
fswriter.Close();
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

82 changes: 82 additions & 0 deletions GKCommunicatorApp/Form1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* "GKCommunicator", the chat and bulletin board of the genealogical network.
* Copyright (C) 2018 by Sergey V. Zhdanovskih.
*
* This file is part of "GEDKeeper".
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Net;
using System.Text;
using System.Windows.Forms;
using GKNet.Protocol;
using GKNet.TCP;

namespace GKSimpleChat
{
public partial class Form1 : Form
{
private TCPDuplexClient fClient;

public Form1()
{
InitializeComponent();
}

private void btnConnect_Click(object sender, EventArgs e)
{
fClient = new TCPDuplexClient();
fClient.DataReceive += RaiseDataReceive;
fClient.Start(int.Parse(txtLocalPort.Text));

txtLocalPort.Enabled = false;
btnConnect.Enabled = false;
}

private void btnSend_Click(object sender, EventArgs e)
{
//var endPoint = new IPEndPoint(IPAddress.Parse(txtRemoteAddress.Text), int.Parse(txtRemotePort.Text));
//fClient.Send(endPoint, txtMsg.Text);
SendHandshakeQuery();
}

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if (fClient != null) {
fClient.Disconnect();
}
}

public void RaiseDataReceive(object sender, DataReceiveEventArgs e)
{
Invoke((MethodInvoker)delegate {
txtLog.Text += Encoding.UTF8.GetString(e.Data) + "\r\n";
});
}

#region Protocol features

private void SendHandshakeQuery()
{
var data = ProtocolHelper.CreateHandshakeQuery();
var endPoint = new IPEndPoint(IPAddress.Parse(txtRemoteAddress.Text), int.Parse(txtRemotePort.Text));
var conn = fClient.GetConnection(endPoint);
conn.Send(data);
}

#endregion
}
}
Loading

0 comments on commit c3e6f9c

Please sign in to comment.