Esempio n. 1
0
        private void ExecuteThread()
        {
            NetworkChannel channel = null;

            try
            {
                // Connect
                channel = new NetworkChannel(Connection);

                // Request
                JsonSearchRequestMessage jsonRequestMessage = new JsonSearchRequestMessage();

                JsonSearch jsonSearch = new JsonSearch()
                {
                    Keyword = Keyword, Filter = Filter
                };
                JsonSearchRequestData jsonRequestData = new JsonSearchRequestData()
                {
                    Search = jsonSearch
                };
                JsonPacket jsonRequest = new JsonPacket(jsonRequestMessage, Group.Encrypt(jsonRequestData));

                HttpRequest httpRequest = new HttpRequest(Session.Id)
                {
                    Data = Session.Encrypt(jsonRequest)
                };
                channel.Send(httpRequest);

                // Response
                HttpResponse httpResponse;
                channel.Receive(out httpResponse);
                Code = httpResponse.Code;

                if (httpResponse.Ok)
                {
                    JsonPacket jsonResponse = JsonPacket.Parse(Session.Decrypt(httpResponse.Data));
                    JsonSearchResponseMessage jsonResponseMessage = JsonSearchResponseMessage.Parse(jsonResponse.Message);
                    Debug.Assert(!string.IsNullOrEmpty(jsonResponseMessage.Id));

                    // Data
                    SearchList.Id = jsonResponseMessage.Id;
#if DEBUG
                    Log.Add(httpRequest, httpResponse, jsonRequest, jsonResponse);
#endif
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (channel != null)
                {
                    channel.Shutdown();
                }
            }
        }
Esempio n. 2
0
        public void Execute(HttpRequest httpRequest, JsonPacket jsonRequest)
        {
            Clear();

            // Connect
            NetworkChannel channel = new NetworkChannel(Connection);

            // Request
            JsonSearchRequestMessage jsonRequestMessage = JsonSearchRequestMessage.Parse(jsonRequest.Message);
            JsonSearchRequestData    jsonRequestData    = JsonSearchRequestData.Parse(Group.Decrypt(jsonRequest.Data));
            JsonSearch jsonSearch = jsonRequestData.Search;

            // Data
            List <FileComponent> compFiles = FileMap.Search(jsonSearch);

            if (compFiles.Count == 0)
            {
                channel.SendNotFound();
                return;
            }

            List <JsonFile> jsonFiles = new List <JsonFile>();

            foreach (FileComponent file in compFiles)
            {
                JsonFile jsonFile = (JsonFile)file;
                jsonFiles.Add(jsonFile);
            }

            // Response
            JsonSearchResponseMessage jsonResponseMessage = new JsonSearchResponseMessage();
            JsonSearchResponseData    jsonResponseData    = new JsonSearchResponseData()
            {
                Files = jsonFiles
            };
            JsonPacket jsonResponse = new JsonPacket(jsonResponseMessage, Group.Encrypt(jsonResponseData));

            HttpResponse httpResponse = new HttpResponse()
            {
                Data = Session.Encrypt(jsonResponse)
            };

            channel.Send(httpResponse);
#if DEBUG
            Log.Add(httpRequest, httpResponse, jsonRequest, jsonResponse);
#endif
        }
Esempio n. 3
0
        public string Execute(string jsonData)
        {
            Clear();

            // Connect
            NetworkChannel channel = new NetworkChannel(Connection);

            // Request
            JsonSearchRequestMessage jsonRequestMessage = new JsonSearchRequestMessage();
            JsonPacket  jsonRequest = new JsonPacket(jsonRequestMessage, jsonData);
            HttpRequest httpRequest = new HttpRequest()
            {
                Data = Session.Encrypt(jsonRequest)
            };

            // Response
            HttpResponse httpResponse;

            // NOTE: We need to lock this send/receive message pairs
            lock (Socket)
            {
                channel.Send(httpRequest);
                channel.Receive(out httpResponse);
            }

            Code = httpResponse.Code;

            if (httpResponse.Ok)
            {
                JsonPacket jsonResponse = JsonPacket.Parse(Session.Decrypt(httpResponse.Data));
#if DEBUG
                Log.Add(httpRequest, httpResponse, jsonRequest, jsonResponse);
#endif
                // Data
                return(jsonResponse.Data);
            }

            return(null);
        }
Esempio n. 4
0
        public void Execute(HttpRequest httpRequest, JsonPacket jsonRequest, SessionComponent session)
        {
            Clear();

            // Connect
            NetworkChannel channel = new NetworkChannel(Connection);

            // Request
            JsonSearchRequestMessage jsonRequestMessage = JsonSearchRequestMessage.Parse(jsonRequest.Message);

            // Data
            Entity         entity   = session.Owner;
            GroupComponent group    = entity.Get <GroupComponent>();
            EntityList     entities = GroupList.Get(group.Id);

            if (entities == null)
            {
                channel.SendNotFound();
                return;
            }

            // Response
            string jsonId = SecurityUtil.CreateKeyString();
            JsonSearchResponseMessage jsonResponseMessage = new JsonSearchResponseMessage(jsonId);
            JsonPacket jsonResponse = new JsonPacket(jsonResponseMessage);

            HttpResponse httpResponse = new HttpResponse()
            {
                Data = session.Encrypt(jsonResponse)
            };

            channel.Send(httpResponse);

            // Optimization: Keep track of the search list for faster downloads
            SearchListComponent download = entity.Get <SearchListComponent>();

            download.Clear();

            // Command
            lock (entities)
            {
                foreach (Entity e in entities)
                {
                    // Do not search yourself
                    if (entity.Id.Equals(e.Id))
                    {
                        continue;
                    }

                    TunnelComponent tunnel = e.Get <TunnelComponent>();
                    if (!tunnel.Connected)
                    {
                        continue;
                    }

                    CommandState state = new CommandState()
                    {
                        Id     = jsonId,
                        Data   = jsonRequest.Data,
                        Source = entity,
                        Target = e
                    };

                    Thread thread = new Thread(new ParameterizedThreadStart(ExecuteThread))
                    {
                        Priority = ThreadPriority.BelowNormal, IsBackground = true
                    };
                    thread.Start(state);
                }
            }
#if DEBUG
            Log.Add(httpRequest, httpResponse, jsonRequest, jsonResponse);
#endif
        }