Guest User

Untitled

a guest
Mar 2nd, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. package bstms.backend;
  2.  
  3. import java.io.*;
  4. import java.net.*;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.PreparedStatement;
  8. import java.sql.ResultSet;
  9. import java.util.Scanner;
  10.  
  11. public final class Camera {
  12.  
  13. private final int port = 14000;
  14. private final String serverAddr = "localhost";
  15. public Socket socket;
  16. public ObjectInputStream In;
  17. public ObjectOutputStream Out;
  18. private String name,url;
  19.  
  20. public Camera(String name_,String url_) throws IOException {
  21. socket = new Socket(InetAddress.getByName(serverAddr), port);
  22. Out = new ObjectOutputStream(socket.getOutputStream());
  23. Out.flush();
  24. In = new ObjectInputStream(socket.getInputStream());
  25. this.name = name_;
  26. this.url = url_;
  27. run();
  28. }
  29.  
  30. public void run() {
  31. try {
  32. Server_MSG ms = new Server_MSG("VIDEO_SOURCE", "",this.name);
  33. this.send(ms);
  34. } catch (ClassNotFoundException e) {
  35. e.getMessage();
  36. }
  37. System.out.println("LOGGED IN AS : " + name);
  38. boolean keepRunning = validate(name);
  39. while (keepRunning) {
  40. try {
  41. System.out.print("waiting for camera commands");
  42. String cmd = In.readUTF();
  43. if ("status".equals(cmd)) {
  44. Server_MSG m = new Server_MSG("status", "",name);
  45. this.send(m);
  46. }
  47. if ("close".equals(cmd)) {
  48. System.out.println("Quiting...");
  49. System.exit(-1);
  50. }
  51. } catch (Exception ex) {
  52. keepRunning = false;
  53. System.out.println("Connection Failure\n");
  54. ex.getLocalizedMessage();
  55. }
  56. }
  57. }
  58.  
  59. public void send(Server_MSG msg) throws ClassNotFoundException {
  60. try {
  61. Out.writeObject(msg);
  62. Out.flush();
  63. System.out.println(msg.toString() + " [ OK ] ");
  64. if (!"transact".equals(msg.type)) {
  65. Server_MSG tr = (Server_MSG) In.readObject();
  66. System.out.println(tr.camera_name);
  67. }
  68. } catch (IOException ex) {
  69. ex.getLocalizedMessage();
  70. }
  71. }
  72.  
  73. public void closeThread(Thread t) {
  74. t = null;
  75. }
  76.  
  77. public static void main(String args[]) {
  78. String server_name = "localhost";
  79. try {
  80. Camera k = new Camera(server_name, "localhost:8080/cam001.mjp");
  81. } catch (IOException ex) {
  82. ex.getMessage();
  83. System.out.println("SERVER NOT RUNNING.");
  84. }
  85. }
  86.  
  87. private boolean validate(String name) {
  88. boolean status = false;
  89. Connection conn;
  90. PreparedStatement pst = null;
  91. ResultSet rs = null;
  92.  
  93. String url = "jdbc:mysql://localhost:3306/";
  94. String dbName = "mobile_money";
  95. String driver = "com.mysql.jdbc.Driver";
  96. String userName = "root";
  97. String password = "";
  98. try {
  99. Class.forName(driver).newInstance();
  100. conn = DriverManager
  101. .getConnection(url + dbName, userName, password);
  102. pst = conn.prepareStatement("select * from mob_kiosk where k_name='" + name + "' or sim='" + name + "'");
  103. rs = pst.executeQuery();
  104. status = rs.next();
  105. } catch (Exception e) {
  106. System.out.println(e);
  107. }
  108. return status;
  109. }
  110.  
  111. }
Add Comment
Please, Sign In to add comment