using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.ServiceModel;
using TestCommon;
namespace TestServer
{
/// <summary>
/// 데이터 서비스
/// </summary>
public class DataService : IDataService
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 임시 디렉토리 경로
/// </summary>
private const string TEMPORARY_DIRECTORY_PATH = "d:\\temporary_server";
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 데이터 구하기 - GetData(dataID, conditionData)
/// <summary>
/// 데이터 구하기
/// </summary>
/// <param name="dataID">데이터 ID</param>
/// <param name="conditionData">조건 데이터</param>
/// <returns>데이터</returns>
public Stream GetData(string dataID, string conditionData)
{
WriteMessage("START GET DATA");
WriteMessage("DATA ID : {0}", dataID);
WriteMessage("CONDITION DATA");
WriteMessage("--------------------------------------------------");
WriteMessage(conditionData);
WriteMessage("--------------------------------------------------");
string transactionID = GUIDHelper.GetGUID();
if(!Directory.Exists(TEMPORARY_DIRECTORY_PATH))
{
Directory.CreateDirectory(TEMPORARY_DIRECTORY_PATH);
}
//////////////////////////////////////////////////
int dataCount = JSONHelper.Deserialize<int>(conditionData);
List<Student> list = Student.GetData(dataCount);
//////////////////////////////////////////////////
WriteMessage("START CREATE TRANSACTION FILE");
string transactionFilePath = Path.Combine(TEMPORARY_DIRECTORY_PATH, string.Format("{0}.dat", transactionID));
using(FileStream fileStream = new FileStream(transactionFilePath, FileMode.Create, FileAccess.Write))
{
using(GZipStream gZipStream = new GZipStream(fileStream, CompressionMode.Compress))
{
JSONHelper.Serialize(list, gZipStream);
}
}
WriteMessage("END CREATE TRANSACTION FILE");
Stream stream = null;
try
{
stream = File.Open(transactionFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
}
catch
{
}
OperationContext operationContext = OperationContext.Current;
operationContext.OperationCompleted += delegate(object sender, EventArgs e)
{
try
{
File.Delete(transactionFilePath);
}
catch
{
}
};
WriteMessage("END GET DATA");
return stream;
}
#endregion
////////////////////////////////////////////////////////////////////////////////////////// Private
#region 메시지 쓰기 - WriteMessage(format, parameterArray)
/// <summary>
/// 메시지 쓰기
/// </summary>
/// <param name="format">포맷 문자열</param>
/// <param name="parameterArray">매개 변수 배열</param>
private void WriteMessage(string format, params object[] parameterArray)
{
string message;
if(parameterArray.Length == 0)
{
message = format;
}
else
{
message = string.Format(format, parameterArray);
}
Console.WriteLine(string.Format("[{0}] {1}", DateTime.Now.ToString("HH:mm:ss"), message));
}
#endregion
}
}