1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
|
using System.Text;
using System.IO;
using System.ServiceModel;
using System.Runtime;
using System.Runtime.Serialization;
using System.Runtime.Remoting;
namespace test_1
{
[ServiceContract]
public interface IClientContract
{
[OperationContract(IsOneWay = true)]
void GetImagesFromServer(List<CustomImage> img);
[OperationContract(IsOneWay = true)]
void GetUpdateFromFileShare(CustomImage img);
}
[ServiceContract(CallbackContract = typeof(IClientContract))]
public interface IServerContract
{
[OperationContract(IsOneWay = false)]
void StartWatcher(string path);
[OperationContract(IsOneWay = false)]
void GetImagesFromPathAtFirstLoad(string path);
}
[DataContract]
public class CustomImage
{
public enum ChangeType
{
Add,
Delete
}
[DataMember]
public string Name { get; set; }
[DataMember]
public string FullPath { get; set; }
[DataMember]
public Byte[] Img { get; set; }
[DataMember]
public ChangeType Modification { get; set; }
}
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.Single)]
public class ServiceImplementation : IServerContract
{
#region Member Fields
private System.IO.FileSystemWatcher fsw = null;
private OperationContext context = null;
private IClientContract client = null;
#endregion
#region IServerContract Members
/// <summary>
/// Start the FileSystemWatcher on the server.
/// </summary>
/// <param name="path">The path to the directory to watch.</param>
public void StartWatcher(string path)
{
try
{
context = OperationContext.Current;
client = context.GetCallbackChannel<IClientContract>();
fsw = new System.IO.FileSystemWatcher();
fsw.Path = path;
fsw.IncludeSubdirectories = true;
fsw.Filter = "*.*";
fsw.Created += new System.IO.FileSystemEventHandler(GetUpdateFileFromFSW);
fsw.Deleted += new System.IO.FileSystemEventHandler(GetUpdateFileFromFSW);
fsw.EnableRaisingEvents = true;
}
catch (Exception ex)
{
throw new FaultException<ServerException>(new ServerException(
string.Format("An error occured !{0}{1}", Environment.NewLine, ex.Message)));
}
}
/// <summary>
/// Retrieve the images which are in the specified folder during the startup.
/// </summary>
/// <param name="path">The folder to get the images.</param>
public void GetImagesFromPathAtFirstLoad(string path)
{
var imgList = new List<CustomImage>();
CustomImage image = null;
foreach (var file in Directory.GetFiles(path))
{
try
{
if (Path.GetExtension(file).ToLower() == ".png" || Path.GetExtension(file).ToLower() == ".jpg" ||
Path.GetExtension(file).ToLower() == ".jpeg" || Path.GetExtension(file).ToLower() == ".bmp" ||
Path.GetExtension(file).ToLower() == ".gif")
{
using (var stream = File.OpenRead(file))
{
if (stream.Length > 0)
{
image = new CustomImage();
image.Name = Path.GetFileName(file);
image.FullPath = Path.GetFullPath(file);
image.Modification = CustomImage.ChangeType.Add;
}
}
byte[] array = GetByteFromImage(file);
image.Img = array;
imgList.Add(image);
}
}
catch (Exception ex)
{
throw new FaultException<ServerException>(new ServerException(
string.Format("An error occured !{0}{1}", Environment.NewLine, ex.Message)));
}
}
if (client != null)
{
client.GetImagesFromServer(imgList);
}
}
#endregion
/// <summary>
/// Convert a file on a byte array.
/// </summary>
/// <param name="file">The file to convert.</param>
/// <returns>An array of byte corresponding to the file.</returns>
private static byte[] GetByteFromImage(string file)
{
byte[] array;
try
{
using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))
{
BinaryReader reader = new BinaryReader(fs);
array = reader.ReadBytes((int)fs.Length);
reader.Close();
}
}
catch (Exception ex)
{
throw new FaultException<ServerException>(new ServerException(
string.Format("An error occured !{0}{1}", Environment.NewLine, ex.Message)));
}
return array;
}
/// <summary>
/// Method called when the FileSystemWatcher detects that a file has been added/deleted.
/// </summary>
private void GetUpdateFileFromFSW(object sender, System.IO.FileSystemEventArgs e)
{
CustomImage image = null;
if (e.ChangeType == System.IO.WatcherChangeTypes.Created)
{
if (File.Exists(e.FullPath) && (Path.GetExtension(e.FullPath).ToLower() == ".png" ||
Path.GetExtension(e.FullPath).ToLower() == ".jpg"
|| Path.GetExtension(e.FullPath).ToLower() == ".jpeg"
|| Path.GetExtension(e.FullPath).ToLower() == ".bmp"
|| Path.GetExtension(e.FullPath).ToLower() == ".gif"))
{
try
{
using (var stream = File.OpenRead(e.FullPath))
{
if (stream.Length > 0)
{
image = new CustomImage();
image.Name = e.Name;
image.FullPath = e.FullPath;
image.Modification = CustomImage.ChangeType.Add;
}
}
byte[] array = GetByteFromImage(e.FullPath);
image.Img = array;
}
catch (Exception ex)
{
throw new FaultException<ServerException>(new ServerException(
string.Format("An error occured !{0}{1}", Environment.NewLine, ex.Message)));
}
}
}
else if (e.ChangeType == System.IO.WatcherChangeTypes.Deleted)
{
image = new CustomImage();
image.Name = e.Name;
image.FullPath = e.FullPath;
image.Modification = CustomImage.ChangeType.Delete;
}
if (image != null)
{
SendUpdateToClient(image);
}
}
/// <summary>
/// Inforl the client that an update is available.
/// </summary>
/// <param name="image">The image that have been added/deleted.</param>
private void SendUpdateToClient(CustomImage image)
{
if (client != null)
{
client.GetUpdateFromFileShare(image);
}
}
}
[CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, UseSynchronizationContext = false)]
public class WCFClient : IServerContractCallback
{
#region Member Fields
private ServerContractClient sc = null;
public delegate void GetImagesFromServerHandler(CustomImage[] imgList);
public event GetImagesFromServerHandler GetImagesFromServerEvent;
public delegate void GetUpdateFromFileShareHandler(CustomImage img);
public event GetUpdateFromFileShareHandler GetUpdateFromFileShareEvent;
#endregion
/// <summary>
/// Public constructor.
/// </summary>
public WCFClient()
{
sc = new ServerContractClient(new System.ServiceModel.InstanceContext(this));
sc.Open();
}
/// <summary>
/// Ask the service to launch the FileSystemWatcher on the server.
/// </summary>
/// <param name="path">The folder to watch on the server.</param>
public void StartWatcher(string path)
{
sc.StartWatcher(path);
}
/// <summary>
/// Ask the service to get the images which are in the dedicated folder.
/// </summary>
/// <param name="path">The folder that you want the images.</param>
public void GetImagesFromPathAtFirstLoad(string path)
{
try
{
sc.GetImagesFromPathAtFirstLoad(path);
}
catch (ServerException ex)
{
throw new FaultException<ServerException>(new ServerException(
string.Format("An error occured !{0}{1}", Environment.NewLine, ex.Message)));
}
}
/// <summary>
/// Disconnect the client from the service.
/// </summary>
public void Disconnect()
{
sc.Close();
}
#region IServerContractCallback Members
/// <summary>
/// Method called when the service pushed the images to the client.
/// </summary>
/// <param name="imgList">The list of images to load/get from the server.</param>
public void GetImagesFromServer(CustomImage[] imgList)
{
if (GetImagesFromServerEvent != null)
{
GetImagesFromServerEvent(imgList);
}
}
/// <summary>
/// Method called when the service detext an update in the folder where the image are stored.
/// </summary>
/// <param name="img">The image that has been added/removed from the folder</param>
public void GetUpdateFromFileShare(CustomImage img)
{
if (GetUpdateFromFileShareEvent != null)
{
GetUpdateFromFileShareEvent(img);
}
}
#endregion
}
} |
Partager