//konstruktor funkcije koji instancira klasu server i pokreće dvije dretve //koje obavljaju posao UDP i TCP servera (na klijentu) public frmDataGrid() { InitializeComponent(); Server server = new Server(this); Thread listeningUDPThread = new Thread(new ThreadStart(server.startUDPServer)); listeningUDPThread.IsBackground = true; listeningUDPThread.Start(); Thread listeningTCPThread = new Thread(new ThreadStart(server.startTCPServer)); listeningTCPThread.IsBackground = true; listeningTCPThread.Start(); //ukoliko ne postoji direktorij za datoteke, kreira ga FileHandling.createFileDirectory(); //uploada datoteke na servis a preko njega u bazu newFolder.uploadFilesToService(); //instanciranje servisa i dohvaćanje podataka s njega u obliku tablice serviceWCF.Service1Client client = new serviceWCF.Service1Client(); DataTable table = client.getData(helper.Name); dgvData.DataSource = table; client.Close(); dgvDownloads.DataSource = newFolder.fillMyFiles(); createDownDgv(); }
//upload naziva datoteka u bazu preko servisa public void uploadFilesToService() { //brišu se sve stare datoteka koje su na servisu te se dohvaćaju nazivi novih clearFilesFromService(); getDirectoryFiles(); getFileNames(); //instancira se servis serviceWCF.Service1Client client = new serviceWCF.Service1Client(); foreach (string filePath in fileList) { //spremanje imena datoteke (bez patha u tmp varijablu) string tmpFileName = Path.GetFileName(filePath); //dodavanje tmp varijable u popis fileName.Add(tmpFileName); //stvaranje informacije o trenutnoj datoteci FileInfo fileInformation = new FileInfo(filePath); //dohavaćanje podatka o veličini trenutne datoteke (u bajtovima) long fileSize = fileInformation.Length / 1024; //upload podataka u bazu string hashFromFile = createHashFromFile(filePath); client.uploadFilesToService(helper.Name, helper.IP, tmpFileName, filePath, fileSize, hashFromFile, helper.portUDP.ToString()); } client.Close(); }
//pretraga datoteka u bazi //instanciranje servisa i pozivanje njegove searchFiles metode private void btnSearchFiles_Click_1(object sender, EventArgs e) { if (txtSearch.Text == "") { return; } try { //spajanje na servis i provjera unešene vrijednosti preko searchFiles metode serviceWCF.Service1Client client = new serviceWCF.Service1Client(); DataTable table = client.searchFiles(txtSearch.Text, helper.Name); if (table != null) { dgvData.DataSource = null; //postavljanje podataka u dataGridView dgvData.DataSource = table; } else { //ukoliko je tablica prazna return; } client.Close(); } catch (Exception exc) { MessageBox.Show(exc.ToString()); } }
//klik na logout u tsMenu nas odjavljuje sa servisa i brise podatke iz baze private void logoutToolStripMenuItem_Click(object sender, EventArgs e) { if (helper.Name == "") { MessageBox.Show("Niste prijavljeni!"); return; } try { serviceWCF.Service1Client client = new serviceWCF.Service1Client(); if (client.clearUserFiles(helper.Name) == 1) { //MessageBox.Show("Logged out!"); helper.Name = ""; tslblSuccessfulLogout.Visible = true; tslblNoLogout.Visible = false; } client.Close(); } catch { MessageBox.Show("Neuspješna odjava."); } }
// refresh liste datoteka, instanciranje servisa i ponovno dohvaćanje svih datoteka private void btnGetData_Click(object sender, EventArgs e) { dgvData.DataSource = null; newFolder.uploadFilesToService(); serviceWCF.Service1Client client = new serviceWCF.Service1Client(); DataTable table = client.getData(helper.Name); dgvData.DataSource = table; client.Close(); }
//provjera korisničkog imena prilikom prijave u aplikaciju //instanciranje servisa i pozivanje metode za provjeru public int checkUsername() { serviceWCF.Service1Client client = new serviceWCF.Service1Client(); if (client.checkUsername(helper.Name) == 1) { client.Close(); return(1); } else { client.Close(); return(0); } }
//klik na login -> provjera unesenog imena i provjera konekcije s bazom private void button1_Click(object sender, EventArgs e) { bool usernameOK = false; //ako nije uneseno prazno polje if (txtUsername.Text != "") { helper.Name = txtUsername.Text; usernameOK = true; } else { usernameOK = false; MessageBox.Show("Please enter your username."); } string strHostName = ""; try { serviceWCF.Service1Client client = new serviceWCF.Service1Client(); //instanciranje WCf servisa //dobivanje vanjske IP adrese računala strHostName = System.Net.Dns.GetHostName(); IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName); IPAddress[] addr = ipEntry.AddressList; helper.IP = addr[addr.Length - 2].ToString(); Match result = Regex.Match(helper.IP, @"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"); if (!result.Success) { helper.IP = "127.0.0.1"; } //ako je korisničko ime pravilno if (usernameOK) { try { //provjera konekcije s servisom if (client.checkConnection() == 1) { FileHandling checkUser = new FileHandling(); //ako ime nije slobodno if (checkUser.checkUsername() == 1) { MessageBox.Show("Ime se koristi."); helper.Name = ""; } else { //zatvaranje konekcije s servisom i login forme, //te instanciranje i prikaz forme sa DataGrid-om client.Close(); this.Close(); frmDataGrid fdg = new frmDataGrid(); fdg.Show(); } } } // ukoliko servis nije dostupan catch { MessageBox.Show("There has been a probem with connecting to server. Please try again."); helper.Name = ""; } } } //ukoliko servis nije dostupan catch { MessageBox.Show("There has been a probem with connecting to server. Please try again."); helper.Name = ""; } }
//brisanje datoteka iz baze nakon što čvor napusti mrežu public void clearFilesFromService() { serviceWCF.Service1Client client = new serviceWCF.Service1Client(); client.clearUserFiles(helper.Name); client.Close(); }