Skip to content

Commit

Permalink
[v.0.03a] Test Code and And Minor Update
Browse files Browse the repository at this point in the history
SUMMARY:
* Added Test Code for Server and Network Client
* Implemented NetworkClient ( early to call it final, but it's working
and usable right now )
* Implemented basic shutdown and start application algorithms

---------------------------------
*OLD!* AutoClient:
---------------------------------
* Renamed AutoClient into NetworkClient
* Added basic functionality and a test temporary code
---------------------------------
App:
---------------------------------
* Changes:
Added code for Shutdown() function. Overrided basic Shutdown() function.
Changed ShutdownMode to Manual
Added code for testing Serialization and Desirialization of UserInfo
class objects.
Added code for testing Server and Client class objects
---------------------------------
ManualWindow:
---------------------------------
Added code for testing (ping)
  • Loading branch information
FakeEmperor committed Mar 16, 2015
1 parent e3818de commit 5ae152f
Show file tree
Hide file tree
Showing 11 changed files with 362 additions and 71 deletions.
6 changes: 6 additions & 0 deletions AutoShare/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
<setting name="UserlistExtension" serializeAs="String">
<value>userlist</value>
</setting>
<setting name="ServerPort" serializeAs="String">
<value>12344</value>
</setting>
</AutoShare.Properties.Settings>
<AutoShare.Settings1>
<setting name="FolderPath" serializeAs="String">
Expand All @@ -38,6 +41,9 @@
<setting name="UseDocFolder" serializeAs="String">
<value>True</value>
</setting>
<setting name="TemporaryUserlistName" serializeAs="String">
<value>users.temp</value>
</setting>
</AutoShare.Properties.Settings>
</userSettings>
</configuration>
1 change: 1 addition & 0 deletions AutoShare/App.xaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Application x:Class="AutoShare.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
ShutdownMode="OnExplicitShutdown"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
Expand Down
73 changes: 68 additions & 5 deletions AutoShare/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
using System.IO;
using System.Windows.Shapes;
using System.Security.Permissions;

using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using AutoShare.Engine.Network.Sharing;

namespace AutoShare
{
Expand All @@ -19,14 +21,25 @@ namespace AutoShare
public partial class App : Application
{
public AutoShare.Engine.IO.FilesFolderChecker FolderWatchdog;
public AutoShare.Engine.IO.SerializableList<AutoShare.Engine.Network.Sharing.UserInfo> KnownUsers;

public AutoShare.Engine.IO.SerializableList<UserInfo> KnownUsers;
public AutoShare.Engine.Network.NetworkClient Client;
public AutoShare.Engine.Network.NetworkServer Server;
//TEMP
public bool AskUserExit = true;
App()
{
#region Initialization Block

KnownUsers = new Engine.IO.SerializableList<Engine.Network.Sharing.UserInfo>();
/** UNCOMMENT TO CREATE TEST USERINFO
*
*
UserInfo self = new UserInfo("TestBuddy", "Apple", new System.Net.IPEndPoint(new System.Net.IPAddress(new byte[4]{127,0,0,1}),4321), true);
KnownUsers.List.Add(self);
*/
Client = new Engine.Network.NetworkClient();
Server = new Engine.Network.NetworkServer(4321);

string path;
if (AutoShare.Properties.Settings.Default.UseDocFolder)
path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + System.IO.Path.DirectorySeparatorChar + AutoShare.Properties.Settings.Default.FolderPath;
Expand All @@ -35,21 +48,71 @@ public partial class App : Application
FolderWatchdog = new Engine.IO.FilesFolderChecker(path, true);


KnownUsers = new Engine.IO.SerializableList<Engine.Network.Sharing.UserInfo>();


#endregion
#region Subscription to events


#endregion
#region Network Startup


#endregion
this.Initialize();


//TEMP just for testing purposes



}
public void Initialize()
{
//TEMP as the Initialize() function is described in the paper
BinaryFormatter formatter = new BinaryFormatter();
string Path = System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + System.IO.Path.DirectorySeparatorChar +
AutoShare.Properties.Settings.Default.UserlistPrefix + System.Environment.UserName + '.' + AutoShare.Properties.Settings.Default.UserlistExtension;
using (FileStream tfs = new FileStream(Path, FileMode.Open, FileAccess.Read))
KnownUsers = (AutoShare.Engine.IO.SerializableList<UserInfo>)formatter.Deserialize(tfs);

}
public void Shutdown()
public new void Shutdown()
{
//save all settings
AutoShare.Properties.Settings.Default.Save();
//save folder state

//save user state
//TEMP (because user can change its name, need to make login system)
string CurrentUserName = System.Environment.UserName;
//filepath
string Path = AutoShare.Properties.Settings.Default.UserlistPrefix + CurrentUserName + '.' + AutoShare.Properties.Settings.Default.UserlistExtension;
string TempPath = AutoShare.Properties.Settings.Default.TemporaryUserlistName + '.' + AutoShare.Properties.Settings.Default.UserlistExtension;
//get save path
BinaryFormatter formatter = new BinaryFormatter();
//TEMP as can be failed/access denied/program halted during I/O operation etc. Need to write first in a temp file, then rewrite, then delete temp file.
//1. Save to temp folder
using( FileStream tfs = new FileStream(TempPath, FileMode.Create, FileAccess.Write) )
formatter.Serialize(tfs, KnownUsers,
new System.Runtime.Remoting.Messaging.Header[1]{ new System.Runtime.Remoting.Messaging.Header("USERLISTLENGTH", KnownUsers.List.Count ) } );
//2. Overwrite
File.Copy( TempPath, System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + System.IO.Path.DirectorySeparatorChar + Path, true );


//Dispose all the resources
this.Server.Dispose();
//Finally, shutdown itself
base.Shutdown();

}

//END CLASS <<APP>>
}





//END APP.XAML.CS
}
2 changes: 1 addition & 1 deletion AutoShare/AutoShare.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
<Compile Include="Engine\IO\FileSystem.cs" />
<Compile Include="Engine\IO\SerializableList.cs" />
<Compile Include="Engine\Network\NetworkServer.cs" />
<Compile Include="Engine\Network\AutoClient.cs" />
<Compile Include="Engine\Network\NetworkClient.cs" />
<Compile Include="Engine\Network\NetPacket.cs" />
<Compile Include="Engine\Network\Sharing\SharedFile.cs" />
<Compile Include="Engine\Network\Sharing\UserInfo.cs" />
Expand Down
46 changes: 0 additions & 46 deletions AutoShare/Engine/Network/AutoClient.cs

This file was deleted.

173 changes: 173 additions & 0 deletions AutoShare/Engine/Network/NetworkClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Threading;
using AutoShare.Engine.Network.Sharing;

namespace AutoShare.Engine.Network
{
//TODO: MOVE LIST<USERStatus> outside of class. To sync values from the server and from the client

public class NetworkClient
{
#region Helper Classes
public struct UserStatus
{
public UserInfo Info;
public DateTime LastPinged;
public bool IsOnline;


public UserStatus(UserInfo Info, DateTime LastPinged, bool IsOnline)
{
this.Info = Info;
this.LastPinged = LastPinged;
this.IsOnline = IsOnline;
}

}

#endregion
#region Private Members
List<Task> task;
ManualResetEvent mres;
List<Thread> socket_threads;
List<UserStatus> nodes; //ping nodes

#endregion
#region Thread Functions

void SocketThread(object index)
{
TcpClient cli = null;
UserStatus us;
try {

lock (nodes)
us = nodes[(int)index];
cli = new TcpClient();
//BEFORE FIRST
cli.NoDelay = false;
cli.ReceiveTimeout = 10;
cli.SendTimeout = 5;
//FIRST - connect to the last known
cli.Connect(us.Info.LastKnownAddress);
//SECOND - cycle through the known ip addresses
if (!cli.Connected)
{
for (int i = 0, s = us.Info.AddressHistory.Count; i < s; ++i)
{
cli.Connect(us.Info.AddressHistory[i].EndPoint);
if (cli.Connected)
break;
}
}
//THIRD - EITHER DIE WITH NO STATE OR CONNECT AND SYNC LIST
us.LastPinged = DateTime.Now;
//TEMP just a workaround
us.IsOnline = cli.Connected;
if (us.IsOnline)
{
//TEMP: Send something
NetworkStream ns = cli.GetStream();
byte[] bytes = System.Text.UTF8Encoding.UTF8.GetBytes("PING");
ns.Write( bytes, 0, bytes.Length );
ns.Close();
}
}
catch (ThreadAbortException) {

}
finally {
//set thread running to false
if(cli!=null)
cli.Close();

}




//END

}

#endregion
#region Constructor and Destructors
public NetworkClient(IEnumerable<UserStatus> UserStatuses = null, bool StartImmediately = true)
{
if (UserStatuses != null && UserStatuses.Count() > 0)
nodes = new List<UserStatus>(UserStatuses);
else
nodes = new List<UserStatus>();
socket_threads = new List<Thread>();
mres = new ManualResetEvent(false);
if (nodes.Count > 0)
{
for (int i = 0, s = nodes.Count; i < s; ++i )
AddThread(true);
}
}

~NetworkClient()
{
mres.Set();
//Delay for dispose
mres.WaitOne();
//Dispose object
mres.Dispose();
}

#endregion
#region Protected Methods
protected void AddThread(bool StartImmediately)
{
Thread thr = new Thread(SocketThread);
lock (this.socket_threads)
{
this.socket_threads.Add(thr);
if (StartImmediately)
thr.Start(this.socket_threads.Count - 1);
}

}
#endregion

#region API Calls and Public Accessors
public void AddNode(UserStatus UserStatus, bool StartImmediately = true)
{
lock (this.nodes)
{
this.nodes.Add(UserStatus);
this.AddThread(StartImmediately);
}
}
public void RemoveNode(int index)
{
this.nodes.RemoveAt(index);
this.socket_threads[index].Abort();
this.socket_threads.RemoveAt(index);
}
public UserStatus this[int i] { get { return nodes[i]; } set { nodes[i] = value; } }

public ThreadState NodeThreadState(int i)
{
return this.socket_threads[i].ThreadState;
}

public void StartNodeThread(int i)
{
this.socket_threads[i].Start();
}

public void StopNodeThread(int i)
{
//TEMP and workaround. Recode this thing
this.socket_threads[i].Abort();
}
#endregion
}
}
Loading

0 comments on commit 5ae152f

Please sign in to comment.