173 lines
6.7 KiB
C#
173 lines
6.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using SafeMobileLib;
|
|
using System.Net.Sockets;
|
|
using System.Threading;
|
|
using System.IO;
|
|
|
|
namespace AppServer
|
|
{
|
|
class RecordingsServer
|
|
{
|
|
private List<Recording> recList;
|
|
private DBrecordingsManager db;
|
|
|
|
private TCPServer tcpServer4Messages;
|
|
private TCPServer tcpServer4Audio;
|
|
|
|
public RecordingsServer()
|
|
{
|
|
SM.Debug("Recording server started!!!!!!!!");
|
|
db = new DBrecordingsManager(Program.cfg.DB_IP, Program.cfg.DB_schema, Program.cfg.DB_user, Program.cfg.DB_passwd, Program.cfg.DB_port);
|
|
|
|
tcpServer4Messages = new TCPServer();
|
|
tcpServer4Messages.OnMessageRecv += new TCPServer.MessageRecv(tcpServer_OnMessageRecv);
|
|
|
|
try
|
|
{
|
|
tcpServer4Messages.Start(Program.cfg.RecordingsPort);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Utils.WriteLine("AppServerRecordingsServer Exception: " + ex.ToString(), ConsoleColor.Red);
|
|
}
|
|
|
|
tcpServer4Audio = new TCPServer();
|
|
tcpServer4Audio.OnMessageRecv += new TCPServer.MessageRecv(tcpServer4Audio_OnMessageRecv);
|
|
try
|
|
{
|
|
tcpServer4Audio.Start(Program.cfg.RecordingsAudioPort);
|
|
}
|
|
catch(Exception ex)
|
|
{
|
|
Utils.WriteLine("AppServerRecordingsServer Exception: " + ex.ToString(), ConsoleColor.Red);
|
|
}
|
|
|
|
}
|
|
|
|
void tcpServer4Audio_OnMessageRecv(byte[] data, int recv, NetworkStream ns)
|
|
{
|
|
try
|
|
{
|
|
if (data[0] == 1)//file request
|
|
{
|
|
long id = BitConverter.ToInt64(data, 1);
|
|
//ASCIIEncoding encoder = new ASCIIEncoding();
|
|
SM.Debug("A request for recording with file path " + GetFilePath(id) + " was made");
|
|
PlayFile(id, ns);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
SM.Debug("tcpServer4Audio_OnMessageRecv Exeception:"+ex.ToString());
|
|
}
|
|
}
|
|
|
|
private void tcpServer_OnMessageRecv(byte[] data, int recv, NetworkStream ns)
|
|
{
|
|
try
|
|
{
|
|
if (data[0] == 0)//list request
|
|
{
|
|
Int32 imei =data[4]*256*256*256+data[5]*256*256+data[6]*256 + data[7];
|
|
Int64 time_start = data[8]*256*256*256+data[9]*256*256+data[10]*256+data[11];
|
|
Int64 time_stop = data[12]*256*256*256+data[13]*256*256+data[14]*256+data[15];
|
|
Console.WriteLine("Got " + data[1] + " " + data[2] + " " + data[3] + " " + imei + " " + time_start +" " + time_stop);
|
|
SendList2Client(ns, data[1], data[2], data[3],imei,time_start,time_stop,data[16]);
|
|
Console.WriteLine();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
SM.Debug("tcpServer_OnMessageRecv Exeception:" + ex.ToString());
|
|
}
|
|
}
|
|
|
|
private void SendList2Client(NetworkStream ns, byte type, byte gw_id, byte r_gw_id_or_user_id, Int32 imei,Int64 time_start,Int64 time_stop, byte call_type)
|
|
{
|
|
if (type == 1)
|
|
recList = db.GetAllRecordingsForGW(gw_id, r_gw_id_or_user_id,imei,time_start,time_stop,call_type);
|
|
else
|
|
recList = db.GetAllRecordingsForSD(r_gw_id_or_user_id,imei, time_start, time_stop);
|
|
string tempString = "";
|
|
if (recList.Count > 0)
|
|
{
|
|
foreach (Recording obj in recList)
|
|
{
|
|
DateTime dtstart = (Convert.ToUInt32(obj.startTime)).GetDTLocalFromSeconds();
|
|
DateTime dtstop = (Convert.ToUInt32(obj.endTime)).GetDTLocalFromSeconds();
|
|
long recId = obj.id;
|
|
int subId = (int)obj.subs_imei;
|
|
tempString += "#";
|
|
tempString += recId.ToString() + ";";
|
|
tempString += subId.ToString() + ";";
|
|
tempString += gw_id.ToString() + ";";
|
|
tempString += obj.radioGWID.ToString() + ";";
|
|
tempString += dtstart.ToString("yyyy-MM-dd HH:mm:ss") + ";";
|
|
tempString += dtstop.ToString("yyyy-MM-dd HH:mm:ss") + ";";
|
|
tempString += obj.typeSD.ToString()+";";
|
|
tempString += obj.calltype.ToString()+";";
|
|
tempString += obj.group_cpsId.ToString();
|
|
}
|
|
|
|
string bufferString = "$" + (tempString.Length).ToString() + "$" + tempString;
|
|
bufferString = "$" + (bufferString.Length).ToString() + "$" + tempString;
|
|
Console.WriteLine(bufferString);
|
|
ASCIIEncoding encoder = new ASCIIEncoding();
|
|
byte[] buffer = encoder.GetBytes(bufferString);
|
|
tcpServer4Messages.Send(buffer, buffer.Length, ns);
|
|
}
|
|
}
|
|
|
|
private string GetFilePath(long id)
|
|
{
|
|
return db.getFilePath(id);
|
|
}
|
|
|
|
private void PlayFile(long id, NetworkStream ns)
|
|
{
|
|
try
|
|
{
|
|
string filePath = GetFilePath(id);
|
|
SM.Debug("Uploading file " + filePath);
|
|
|
|
byte[] buffer;
|
|
FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
|
|
try
|
|
{
|
|
int length = (int)fileStream.Length; // get file length
|
|
buffer = new byte[128]; // create buffer
|
|
int count; // actual number of bytes read
|
|
//read until Read method returns 0 (end of the stream has been reached)
|
|
count = fileStream.Read(buffer, 0, buffer.Length);
|
|
|
|
while (count > 0 && MainForm.isRunning)
|
|
{
|
|
tcpServer4Audio.Send(buffer, 128, ns);
|
|
buffer = new byte[128];
|
|
count = fileStream.Read(buffer, 0, buffer.Length);
|
|
}
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Utils.WriteLine(ex.ToString(), ConsoleColor.Red);
|
|
}
|
|
finally
|
|
{
|
|
fileStream.Close();
|
|
}
|
|
SM.Debug("Done Uploading file");
|
|
ns.Close();
|
|
//send done
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Utils.WriteLine(ex.ToString(), ConsoleColor.Red);
|
|
}
|
|
}
|
|
}
|
|
}
|