Skip to content

Commit a6340dd

Browse files
author
blomman84
committed
* Added a new example. Mostly because i don't want to rewrite code every time i want to test something :)
1 parent 00e3e61 commit a6340dd

7 files changed

+231
-5
lines changed

FlowLibDemo/ConsoleDemo/ConsoleDemo.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<ItemGroup>
4444
<Compile Include="Examples\CollectTransferedInformationForHub.cs" />
4545
<Compile Include="Examples\CollectTransferedInformationFromFilelistDownload.cs" />
46+
<Compile Include="Examples\MultiConnections.cs" />
4647
<Compile Include="Examples\WhatConnectionIsPossible.cs" />
4748
<Compile Include="ConsoleClient\Controls\Button.cs" />
4849
<Compile Include="ConsoleClient\Controls\Control.cs" />

FlowLibDemo/ConsoleDemo/Examples/ActiveDownloadFilelistFromUser.cs

-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ void Protocol_RequestTransfer(object sender, FmdcEventArgs e)
182182
e.Handled = true;
183183
e.Data = req;
184184
transferManager.RemoveTransferReq(req.Key);
185-
transferManager.AddTransfer(trans);
186185
}
187186
}
188187

FlowLibDemo/ConsoleDemo/Examples/ActiveEmptySharing.cs

-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ void Protocol_RequestTransfer(object sender, FmdcEventArgs e)
149149
e.Handled = true;
150150
e.Data = req;
151151
transferManager.RemoveTransferReq(req.Key);
152-
transferManager.AddTransfer(trans);
153152
}
154153
}
155154

FlowLibDemo/ConsoleDemo/Examples/ActiveEmptySharingUsingTLS.cs

-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,6 @@ void Protocol_RequestTransfer(object sender, FmdcEventArgs e)
171171
e.Handled = true;
172172
e.Data = req;
173173
transferManager.RemoveTransferReq(req.Key);
174-
transferManager.AddTransfer(trans);
175174
}
176175
}
177176

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+

2+
/*
3+
*
4+
* Copyright (C) 2009 Mattias Blomqvist, patr-blo at dsv dot su dot se
5+
*
6+
* This program is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 2 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*
20+
*/
21+
22+
using FlowLib.Connections;
23+
using FlowLib.Containers;
24+
using FlowLib.Protocols;
25+
using FlowLib.Events;
26+
using FlowLib.Interfaces;
27+
using FlowLib.Managers;
28+
using FlowLib.Utils.FileLists;
29+
using System.Collections.Generic;
30+
using System.Threading;
31+
32+
namespace ConsoleDemo.Examples
33+
{
34+
public class MultiConnections : IBaseUpdater
35+
{
36+
public event FmdcEventHandler UpdateBase;
37+
38+
TransferManager transferManager = new TransferManager();
39+
DownloadManager downloadManager = new DownloadManager();
40+
TcpConnectionListener incomingConnectionListener = null;
41+
System.Timers.Timer timer = new System.Timers.Timer();
42+
long last = 0;
43+
44+
//string currentDir = @"C:\Temp\";
45+
string currentDir = System.AppDomain.CurrentDomain.BaseDirectory;
46+
bool sentRequest = false;
47+
48+
Hub hub = null;
49+
50+
public MultiConnections()
51+
{
52+
UpdateBase = new FmdcEventHandler(PassiveConnectToUser_UpdateBase);
53+
54+
// Creates a empty share
55+
Share share = new Share("Testing");
56+
// Port to listen for incomming connections on
57+
share.Port = 12345;
58+
59+
incomingConnectionListener = new TcpConnectionListener(share.Port);
60+
incomingConnectionListener.Update += new FmdcEventHandler(Connection_Update);
61+
incomingConnectionListener.Start();
62+
63+
// Adds common filelist to share
64+
AddFilelistsToShare(share);
65+
66+
HubSetting setting = new HubSetting();
67+
setting.Address = "127.0.0.1";
68+
setting.Port = 411;
69+
setting.DisplayName = "FlowLib";
70+
setting.Protocol = "Auto";
71+
72+
hub = new Hub(setting, this);
73+
hub.ProtocolChange += new FmdcEventHandler(hubConnection_ProtocolChange);
74+
// Adds share to hub
75+
hub.Share = share;
76+
hub.Me.Mode = FlowLib.Enums.ConnectionTypes.Direct;
77+
hub.Connect();
78+
79+
80+
last = System.DateTime.Now.Ticks;
81+
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
82+
timer.AutoReset = true;
83+
timer.Interval = 1000;
84+
timer.Start();
85+
}
86+
87+
void Connection_Update(object sender, FlowLib.Events.FmdcEventArgs e)
88+
{
89+
switch (e.Action)
90+
{
91+
case Actions.TransferStarted:
92+
Transfer trans = e.Data as Transfer;
93+
if (trans != null)
94+
{
95+
if (trans.Protocol == null)
96+
{
97+
trans.Protocol = new FlowLib.Protocols.AdcProtocol(trans);
98+
trans.Listen();
99+
transferManager.AddTransfer(trans);
100+
}
101+
102+
trans.Protocol.ChangeDownloadItem += new FmdcEventHandler(Protocol_ChangeDownloadItem);
103+
trans.Protocol.RequestTransfer += new FmdcEventHandler(Protocol_RequestTransfer);
104+
trans.ProtocolChange += new FmdcEventHandler(trans_ProtocolChange);
105+
e.Handled = true;
106+
}
107+
break;
108+
}
109+
}
110+
111+
void trans_ProtocolChange(object sender, FmdcEventArgs e)
112+
{
113+
Transfer trans = sender as Transfer;
114+
if (trans == null)
115+
return;
116+
IProtocolTransfer prot = e as IProtocolTransfer;
117+
if (prot != null)
118+
{
119+
prot.ChangeDownloadItem -= Protocol_ChangeDownloadItem;
120+
prot.RequestTransfer -= Protocol_RequestTransfer;
121+
}
122+
trans.Protocol.ChangeDownloadItem += new FmdcEventHandler(Protocol_ChangeDownloadItem);
123+
trans.Protocol.RequestTransfer += new FmdcEventHandler(Protocol_RequestTransfer);
124+
}
125+
126+
void PassiveConnectToUser_UpdateBase(object sender, FmdcEventArgs e) { }
127+
128+
void hubConnection_ProtocolChange(object sender, FmdcEventArgs e)
129+
{
130+
Hub hubConnection = sender as Hub;
131+
IProtocol prot = e.Data as IProtocol;
132+
if (prot != null)
133+
{
134+
prot.Update -= hubConnection_Update;
135+
}
136+
hubConnection.Protocol.Update += new FmdcEventHandler(hubConnection_Update);
137+
}
138+
139+
void AddFilelistsToShare(Share s)
140+
{
141+
// This will add common filelists to share and save them in directory specified.
142+
General.AddCommonFilelistsToShare(s, currentDir + "MyFileLists\\");
143+
}
144+
145+
void hubConnection_Update(object sender, FmdcEventArgs e)
146+
{
147+
Hub hub = (Hub)sender;
148+
switch (e.Action)
149+
{
150+
case Actions.TransferRequest:
151+
if (e.Data is TransferRequest)
152+
{
153+
TransferRequest req = (TransferRequest)e.Data;
154+
if (transferManager.GetTransferReq(req.Key) == null)
155+
transferManager.AddTransferReq(req);
156+
}
157+
break;
158+
case Actions.TransferStarted:
159+
Transfer trans = e.Data as Transfer;
160+
if (trans != null)
161+
{
162+
transferManager.StartTransfer(trans);
163+
trans.Protocol.ChangeDownloadItem += new FmdcEventHandler(Protocol_ChangeDownloadItem);
164+
trans.Protocol.RequestTransfer += new FmdcEventHandler(Protocol_RequestTransfer);
165+
}
166+
break;
167+
case Actions.UserOnline:
168+
last = System.DateTime.Now.Ticks;
169+
break;
170+
}
171+
}
172+
173+
void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
174+
{
175+
if (hub != null && !hub.IsDisposed && hub.RegMode >= 0 && !sentRequest)
176+
{
177+
if (last > System.DateTime.Now.Subtract(new System.TimeSpan(0, 0, 30)).Ticks)
178+
{
179+
SortedList<string, User> usrs = new SortedList<string, User>(hub.Userlist);
180+
foreach (var item in usrs)
181+
{
182+
if (item.Value.ID == hub.Me.ID || item.Value.ID == "-YnHub-")
183+
{
184+
continue;
185+
}
186+
User usr = item.Value;
187+
new Thread(
188+
delegate()
189+
{
190+
// Adding filelist of unknown type to download manager.
191+
ContentInfo info = new ContentInfo(ContentInfo.FILELIST, BaseFilelist.UNKNOWN);
192+
info.Set(ContentInfo.ID, usr.StoreID);
193+
info.Set(ContentInfo.STORAGEPATH, currentDir + "Filelists\\" + usr.StoreID + ".filelist");
194+
downloadManager.AddDownload(new DownloadItem(info), new Source(hub.RemoteAddress.ToString(), usr.StoreID));
195+
// Start transfer to user
196+
UpdateBase(this, new FmdcEventArgs(Actions.StartTransfer, usr));
197+
}).Start();
198+
}
199+
sentRequest = true;
200+
}
201+
}
202+
}
203+
204+
void Protocol_RequestTransfer(object sender, FmdcEventArgs e)
205+
{
206+
ITransfer trans = sender as ITransfer;
207+
TransferRequest req = e.Data as TransferRequest;
208+
req = transferManager.GetTransferReq(req.Key);
209+
if (trans != null && req != null)
210+
{
211+
e.Handled = true;
212+
e.Data = req;
213+
transferManager.RemoveTransferReq(req.Key);
214+
}
215+
}
216+
217+
void Protocol_ChangeDownloadItem(object sender, FmdcEventArgs e)
218+
{
219+
Transfer trans = sender as Transfer;
220+
if (trans == null)
221+
return;
222+
DownloadItem dwnItem = null;
223+
if (downloadManager.TryGetDownload(trans.Source, out dwnItem))
224+
{
225+
e.Data = dwnItem;
226+
e.Handled = true;
227+
}
228+
}
229+
}
230+
}

FlowLibDemo/ConsoleDemo/Examples/PassiveDownloadFilelistFromUser.cs

-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ void Protocol_RequestTransfer(object sender, FmdcEventArgs e)
133133
e.Handled = true;
134134
e.Data = req;
135135
transferManager.RemoveTransferReq(req.Key);
136-
transferManager.AddTransfer(trans);
137136
}
138137
}
139138

FlowLibDemo/ConsoleDemo/Examples/PassiveDownloadFilelistFromUserUsingTLS.cs

-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ void Protocol_RequestTransfer(object sender, FmdcEventArgs e)
145145
e.Handled = true;
146146
e.Data = req;
147147
transferManager.RemoveTransferReq(req.Key);
148-
transferManager.AddTransfer(trans);
149148
}
150149
}
151150

0 commit comments

Comments
 (0)