Exemple #1
0
        private void processImage(Bitmap current, MySocket socket)
        {
            //abilito il calcolo
            motionDetector.MotionLevelCalculation = true;

            if (lastFrame != null)
            {
                lastFrame.Dispose();
            }

            lastFrame = (System.Drawing.Bitmap)current.Clone();

            // apply motion detector
            if (motionDetector != null)
            {
                motionDetector.ProcessFrame(ref lastFrame);

                // check motion level
                if (motionDetector.MotionLevel >= alarmLevel)
                {
                    Console.WriteLine("MotionLevel:" + motionDetector.MotionLevel);
                    //funzione che esegue operazioni quando scatta allarme
                    byte[] toSend = System.Text.Encoding.UTF8.GetBytes(ALARM);
                    socket.Send(toSend, toSend.Length, SocketFlags.None);
                    socket.Close();
                    Console.WriteLine("Allarme Scattato!\n");
                    isdead    = true;
                    keepalive = false;
                    String mac = myMac.Remove(myMac.Length - 1);
                    mDatabase.removeClient(mac);
                    sendMail("Alarm", "The client" + mac + " has detected a sospicious motion, the alarm was thrown", MotionDetector4.lastimage);
                }
                else
                {
                    byte[] toSend = System.Text.Encoding.UTF8.GetBytes(NOALARM);
                    socket.Send(toSend, toSend.Length, SocketFlags.None);
                    socket.Close();
                }
            }

            return;
        }
Exemple #2
0
        /**
         * This method manage the execution of a client request.
         */
        private void manageCommand()
        {
            String cmd = s.receiveString();

            try
            {
                switch (cmd)
                {
                case "getPort\0":
                    Console.WriteLine(cmd);
                    String macaddr = s.receiveString();
                    Console.WriteLine(macaddr);

                    String macToCheck = macaddr.Substring(0, macaddr.Length - 1);

                    int port = startingport + progressiveport;
                    progressiveport++;

                    Boolean ok = db.insertClient(macToCheck, port);
                    if (!ok)
                    {
                        Console.WriteLine("Error: Impossible to save new client in db!");
                        sendPort(-1);
                        return;
                    }

                    sendPort(port);

                    // This method manage the comunication between this client and the server.
                    ClientManager client = new ClientManager(macaddr, port);
                    client.start();

                    Console.WriteLine("Client: " + macaddr + " on port: " + port + " has ended!\n");
                    break;

                default:
                    Console.WriteLine("Error: received unknown request!\nCommand: " + cmd + "\n");
                    break;
                }
            }
            catch (Exception e) {
                Console.WriteLine("Error: manage commeand " + e.Message + "\n");

                if (s != null)
                {
                    s.Close();
                }
            }
        }
Exemple #3
0
        private void checkKeepAlive()
        {
            while (keepalive)
            {
                if (lastTime != 0)
                {
                    if (CurrentTimeMillis() - lastTime > 50000)
                    {
                        Console.WriteLine("Warning client disconnected \n");
                        String mac     = myMac.Remove(myMac.Length - 1);
                        String subject = "Warning client disconnected";
                        String message = "The client: " + mac + " has shut down at: "
                                         + DateTime.Now.ToLongTimeString() + " of: " + DateTime.Now.ToLongDateString();

                        mDatabase.removeClient(mac);  // removing the client from the database
                        sendMail(subject, message, null);
                        keepalive = false;
                        clientsock.Close();
                        isdead = true;
                    }
                }
            }
        }
Exemple #4
0
        public void start()
        {
            Socket     handlerClient;
            IPAddress  ip            = IPAddress.Parse(Constants.SERVER_IP_ADDR);
            IPEndPoint localEndPoint = new IPEndPoint(ip, porta);
            Socket     listener      = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            Boolean    ok;

            mDatabase.OpenConnect();

            try {
                Thread keepalive = new Thread(() => checkKeepAlive());
                keepalive.Start();

                listener.Bind(localEndPoint);
                listener.Listen(2); // Start listening for connections.
            }
            catch (Exception e) {
                Console.WriteLine("Unexpected Error");
                Console.WriteLine("Source : " + e.Source);
                Console.WriteLine("Message : " + e.Message);
                mDatabase.CloseConnect();
                return;
            }

            while (!isdead)
            {
                try
                {
                    Console.WriteLine("Waiting for a connection..." + "from client: " + this.myMac + "on port:" + porta);
                    Console.WriteLine("");
                    // Program is suspended while waiting for an incoming connection.
                    handlerClient = listener.Accept();

                    clientsock = new MySocket(handlerClient);

                    String cmd = receiveString(clientsock.s);
                    switch (cmd)
                    {
                    case "keep\0":
                        Console.WriteLine(cmd);
                        String macaddr2 = clientsock.receiveString();
                        String time     = clientsock.receiveString();
                        if (macaddr2.Equals(this.myMac))
                        {
                            Console.WriteLine("Received keepalive from: " + macaddr2 + "at: " + time);
                            lastTime = Int64.Parse(time);
                        }

                        byte[] toSend = System.Text.Encoding.UTF8.GetBytes(OK);
                        clientsock.Send(toSend, toSend.Length, SocketFlags.None);
                        clientsock.Close();
                        break;

                    case "firstImage\0":
                        Console.WriteLine("Received first image from: " + myMac);
                        System.Drawing.Bitmap current1 = receiveFile(lungImage, clientsock.s);
                        long time1 = CurrentTimeMillis();

                        String strValue = myMac;
                        strValue = Regex.Replace(strValue, @"-", "");
                        strValue = strValue.Remove(strValue.Length - 1);

                        String pictureFolderName = strValue + "\\firstimage" + time1 + ".jpg";
                        String picturePath       = Constants.SERVER_DIRECTORY + Constants.IMAGES_FOLDER + pictureFolderName;
                        String relativePath      = Constants.IMAGES_FOLDER + pictureFolderName;

                        bool exists = System.IO.Directory.Exists(Constants.SERVER_DIRECTORY + Constants.IMAGES_FOLDER + "\\" + strValue);
                        if (!exists)
                        {
                            System.IO.Directory.CreateDirectory(Constants.SERVER_DIRECTORY + Constants.IMAGES_FOLDER + "\\" + strValue);
                        }
                        try
                        {
                            current1.Save(picturePath);
                            String path = relativePath.Replace("\\", "/");
                            ok = mDatabase.insertSuspiciousPicturePath(myMac, time1, @"./" + path);
                            if (!ok)
                            {
                                Console.WriteLine("Error: Impossible to store picture: " + picturePath + " on the database!\n");
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("Error! Impossible to store the received picture! Exception: " + ex.ToString() + "\n");
                        }
                        Thread thread = new Thread(() => processImage(current1, clientsock));
                        thread.Start();
                        break;

                    case "manageImage\0":
                        Console.WriteLine("Received image from: " + myMac);
                        System.Drawing.Bitmap current = receiveFile(lungImage, clientsock.s);
                        Thread thread2 = new Thread(() => processImage(current, clientsock));
                        thread2.Start();
                        break;

                    case "unbind\0":
                        Console.WriteLine("Received unBind from: " + myMac);
                        isdead    = true;
                        keepalive = false;
                        String mac = myMac.Remove(myMac.Length - 1);
                        mDatabase.removeClient(mac);
                        break;

                    default:
                        Console.WriteLine("Error : Unknokwn command");
                        byte[] toSend2 = System.Text.Encoding.UTF8.GetBytes(UNKNOW_COMMND);
                        clientsock.Send(toSend2, toSend2.Length, SocketFlags.None);
                        clientsock.Close();
                        break;
                    }
                }
                catch (Exception e) {
                    Console.WriteLine("Unexpected Error");
                    Console.WriteLine("Source : " + e.Source);
                    Console.WriteLine("Message : " + e.Message);
                }
                finally {
                    mDatabase.CloseConnect();
                }
            }
        }