Skip to content

Commit

Permalink
Commit initial: Adaugare proiect in git.
Browse files Browse the repository at this point in the history
  • Loading branch information
sebitza1020 committed Jun 6, 2020
0 parents commit 0302867
Show file tree
Hide file tree
Showing 13 changed files with 204 additions and 0 deletions.
43 changes: 43 additions & 0 deletions Server.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library" scope="TEST">
<library name="JUnit5.4">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.4.2/junit-jupiter-5.4.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.4.2/junit-jupiter-api-5.4.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.0.0/apiguardian-api-1.0.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.1.1/opentest4j-1.1.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.4.2/junit-platform-commons-1.4.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.4.2/junit-jupiter-params-5.4.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.4.2/junit-jupiter-engine-5.4.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.4.2/junit-platform-engine-1.4.2.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.4.2/junit-jupiter-5.4.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.4.2/junit-jupiter-api-5.4.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.0.0/apiguardian-api-1.0.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.1.1/opentest4j-1.1.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.4.2/junit-platform-commons-1.4.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.4.2/junit-jupiter-params-5.4.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.4.2/junit-jupiter-engine-5.4.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.4.2/junit-platform-engine-1.4.2.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
Binary file added out/production/Server/ClientHandler.class
Binary file not shown.
Binary file added out/production/Server/DBConn.class
Binary file not shown.
Binary file added out/production/Server/Main.class
Binary file not shown.
Binary file added out/production/Server/PhoneNumberData.class
Binary file not shown.
Binary file added out/production/Server/PhoneNumberDataTest.class
Binary file not shown.
Binary file added out/production/Server/Server.class
Binary file not shown.
60 changes: 60 additions & 0 deletions src/ClientHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOError;
import java.io.IOException;
import java.net.Socket;
import java.sql.*;

public class ClientHandler extends Thread{
final Socket socket;
final DataInputStream input;
final DataOutputStream output;
DBConn dbc = DBConn.getDBConn();
Connection con = dbc.getConnection();

public ClientHandler(Socket socket, DataInputStream input, DataOutputStream output) {
this.socket = socket;
this.input = input;
this.output = output;
}

@Override
public void run() {
while(true) {
try {
output.writeUTF("");
String received = input.readUTF();
if (received.equals("Done") || this.con.isClosed()) {
this.socket.close();
System.out.println("Connection terminated");
break;
}
System.out.println(received);
PhoneNumberData pnd = new PhoneNumberData(received);
String sql = "SELECT * FROM contacts WHERE phoneNumber='" + pnd.getPhoneNumber() + "'";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
if (rs.getString(5).equals(pnd.getPhoneNumber())) {
output.writeUTF("OCUPAT");
break;
}
}
if(!rs.isFirst()) {
output.writeUTF("DISPONIBIL");
}
}
catch (IOException | SQLException i) {
System.out.println(i);
break;
}
}
try{
this.input.close();
this.output.close();
}
catch (IOException i){
System.out.println(i);
}
}
}
26 changes: 26 additions & 0 deletions src/DBConn.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import java.sql.Connection;
import java.sql.DriverManager;

public class DBConn {
private Connection con;
private static DBConn dbc;
private DBConn() {
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/contactdb","root", "");
} catch (Exception e) {
System.out.println(e);
}
}

public static DBConn getDBConn() {
if(dbc == null) {
dbc = new DBConn();
}
return dbc;
}

public Connection getConnection() {
return con;
}
}
7 changes: 7 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import java.io.IOException;

public class Main {
public static void main(String[] args) throws IOException {
Server server = new Server(5000);
}
}
15 changes: 15 additions & 0 deletions src/PhoneNumberData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public class PhoneNumberData {
String phoneNumber;

public PhoneNumberData(String phoneNumber) {
this.phoneNumber = phoneNumber;
}

public String getPhoneNumber() {
return phoneNumber;
}

public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
22 changes: 22 additions & 0 deletions src/PhoneNumberDataTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import org.junit.jupiter.api.Assertions;

import static org.junit.jupiter.api.Assertions.*;

class PhoneNumberDataTest {

@org.junit.jupiter.api.Test
void getPhoneNumber() {
PhoneNumberData pnd = new PhoneNumberData("0749091217");
String expected = pnd.getPhoneNumber();
String actual = "0722683155";
Assertions.assertEquals(expected, actual);
}

@org.junit.jupiter.api.Test
void setPhoneNumber() {
PhoneNumberData pnd = new PhoneNumberData("0749091217");
pnd.phoneNumber = "0771519112";
pnd.setPhoneNumber(pnd.phoneNumber);
Assertions.assertEquals(pnd.getPhoneNumber(), pnd.phoneNumber);
}
}
31 changes: 31 additions & 0 deletions src/Server.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
public Server(int port) throws IOException {
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Contacts App");
System.out.println("Server started");
System.out.println("Waiting for a client");
while (true) {
Socket socket = null;
try {
socket = serverSocket.accept();
System.out.println("A new client is connected: " + socket);

DataInputStream inputStream = new DataInputStream(socket.getInputStream());
DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
System.out.println("Assigning new thread for this client");
ClientHandler clientHandler = new ClientHandler(socket, inputStream, outputStream);
clientHandler.start();
}
catch (IOException i) {
socket.close();
System.out.println(i);
}
}
}
}

0 comments on commit 0302867

Please sign in to comment.