Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Decompiled with JetBrains decompiler
- // Type: WindowsFormsApplication1.Form1
- // Assembly: WindowsFormsApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
- // MVID: A0967493-2B5E-45DB-9F7E-6A0B14E2A48A
- // Assembly location: C:\Users\yonka\Desktop\WeatherClient\WeatherClient.exe
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Drawing;
- using System.IO;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading;
- using System.Windows.Forms;
- using WindowsFormsApplication1.Properties;
- namespace WindowsFormsApplication1
- {
- public class Form1 : Form
- {
- private const int PORT = 77;
- private string ip;
- private string city;
- private string date;
- private int lastIndex = 0;
- private Dictionary<string, Bitmap> resourcesWeatherDictioanry = new Dictionary<string, Bitmap>();
- private string[] keysHirarchey = new string[7]
- {
- "sky is clear",
- "few clouds",
- "shower rain",
- "thunder storm",
- "clouds",
- "rain",
- "snow"
- };
- private IContainer components = (IContainer) null;
- private PictureBox imgBoxForecast;
- private Label locationLBL;
- private Label dateLBL;
- private Label textLBL;
- private Label tempLBL;
- private void start_comm()
- {
- ServerResponse response = (ServerResponse) null;
- this.imgBoxForecast.SizeMode = PictureBoxSizeMode.Zoom;
- Dictionary<string, string> dictionary = this.readConfigFile("config.ini");
- if (dictionary != null)
- {
- this.ip = dictionary["server-ip"];
- this.city = dictionary["city"];
- this.date = dictionary["date"];
- Console.WriteLine(this.ip + ", " + this.city + ", " + this.date);
- response = this.requestResults(this.ip, this.city, this.date);
- }
- this.proccessResponse(response);
- }
- public Form1()
- {
- this.InitializeComponent();
- this.InitializeResourcesWeatherDictioanry();
- new Thread(new ThreadStart(this.start_comm)).Start();
- }
- private string CalculateChecksum(string city, string date)
- {
- city = city.ToLower();
- int num1 = 0;
- int num2 = 0;
- foreach (char ch in city)
- {
- if (ch >= 'a' && ch <= 'z')
- num1 += (int) ch - 97 + 1;
- }
- foreach (char ch in date)
- {
- if (ch >= '0' && ch <= '9')
- num2 += (int) ch - 48;
- }
- return num1.ToString() + "." + (object) num2;
- }
- private ServerResponse requestResults(string ip, string city, string date)
- {
- byte[] buffer = new byte[1024];
- string checksum = this.CalculateChecksum(city, date);
- try
- {
- IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse(ip), 77);
- Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- try
- {
- socket.Connect((EndPoint) ipEndPoint);
- socket.Receive(buffer);
- string s = "100:REQUEST:city=" + city + "&date=" + date + "&checksum=" + checksum;
- for (int index = 0; index < 1024; ++index)
- buffer[index] = index >= s.Length ? (byte) 0 : (byte) s[index];
- socket.Send(Encoding.ASCII.GetBytes(s));
- int num = socket.Receive(buffer);
- string raw_response = "";
- for (int index = 0; index < num; ++index)
- raw_response += ((char) buffer[index]).ToString();
- socket.Shutdown(SocketShutdown.Both);
- socket.Close();
- return new ServerResponse(raw_response);
- }
- catch (ArgumentNullException ex)
- {
- Console.WriteLine("ArgumentNullException : {0}", (object) ex.ToString());
- }
- catch (SocketException ex)
- {
- Console.WriteLine("SocketException : {0}", (object) ex.ToString());
- }
- catch (Exception ex)
- {
- Console.WriteLine("Unexpected exception : {0}", (object) ex.ToString());
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.ToString());
- }
- return (ServerResponse) null;
- }
- private static byte[] GetBytes(string str)
- {
- byte[] numArray = new byte[str.Length * 2];
- Buffer.BlockCopy((Array) str.ToCharArray(), 0, (Array) numArray, 0, numArray.Length);
- return numArray;
- }
- private static string GetString(byte[] bytes)
- {
- char[] chArray = new char[bytes.Length / 2];
- Buffer.BlockCopy((Array) bytes, 0, (Array) chArray, 0, bytes.Length);
- return new string(chArray);
- }
- private Dictionary<string, string> readConfigFile(string fileName)
- {
- Dictionary<string, string> dictionary = new Dictionary<string, string>();
- using (StreamReader streamReader = new StreamReader(fileName))
- {
- string str;
- while ((str = streamReader.ReadLine()) != null)
- {
- string[] strArray = str.Split('=');
- if (strArray.Length == 2)
- dictionary[strArray[0]] = strArray[1];
- }
- }
- return dictionary.Count != 3 || (dictionary["server-ip"] == null || dictionary["city"] == null || dictionary["date"] == null) ? (Dictionary<string, string>) null : dictionary;
- }
- private void proccessResponse(ServerResponse response)
- {
- if (response == null)
- {
- int num = (int) MessageBox.Show("ERROR: No Internet Connection / Server Unavailable");
- }
- else
- {
- foreach (string key in this.keysHirarchey)
- {
- if (response.getText().Contains(key))
- {
- this.imgBoxForecast.Image = (Image) this.resourcesWeatherDictioanry[key];
- break;
- }
- }
- this.Invoke((Delegate) (() => this.updateGUI(response)));
- }
- }
- private void updateGUI(ServerResponse response)
- {
- this.locationLBL.Text = response.getLocation();
- this.dateLBL.Text = response.getDate();
- this.textLBL.Text = response.getText();
- this.tempLBL.Text = ((int) response.getTemperture()).ToString() + "°";
- }
- private void InitializeResourcesWeatherDictioanry()
- {
- this.resourcesWeatherDictioanry["sky is clear"] = new Bitmap((Image) Resources.res_clear_sky_day);
- this.resourcesWeatherDictioanry["few clouds"] = new Bitmap((Image) Resources.res_few_clouds_day);
- this.resourcesWeatherDictioanry["clouds"] = new Bitmap((Image) Resources.res_clouds_day);
- this.resourcesWeatherDictioanry["rain"] = new Bitmap((Image) Resources.res_rain_day);
- this.resourcesWeatherDictioanry["shower rain"] = new Bitmap((Image) Resources.res_shower_rain);
- this.resourcesWeatherDictioanry["snow"] = new Bitmap((Image) Resources.res_snow);
- this.resourcesWeatherDictioanry["thunder storm"] = new Bitmap((Image) Resources.res_thunder_strom);
- }
- public void Form1_FormClosing(object sender, FormClosingEventArgs e)
- {
- if (!(string.Concat((object) e.CloseReason) != "UserClosing"))
- return;
- int num = (int) MessageBox.Show(string.Concat((object) e.CloseReason));
- }
- protected override void Dispose(bool disposing)
- {
- if (disposing && this.components != null)
- this.components.Dispose();
- base.Dispose(disposing);
- }
- private void InitializeComponent()
- {
- this.imgBoxForecast = new PictureBox();
- this.locationLBL = new Label();
- this.dateLBL = new Label();
- this.textLBL = new Label();
- this.tempLBL = new Label();
- ((ISupportInitialize) this.imgBoxForecast).BeginInit();
- this.SuspendLayout();
- this.imgBoxForecast.Location = new Point(12, 243);
- this.imgBoxForecast.Name = "imgBoxForecast";
- this.imgBoxForecast.Size = new Size(360, 216);
- this.imgBoxForecast.TabIndex = 2;
- this.imgBoxForecast.TabStop = false;
- this.locationLBL.AutoSize = true;
- this.locationLBL.Font = new Font("Microsoft Sans Serif", 18f, FontStyle.Regular, GraphicsUnit.Point, (byte) 177);
- this.locationLBL.Location = new Point(10, 43);
- this.locationLBL.Name = "locationLBL";
- this.locationLBL.Size = new Size(105, 29);
- this.locationLBL.TabIndex = 3;
- this.locationLBL.Text = "loading..";
- this.dateLBL.AutoSize = true;
- this.dateLBL.Location = new Point(12, 72);
- this.dateLBL.Name = "dateLBL";
- this.dateLBL.Size = new Size(50, 13);
- this.dateLBL.TabIndex = 4;
- this.dateLBL.Text = "loading...";
- this.textLBL.AutoSize = true;
- this.textLBL.Font = new Font("Microsoft Sans Serif", 12f, FontStyle.Regular, GraphicsUnit.Point, (byte) 177);
- this.textLBL.Location = new Point(12, 220);
- this.textLBL.Name = "textLBL";
- this.textLBL.Size = new Size(68, 20);
- this.textLBL.TabIndex = 5;
- this.textLBL.Text = "loading..";
- this.tempLBL.AutoSize = true;
- this.tempLBL.Font = new Font("Microsoft Sans Serif", 60f, FontStyle.Regular, GraphicsUnit.Point, (byte) 177);
- this.tempLBL.Location = new Point(214, 72);
- this.tempLBL.Name = "tempLBL";
- this.tempLBL.Size = new Size(110, 91);
- this.tempLBL.TabIndex = 6;
- this.tempLBL.Text = "-1";
- this.tempLBL.TextAlign = ContentAlignment.MiddleCenter;
- this.AutoScaleDimensions = new SizeF(6f, 13f);
- this.AutoScaleMode = AutoScaleMode.Font;
- this.ClientSize = new Size(384, 471);
- this.Controls.Add((Control) this.tempLBL);
- this.Controls.Add((Control) this.textLBL);
- this.Controls.Add((Control) this.dateLBL);
- this.Controls.Add((Control) this.locationLBL);
- this.Controls.Add((Control) this.imgBoxForecast);
- this.MaximizeBox = false;
- this.MaximumSize = new Size(400, 510);
- this.MinimumSize = new Size(400, 510);
- this.Name = nameof (Form1);
- this.Text = "Forecast Client - Magshimim";
- this.FormClosing += new FormClosingEventHandler(this.Form1_FormClosing);
- ((ISupportInitialize) this.imgBoxForecast).EndInit();
- this.ResumeLayout(false);
- this.PerformLayout();
- }
- }
- }
Add Comment
Please, Sign In to add comment