public (MySqlDataReader, MySqlConnection) RunQuery(string query) { MySqlConnection connection = new MySqlConnection(connectionString); MySqlCommand command = new MySqlCommand(query, connection) { CommandTimeout = 5000 }; try { connection.Open(); var reader = command.ExecuteReader(); if (!reader.HasRows && reader.RecordsAffected == 0) { return(null, connection); } return(reader, connection); } catch (Exception e) { StatusManager.PushMessage(e.Message + "QUERY : " + query, StatusType.Error); } return(null, connection); }
/// <summary> /// Stop the main Tutor Server. /// </summary> public static void Stop() { IsRunning = false; server.Stop(); Home_Page.ConnectedClient = 0; StatusManager.PushMessage("Tutor Server Stopped.", StatusType.Error); }
public void FileHandlingService(object FileName) { //Acknowledge Packet to acknowledge successful reception. Acknowledge ack = new Acknowledge() { Status = "OK", Reason = "Video Uploaded Received Successfully" }; Response rsp = new Response() { Type = ResponseType.Acknowledge, Content = ack, Status = "OK" }; string message = JsonConvert.SerializeObject(rsp); byte[] buffer = Encoding.UTF8.GetBytes(message); TcpClient fileSender = _backupClient.AcceptTcpClient(); ReceiveFile((string)FileName, ".jpg", fileSender); StatusManager.PushMessage(FileName + " Received", StatusType.Success); //Getting stream and writing acknowledgement. var stream = fileSender.GetStream(); stream.Write(buffer, 0, buffer.Length); //Receiving Video File ReceiveFile((string)FileName, ".mp4", fileSender); StatusManager.PushMessage(FileName + " Received", StatusType.Success); stream.Write(buffer, 0, buffer.Length); //Move files from temporary storage to permanant if (File.Exists($"{serverroot}/Temp/{FileName}.jpg")) { File.Move($"{serverroot}/Temp/{FileName}.jpg", $"{serverroot}/Thumbnails/{FileName}.jpg"); } if (File.Exists($"{serverroot}/Temp/{FileName}.mp4")) { File.Move($"{serverroot}/Temp/{FileName}.mp4", $"{serverroot}/Videos/{FileName}.mp4"); } _backupClient.Stop(); }
private static void _startServer() { string serb = server.LocalEndpoint.ToString(); IsRunning = true; try { server.Start(); StatusManager.PushMessage("Tutor Server Started.", StatusType.Success); } catch (Exception e) { StatusManager.PushMessage(e.Message, StatusType.Error); } int _crashCounter = 5; while (IsRunning) { try { TcpClient clientHandle = server.AcceptTcpClient(); Client client = new Client(clientHandle); client.Handle(); //Handle Clients. } catch (Exception e) { _crashCounter--; if (_crashCounter == 0) { //Notify that server stopped. Stop(); } } } }
private void _handleClient() { NetworkStream clientStream = _client.GetStream(); int ConnectionTimeout = 1500; //30 Seconds //send acnowledgement Home_Page.ConnectedClient++; StatusManager.PushMessage("Client " + _client.Client.RemoteEndPoint.ToString() + " Connected. ", StatusType.Success); while (_client.Connected && MainServer.IsRunning && ConnectionTimeout > 0) { try { while (!clientStream.DataAvailable && _client.Connected && ConnectionTimeout > 0) { Thread.Sleep(200); ConnectionTimeout--; if (!MainServer.IsRunning) { return; } } byte[] buffer = new byte[65536]; if (clientStream.DataAvailable) { int readBytes = clientStream.Read(buffer, 0, 65535); string message = Encoding.UTF8.GetString(buffer).Trim('\0'); //Process Message Request request = JsonConvert.DeserializeObject <Request>(message); Response result = RequestHandlers.GetResponse(request.Type, (JObject)request.Content); if (result.Type == ResponseType.CreateVideo) { try { _backupClient.Start(); ((CreateVideoResponse)result.Content).Port = _backupClient.LocalEndpoint.ToString().Split(':')[1]; } catch (Exception e) { StatusManager.PushMessage(e.Message, StatusType.Error); } } var response = JsonConvert.SerializeObject(result); //simulating heavy server load //Thread.Sleep(2000); buffer = Encoding.UTF8.GetBytes(response); clientStream.Write(buffer, 0, response.Length); if (result.Type == ResponseType.UpdateProfile) { if (((ProfileUpdateResponse)result.Content).RequiresImageUpdate) { ReceiveImage(request.Username, ".jpg"); } } else if (result.Type == ResponseType.CreateVideo) { Thread thread = new Thread(FileHandlingService) { IsBackground = true, Name = "Backup server", }; thread.Start(((CreateVideoResponse)result.Content).VideoID); } else if (result.Type == ResponseType.VideoUpdate) { VideoUpdateResponse resp = ((VideoUpdateResponse)result.Content); if (resp.IsThumbnailUpdateRequired) { ReceiveImage(resp.ThumbnailImage, ".jpg", false); } } ConnectionTimeout = 1500; //timeout restored. } } catch (Exception e) { StatusManager.PushMessage(e.Message, StatusType.Error); //throw; } } //might send a client disconnected message. Home_Page.ConnectedClient--; StatusManager.PushMessage("Client " + _client.Client.RemoteEndPoint.ToString() + " Disconnected. ", StatusType.Error); clientStream.Close(); }
public bool SetUpDatabase() { //create comments table string successString = ""; (MySqlDataReader reader, var Connection) = RunQuery(@"CREATE TABLE `comments` ( `userid` varchar(128) NOT NULL, `videoid` varchar(128) NOT NULL, `content` varchar(256) NOT NULL, `likes` int(255) NOT NULL DEFAULT '0', `dislikes` int(255) NOT NULL DEFAULT '0', `date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, `uid` varchar(128) NOT NULL, PRIMARY KEY(`uid`) ) ENGINE = InnoDB DEFAULT CHARSET = latin1"); if (ErrorReason.Length < 5) { successString += "Comments"; } ErrorReason = ""; Connection.Close(); (reader, Connection) = RunQuery(@"CREATE TABLE `account_data` ( `username` varchar(50) NOT NULL, `fullname` varchar(64) NOT NULL, `acctype` varchar(32) NOT NULL, `type` int(10) NOT NULL, `profileimage` varchar(256) DEFAULT '/Defaults/default_pp.png', PRIMARY KEY (`username`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1"); if (ErrorReason.Length < 5) { successString += ", Account_Data"; } ErrorReason = ""; Connection.Close(); (reader, Connection) = RunQuery(@"CREATE TABLE `course_details` ( `courseid` varchar(128) NOT NULL, `coursename` varchar(64) NOT NULL, `authorid` varchar(128) NOT NULL, `rating` float NOT NULL DEFAULT '0', `createdon` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, `videocount` int(11) NOT NULL DEFAULT '0', `ratingCount` int(11) NOT NULL DEFAULT '0', PRIMARY KEY (`courseid`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1"); if (ErrorReason.Length < 5) { successString += ", Course_details"; } ErrorReason = ""; Connection.Close(); (reader, Connection) = RunQuery(@"CREATE TABLE `favorites` ( `userid` varchar(128) NOT NULL, `videoid` varchar(128) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1"); if (ErrorReason.Length < 5) { successString += ", Favorites"; } Connection.Close(); ErrorReason = ""; (reader, Connection) = RunQuery(@"CREATE TABLE `tag_details` ( `tag` varchar(50) NOT NULL, `videoid` varchar(128) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1"); if (ErrorReason.Length < 5) { successString += ", Tag_details"; } ErrorReason = ""; Connection.Close(); (reader, Connection) = RunQuery(@"CREATE TABLE `user_login` ( `username` varchar(50) NOT NULL, `password` varchar(128) NOT NULL, `authkey` varchar(128) DEFAULT NULL, `lastloggedon` datetime DEFAULT NULL, PRIMARY KEY (`username`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1"); if (ErrorReason.Length < 5) { successString += ", User_login"; } ErrorReason = ""; Connection.Close(); (reader, Connection) = RunQuery(@"CREATE TABLE `video_details` ( `videoid` varchar(128) NOT NULL, `path` varchar(256) NOT NULL, `thumbnail` varchar(256) NOT NULL DEFAULT '/Defaults/default_thumbnail.png', `title` varchar(256) NOT NULL DEFAULT 'Title Unavailable', `description` varchar(512) DEFAULT 'No description available.', `width` int(255) NOT NULL DEFAULT '1280', `height` int(255) NOT NULL DEFAULT '720', `duration` time NOT NULL DEFAULT '00:00:00', `authorid` varchar(128) NOT NULL, `authorname` varchar(64) NOT NULL, `authorimage` varchar(256) NOT NULL DEFAULT '/Defaults/default_pp.png', `course` varchar(64) NOT NULL, `courseid` varchar(128) NOT NULL, `lastupdatedon` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, `likes` int(255) NOT NULL DEFAULT '0', `dislikes` int(255) NOT NULL DEFAULT '0', PRIMARY KEY (`videoid`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1"); if (ErrorReason.Length < 5) { successString += ", Video_Details "; } ErrorReason = ""; Connection.Close(); if (successString.Length > 5) { StatusManager.PushMessage("TABLE " + successString + "created successfully.", StatusType.Success); } else { StatusManager.PushMessage("Error in table creation", StatusType.Error); } return(true); }