Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package bstms.backend;
- import java.io.*;
- import java.net.*;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.util.Scanner;
- public final class Camera {
- private final int port = 14000;
- private final String serverAddr = "localhost";
- public Socket socket;
- public ObjectInputStream In;
- public ObjectOutputStream Out;
- private String name,url;
- public Camera(String name_,String url_) throws IOException {
- socket = new Socket(InetAddress.getByName(serverAddr), port);
- Out = new ObjectOutputStream(socket.getOutputStream());
- Out.flush();
- In = new ObjectInputStream(socket.getInputStream());
- this.name = name_;
- this.url = url_;
- run();
- }
- public void run() {
- try {
- Server_MSG ms = new Server_MSG("VIDEO_SOURCE", "",this.name);
- this.send(ms);
- } catch (ClassNotFoundException e) {
- e.getMessage();
- }
- System.out.println("LOGGED IN AS : " + name);
- boolean keepRunning = validate(name);
- while (keepRunning) {
- try {
- System.out.print("waiting for camera commands");
- String cmd = In.readUTF();
- if ("status".equals(cmd)) {
- Server_MSG m = new Server_MSG("status", "",name);
- this.send(m);
- }
- if ("close".equals(cmd)) {
- System.out.println("Quiting...");
- System.exit(-1);
- }
- } catch (Exception ex) {
- keepRunning = false;
- System.out.println("Connection Failure\n");
- ex.getLocalizedMessage();
- }
- }
- }
- public void send(Server_MSG msg) throws ClassNotFoundException {
- try {
- Out.writeObject(msg);
- Out.flush();
- System.out.println(msg.toString() + " [ OK ] ");
- if (!"transact".equals(msg.type)) {
- Server_MSG tr = (Server_MSG) In.readObject();
- System.out.println(tr.camera_name);
- }
- } catch (IOException ex) {
- ex.getLocalizedMessage();
- }
- }
- public void closeThread(Thread t) {
- t = null;
- }
- public static void main(String args[]) {
- String server_name = "localhost";
- try {
- Camera k = new Camera(server_name, "localhost:8080/cam001.mjp");
- } catch (IOException ex) {
- ex.getMessage();
- System.out.println("SERVER NOT RUNNING.");
- }
- }
- private boolean validate(String name) {
- boolean status = false;
- Connection conn;
- PreparedStatement pst = null;
- ResultSet rs = null;
- String url = "jdbc:mysql://localhost:3306/";
- String dbName = "mobile_money";
- String driver = "com.mysql.jdbc.Driver";
- String userName = "root";
- String password = "";
- try {
- Class.forName(driver).newInstance();
- conn = DriverManager
- .getConnection(url + dbName, userName, password);
- pst = conn.prepareStatement("select * from mob_kiosk where k_name='" + name + "' or sim='" + name + "'");
- rs = pst.executeQuery();
- status = rs.next();
- } catch (Exception e) {
- System.out.println(e);
- }
- return status;
- }
- }
Add Comment
Please, Sign In to add comment