Ejemplo n.º 1
0
        public bool AddSmasher(ClientInfo info)
        {
            string infoJson = JsonConvert.SerializeObject(info);
            Uri addSmasherUrl = new Uri(mServerUrl.AbsoluteUri + "addsmasher");
            string responseJson = SendHttpRequest(addSmasherUrl, POST, infoJson);

            // HACKYYYYYY, but is probably quicker than deserialising! :)
            return !responseJson.Contains("FAIL");
        }
Ejemplo n.º 2
0
        public void Connect(string serverAddress, ClientInfo selfInfo)
        {
            Uri serverUrl = new Uri("http://" + serverAddress, UriKind.Absolute);
            mApi = new ServerApi(serverUrl);
            mSelfInfo = selfInfo;

            // Start Keep Alive loop in a different thread.
            // This will update the list of connected smashers and get more from the server if necessary
            mIsRunning = true;
            Thread updateThread = new Thread(new ThreadStart(() => {
                Update();
            }));
            updateThread.Start();
        }
Ejemplo n.º 3
0
        public IEnumerable<string> GetSmasherList(ClientInfo self)
        {
            string url = mServerUrl.AbsoluteUri + "smasherlist.json";
            if (!string.IsNullOrEmpty(self.Address))
            {
                // Added self so that the server can ignore it
                url += "/filter/" + self.Address;
            }

            Uri getListUrl = new Uri(url);
            string smasherListJson = SendHttpRequest(getListUrl, GET);

            if (!string.IsNullOrEmpty(smasherListJson))
            {
                // We got the list, deserialise! :)
                IEnumerable<string> smasherList = JsonConvert.DeserializeObject<IEnumerable<string>>(smasherListJson);
                return smasherList;
            }

            return null;
        }
Ejemplo n.º 4
0
        public static void Main(string[] args)
        {
            // Parse arguments or use default values
            string serverAddress = "localhost:3000";
            string listenerPort = "1234";
            ParseArguments(args, ref serverAddress, ref listenerPort);

            // Get client information
            IPAddress ipAddress = SmasherAddressUtil.GetIpAddress(AddressFamily.InterNetwork);
            ClientInfo selfInfo =
                new ClientInfo(ipAddress.ToString() + ":" + listenerPort, "0.0.1");

            // Create consumers
            Console.WriteLine("MAIN - Create Job Consumers");

            LocalJobConsumer local = new LocalJobConsumer(2);
            RemoteJobConsumer remote = new RemoteJobConsumer();
            remote.Connect(serverAddress, selfInfo);

            // Create the job manager (different thread)
            Console.WriteLine("MAIN - Start Job Manager");

            JobManager manager = new JobManager();
            Thread managerThread = new Thread(new ThreadStart(() => {
                if (listenerPort == gListenter)
                    manager.Start(local, null);
                else
                    manager.Start(remote, null);
            }));

            // We need the manager to one of the consumer delegates, set them here before starting consuming stuff!!!
            // Not, no circular dependencies ;) and that's why we love delegates
            SetConsumerDelegates(local, manager, "local");
            SetConsumerDelegates(remote, manager, "remote");

            // Now we can start the manager
            managerThread.Start();

            /**/ // Toggle debug code
            if (listenerPort != gListenter)
            {
                int seed = DateTime.Now.Millisecond;
                //int seed = 339;
                Random generator = new Random(seed);
                Console.WriteLine("MAIN - Current Seed is {0}", seed); // in case we need to test a specific case
                for (uint i = 0; i < 10; ++i)
                {
                    manager.EnqueueJob(new SleepJob(i, generator.Next(10000)));
                }
            }
            /**/

            // Start listening for network jobs
            Console.WriteLine("MAIN - Start Network Listener");

            NetworkJobListener jobListener = new NetworkJobListener();
            jobListener.JobReceived += (job) => {
                Console.WriteLine("LIST - Received {0} job over the network", job.Id);
                manager.EnqueueJob(job);
            };
            jobListener.JobReturned += (job) => {
                Console.WriteLine("LIST - Returned {0} job over the network", job.Id);
            };

            Thread listenerThread = new Thread(new ThreadStart(() => {
                try
                {
                    if (!jobListener.Listen(serverAddress, selfInfo))
                        Console.WriteLine("LIST - Failed to add to the server");
                }
                catch (SocketException)
                {
                    Console.WriteLine("LIST - We're running without the listener!");
                }
            }));

            if (listenerPort == gListenter)
                listenerThread.Start();

            /**/ // Toggle 5 sec shutdown
            Thread debugTerminate = new Thread(new ThreadStart(() => {
                Thread.Sleep(5000);
                // A null job will shutdown the Job Manager which is the only thread we're waiting for
                manager.EnqueueJob(null);
            }));
            debugTerminate.Start();
            /**/

            managerThread.Join();

            // Terminate update threads!
            Console.WriteLine("MAIN - Terminating update loops");

            remote.Disconnect();
            jobListener.Stop();

            Console.WriteLine("MAIN - The End!");
            Console.ReadKey();
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Listen the specified serverAddress, listenerPort and clientVersion.
        /// THIS BLOCKS THE THREAD! Run in a different thread if you don't want to block!
        /// </summary>
        public bool Listen(string serverAddress, ClientInfo listenerInfo)
        {
            if (mIsListening)
            {
                throw new InvalidOperationException("Sorry, you can't listen with the same listener twice...");
            }

            Uri serverUrl = new Uri("http://" + serverAddress, UriKind.Absolute);
            mApi = new ServerApi(serverUrl);
            mListenerInfo = listenerInfo;

            if (!mApi.AddSmasher(mListenerInfo))
                return false;

            // Start listening
            mIsListening = true;
            UpdateLoop(SmasherAddressUtil.GetSmasherEndPoint(mListenerInfo.Address));

            return true;
        }