//private static IList<string> licensesMain = null;
        //private static IList<string> licensesBss = null;
        //System.IO.StreamWriter file = null;
        /*
        public ImageHandler() : base()
        {

            //System.IO.File.WriteAllText(HttpContext.Current.Server.MapPath("\\BioProcessorLog.txt"), "ctor");

            if (licensesMain == null)
            {
            //    file = new System.IO.StreamWriter(HttpContext.Current.Server.MapPath("\\BioProcessorLog.txt"), true);
            //    file.WriteLine("wsq: ctor");
            //    file.Flush();

                initFingersLicences(HttpContext.Current);
            }
        }

        ~ImageHandler()
        {
            //throw new Exception("destructor");

            if (licensesMain != null)
            {
                releaseFingersLicences();
                licensesMain = null;
            }
            //file.WriteLine("wsq: dtor");
            //file.Flush();
            //file.Close();
        }
         */
        public void ProcessRequest(HttpContext context)
        {
            //#if DEBUG
            //            System.Diagnostics.Debugger.Launch();
            //#endif

            //context.Response.ContentType = "application/xop+xml";
            context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            context.Response.Cache.SetNoStore();
            context.Response.Cache.SetExpires(DateTime.MinValue);

            if (context.Request.QueryString["getSessionState"] != null)
            {
                //object ses = context.Application["myerror"];
                if (context.Application["myerror"] != null)
                {
                    context.Response.Write(context.Application["myerror"]);
                    //context.Response.End();
                    //context.Application.Remove("myerror");
                    if (context.Request.QueryString["clearError"] != null)
                        context.Application["myerror"] = null;
                }
                return;
            }

            var cache = new MemoryCacheServiceClient();

            if (context.Request.QueryString["clearSession"] != null)
            {
                try
                {
                    cache.SetDirty();
                }
                catch (System.ServiceModel.FaultException ex)
                {
                    JavaScriptSerializer serializer = new JavaScriptSerializer();
                    context.Response.Write(serializer.Serialize(ex.Message));
                }
                catch (Exception ex)
                {
                    JavaScriptSerializer serializer = new JavaScriptSerializer();
                    context.Response.Write(serializer.Serialize(ex.Message));
                }
                //MemoryCache.Default.Remove("fingersCollection");
                //MemoryCache.Default.Remove("id");

                //context.Application.Remove("fingersCollection");
                //context.Application.Remove("id");
                return;
            }
            //context.Response.Cache.SetAllowResponseInBrowserHistory(false);

            //System.Object lockThis = new System.Object();

            //initFingersLicences(context);

            //_context = context;
            //_fingersCollectionList = new Dictionary<int, byte[]>();
            //            IList<string> licensesMain = null;
            //            IList<string> licensesBss = null;
            string id = context.Request.QueryString["id"];
            try {
                Int32.Parse(id);
            } catch (Exception) {
                context.Application["myerror"] = "Please enter a valid ID";
                throw new Exception("Please enter a valid ID");
            }

            string wsqQuery = context.Request.QueryString["wsq"];

            byte[] buffer = new byte[0];

            //            var cache = new MemoryCacheServiceClient();

            /*
            //file.WriteLine("wsq: " + wsqQuery);
            //file.Flush();

            DataSource ds = null;

            //if (System.Configuration.ConfigurationManager.AppSettings["Enroll"] == "db")
            if (getAppSetting("Enroll") == "db")
                ds = new DatabaseService();
            else
            {
                context.Response.BinaryWrite(new byte[1]);
                context.Response.End();
                return;
            }
            */
            if (wsqQuery == null)
            {
                try
                {
                    buffer = cache.GetPicture(id);
                    //buffer = ds.GetImage(IMAGE_TYPE.picture, Convert.ToInt32(id));
                    if (buffer != null)
                        context.Response.BinaryWrite(buffer);
                    //context.Response.End();
                }
                catch (Exception ex)
                {
                    context.Application["myerror"] = ex.Message;
                    throw new Exception(ex.Message);
                }
            }
            else
            {
                lock (lockThis)
                {
                    Dictionary<string, int> dict = new Dictionary<string, int>();
                    dict.Add("li", 0);
                    dict.Add("lm", 1);
                    dict.Add("lr", 2);
                    dict.Add("ll", 3);
                    dict.Add("ri", 4);
                    dict.Add("rm", 5);
                    dict.Add("rr", 6);
                    dict.Add("rl", 7);
                    dict.Add("lt", 8);
                    dict.Add("rt", 9);

                    ArrayList fingersCollection = null;
                    //MemoryStream ms = null;
                    try
                    {
                        fingersCollection = cache.GetQualityFingerCollection(id);

            //                        if (context.Application["fingersCollection"] == null || id != context.Application["id"] as String)
                        //if (HttpRuntime.Cache["fingersCollection"] == null || id != HttpRuntime.Cache["id"] as String)

                        //DataCache cache = CacheUtil.GetCache();
                        //ArrayList cfc = cache.Get("fingersCollection") as ArrayList;
                        //string cId = cache.Get("id") as String;

                        //if (MemoryCache.Default["fingersCollection"] == null || id != MemoryCache.Default["id"] as String)
                        //if (cfc == null || id != cId)
            /*
                        var cache = new MemoryCacheServiceClient();
                        //if (!cache.Contains(id))
                        if (!cache.IsDirty(id))
                        {
                            cache.GetImage(IMAGE_TYPE.wsq, Convert.ToInt32(id));

            //                            if (context.Application["fingersCollection"] != null)
            //                            if (HttpRuntime.Cache["fingersCollection"] != null)
            //                            if (MemoryCache.Default["fingersCollection"] != null)
                            //if (cfc != null)
                            //{
                                //context.Session.Remove("FingersLicences");
            //                                context.Application.Remove("fingersCollection");
            //                                context.Application.Remove("id");
            //                                HttpRuntime.Cache.Remove("fingersCollection");
            //                                HttpRuntime.Cache.Remove("id");
                                //MemoryCache.Default.Remove("fingersCollection");
                                //MemoryCache.Default.Remove("id");
                              //  cache.Remove("fingersCollection");
                              //  cache.Remove("id");
                                //MemoryCache.Default.Remove("fingersCollection");
                                //MemoryCache.Default.Remove("id");
                                //releaseFingersLicences();
                                //initFingersLicences(context);
                            //}

                            //initFingersLicences(context, out licensesMain, out licensesBss);

                            buffer = ds.GetImage(IMAGE_TYPE.wsq, Convert.ToInt32(id));

                            var bioProcessor = new BioProcessor.BioProcessor();
                            bioProcessor.processEnrolledData(buffer, out fingersCollection);

                            //ms = new MemoryStream(buffer);

                            // Construct a BinaryFormatter and use it to deserialize the data to the stream.
                            //BinaryFormatter formatter = new BinaryFormatter();
                            //formatter.Binder = new GenericBinder<WsqImage>();
                            //fingersCollection = formatter.Deserialize(ms) as ArrayList;
            //                            context.Application["id"] = id;
            //                            context.Application["fingersCollection"] = fingersCollection;
            //                            HttpRuntime.Cache["id"] = id;
            //                            HttpRuntime.Cache["fingersCollection"] = fingersCollection;
            //                            cache.Add("fingersCollection", fingersCollection);
            //                            cache.Add("id", id);
                            //cache.Put("id", id);
                            //cache.Put("fingersCollection", fingersCollection);
                            var dataContract = new FingerPrintDataContract();
                            dataContract.id = id;
                            dataContract.fingersCollection = fingersCollection;
                            cache.Put(dataContract);
                            //MemoryCache.Default["id"] = id;
                            //MemoryCache.Default["fingersCollection"] = fingersCollection;
                            //throw new Exception(fingersCollection.Count.ToString());
                            //releaseFingersLicences(licensesMain, licensesBss);

                        }
                        else
                            fingersCollection = cache.GetFingerCollection();
            //                        fingersCollection = MemoryCache.Default["fingersCollection"] as ArrayList;
            //                        fingersCollection = HttpRuntime.Cache["fingersCollection"] as ArrayList;
            //                        fingersCollection = context.Application["fingersCollection"] as ArrayList;

                        //throw new Exception((fingersCollection == null).ToString());
                        //throw new Exception("type: " + fingersCollection[dict[wsqQuery]].GetType().ToString());

                        //throw new Exception("type: " + fingersCollection[dict[wsqQuery]].ToString());
                        //byte[] buffer2 = new byte[0];
                        //image.ToByteArray(ImageFormat.Bmp);
                        //buffer = fingersCollection[dict[wsqQuery]] as byte[];
                        //throw new Exception("type: " + fingersCollection[dict[wsqQuery]].ToString());
                        //throw new Exception("type: " + buffer2.GetType().ToString());

                        //WsqImage wsqImage = fingersCollection[dict[wsqQuery]] as WsqImage;
                        //buffer = ConvertWSQToBmp(wsqImage);
            */
                        if (fingersCollection != null)
                            context.Response.BinaryWrite(fingersCollection[dict[wsqQuery]] as byte[]);
                        else
                            throw new Exception("Fingerprint image does not exist");
                        //context.Response.Flush();
                    }
                    catch (Exception ex)
                    {
                        //context.Response.Write("<script language=javascript>alert('ERROR');</script>");
                        //context.Response.Write(String.Format("<script>$('#error_box').val({0});</script>", ex.ToString()));
                        //context.Response.Flush();
                        //context.Response.Status = "kuku";
                        //context.Response.StatusDescription = "kuku2";
                        //context.Response.SubStatusCode = 333;

                        //context.Response.StatusCode = 404;
                        //context.Response.SuppressContent = true;
                        //HttpContext.Current.ApplicationInstance.CompleteRequest();
                        context.Application["myerror"] = ex.Message;
                        throw new Exception(ex.Message);
                    }
                    finally
                    {
                        //DisposeWSQImage();

                        //if (ms != null)
                          //  ms.Close();
                    }
                }
            }
        }
Example #2
0
        private Stream Scan(string id, int hand, string checkBoxesStates, string callback)
        {
            var result = "";
            var message = "";

            try
            {
                Int32.Parse(id);
            }
            catch (Exception)
            {
                //toReturn = "Please enter a valid ID";
                result = "error";
                message = "Please enter a valid ID";
            }

            if (result.Length == 0)
            {
                string[] checkBoxes = null;

                if (hand == 3)
                    checkBoxes = new[] { "1", "1", "1", "1" };
                else
                    checkBoxes = checkBoxesStates.Split(',');

                Scanner scanner = null;
                MemoryStream ms = null;
                try
                {

            #if DEBUG
                    System.Diagnostics.Debugger.Launch();
            #endif

                    var cache = new MemoryCacheServiceClient();
                    //ArrayList fingersCollection = cache.GetQualityFingerCollection(id);

                    var client = new DataServiceClient();
                    byte[] buffer = client.GetWSQImages(id);
                    ms = new MemoryStream(buffer);
                    BinaryFormatter formatter = new BinaryFormatter();
                    formatter.Binder = new WsqSerializationBinder.GenericBinder<WsqImage>();
                    ArrayList fingersCollection = formatter.Deserialize(ms) as ArrayList;

                    //List<WsqImage> fingersCollection = null;
                    //var bioProcessor = new BioProcessor.BioProcessor();
                    //bioProcessor.DeserializeWSQArray(buffer, out fingersCollection);

                    scanner = new Scanner();
                    scanner.FingersCollection = fingersCollection;

                    result = scanner.scan(hand, id, checkBoxes);

                    //var cache = new MemoryCacheServiceClient();
                    cache.SetDirty();

                    //var dataContract = new FingerPrintDataContract();
                    //dataContract.id = id;
                    //dataContract.fingersCollection = scanner.FingersCollection;
                    //cache.Put(dataContract);

                }
                catch (Exception ex)
                {
                    //if (scanner != null)
                    //    scanner.Disconnect();
                    //toReturn = ex.Message;
                    result = "error";
                    message = ex.Message;
                }
                finally
                {
                    if (ms != null)
                        ms.Close();
                }
            }

            message = System.Web.HttpUtility.JavaScriptStringEncode(message);

            //toReturn = callback + "({\"Status\":\"" + toReturn + "\"});";
            //toReturn = callback + "({\"error\":\"" + error + "\"});";
            WebOperationContext.Current.OutgoingResponse.ContentType = "application/javascript";
            //return new MemoryStream(Encoding.UTF8.GetBytes(toReturn));
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            var str = serializer.Serialize(new { result = result, message = message });
            str = callback + "(" + str + ")";
            return new MemoryStream(Encoding.UTF8.GetBytes(str));

            //IList<Object> UserInfo = new List<Object>();

            /* var jsonEmptyData = new
             {
                 Total = 0,
                 Page = 1,
                 Records = 0,
                 Rows = new { item_id = 0, part_no = "", part_no_description = "", sum_quantity = 0, unit_price = 0d, total_price = 0d }
             };
             return Json(jsonEmptyData, JsonRequestBehavior.AllowGet);*/
            //throw new Exception("scan left");
            /*
            string toReturn = id + " --- "; // +checkBoxesStates.Count.ToString();

            var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();

            StringBuilder sb = new StringBuilder();
            sb.Append(callback + "(");
            sb.Append(serializer.Serialize(toReturn));
            sb.Append(");");

            WebOperationContext.Current.OutgoingResponse.ContentType = "application/javascript";
            return sb.ToString();
            */
            //WebOperationContext.Current.OutgoingResponse.ContentType = "application/javascript";
            //var toReturn = callback + "({\"Status\":\"" + checkBoxesStates.Length + "\"});";
            //return new MemoryStream(Encoding.UTF8.GetBytes(toReturn));

            //return callback + "({\"Status\":\"OK\"});";

            //Context.Response.Clear();
            //Context.Response.ContentType = "application/json";
            //Context.Response.Write(sb.ToString());
            //Context.Response.End();

            //return id + " --- "; // +checkBoxesStates.Count.ToString();
        }