LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-08-2006, 04:57 PM   #1
Dark Carnival
Member
 
Registered: Jun 2003
Posts: 166

Rep: Reputation: 30
C#, Mono and ISerializable (It's driving me crazy!)


Ok.. I've spent 6 hours straight on getting this to work and I've encountered *alot* of bizzare error messages so bear with me if I seem a bit wierd...

Basically I have a class called Proxy which inherits from WebProxy and ISerializable and implements a
deserialization constructor along with a the GetObjectData method for serialization. I've looked over some 5 tutorials concerning serialization and this is what I've been brewing together:

(It's 3 different files because it's a part of a project I'm working on)

File 1: proxyClass.cs
Code:
// created on 11/3/2006 at 8:39 AM

using System;

using System.Net;

using System.Runtime.Serialization;

namespace AnoDownload

{

[Serializable()]

	class Proxy : WebProxy, ISerializable

	{

		private static int numberOfProxies; //Holds the number of proxy objects (and logicaly) the amount of proxies available to the program

		

		public string address; //holds the url (and port) of the proxy

		

		public string getAddress

		{

			get

			{

				return address;

			}

		}

		

		//Variables associated with possible NetWorkCredential

		public string username = null;

		public string getUsername

		{

			get

			{

				return username;

			}

		}

		

		public string password = null;

		public string getPassword

		{

			get

			{

				return password;

			}

		}

		

		public string domain = null;

		public string getDomain

		{

			get

			{

				return domain;

			}

		}

		

		//Variables associated with lockout (cooldown for rapidshare, megaupload, sexupload)

		//Info: it doesn't really hold the number of minutes to be locked out it

		//		holds the time (and date) of the moment where the lockout should've expired

		DateTime rapidshareCom = DateTime.MinValue; //MinValue = 1/1/0001 12:00:00 AM  ((which won't be something our user has, unless his watch is seriously fucked up.)

		DateTime rapidshareDe = DateTime.MinValue;

		DateTime megauploadCom = DateTime.MinValue;

		DateTime sexuploadCom = DateTime.MinValue;

		

		// Constructors

		

		public Proxy(string proxyUrl, bool bypass) : base(proxyUrl, bypass) //assume that no user credentials are needed

		{ //default constructor

			numberOfProxies++;    //just to keep track of the number of proxies

			address = proxyUrl;

			

			//WebProxy proxy = new WebProxy(address, true);

		}

		

		public Proxy(string proxyUrl, bool bypass, string inUsername, string inPassword, string inDomain) : base(proxyUrl, bypass) //use overloaded webproxy constructor WebProxy(url, bypass) -- bypass = use direct connections for LAN

		{

			numberOfProxies++; //just to keep track of the number of proxies

			address = proxyUrl;

			

			username = inUsername;

			password = inPassword;

			domain = inDomain;

			

			//WebProxy Proxy = new WebProxy(address, true);

			

			//Proxy.Credentials = new NetworkCredential(username, password, domain);

			this.Credentials = new NetworkCredential(username, password, domain);

		}

		

		///////SERIALIZATION IMPLEMENTATION

		//////////////////////////////////////////////////////////////////////////////////////////////////////////

		//Deserialization constructor

		public Proxy(SerializationInfo info, StreamingContext context) : base(info, context) 

    	{

    		try

    		{

    			//Get the values from info and assign them to the appropriate properties

    			address = (string)info.GetValue("storedAddress", typeof(string));

    			username = (string)info.GetValue("storedUsername", typeof(string));

    			password = (string)info.GetValue("storedPassword", typeof(string));

    			domain = (string)info.GetValue("storedDomain", typeof(string));

    		}

    		catch (Exception e)

    		{

    			Console.WriteLine("Debug(deserialization constructor): Exception Caught!");

    			Console.WriteLine("ErrorMsg: " + e);

    		}

    	}

    	

    	//Serialization method:

    	public new void GetObjectData(SerializationInfo info, StreamingContext context)

		{

        	// Any other custom data which needs to be transferred

        	// info.AddValue("STORE-NAME", ORIG-VAR-NAME);

        	

        	//Debug messages about values of variables before serialization should store them

        	Console.WriteLine("Debug: Serialization method called");

        	Console.WriteLine("Debug: Value of address: " + address);

        	Console.WriteLine("Debug: Value of username: " + username);

        	Console.WriteLine("Debug: Value of password: " + password);

        	Console.WriteLine("Debug: Value of domain: " + domain);

        	

        	try

        	{

        	info.AddValue("storedAddress", address);

        	info.AddValue("storedUsername", username);

        	info.AddValue("storedPassword", password);

        	info.AddValue("storedDomain", domain);

        	}

        	catch (Exception e)

        	{

      		    Console.WriteLine("Debug(GetObjectData): Exception Caught!");

    			Console.WriteLine("ErrorMsg: " + e);

        	}

        	base.GetObjectData( info, context ); //ctxt

		}

		//////////////////////////////////////////////////////////////////////////////////////////////////////////

		

		

		// Class methods

		public void setLockoutTimer(string serviceName, double timeOut)

		{

			//Sets the lockout timer for a service var by taking the current time and adding the number of timeout minutes

			switch (serviceName)

			{

				case "rapidshare.com":

					Console.WriteLine("Stub: rapidshare.com lockout timer activated");

					rapidshareCom = DateTime.Now.AddMinutes(timeOut);

					break;

				case "rapidshare.de":

					Console.WriteLine("Stub: rapidshare.de lockout timer activated");

					rapidshareDe = DateTime.Now.AddMinutes(timeOut);

					break;

				case "megaupload.com":

					Console.WriteLine("Stub: megaupload.com lockout timer activated");

					megauploadCom = DateTime.Now.AddMinutes(timeOut);

					break;

				case "sexupload.com":

					Console.WriteLine("Stub: sexupload.com lockout timer activated");

					sexuploadCom = DateTime.Now.AddMinutes(timeOut);

					break;

				default:

					Console.WriteLine("Error: serviceName was not an expected service (rapidshare.com/rapidshare.de\nmegaupload.com/sexupload.com");

					break;

			}

		}

		

		public bool isAvailable(string serviceName)

		{

			//Should return a bool indicating whether or not the proxy is available to use on a specific downloadsite

			//what determines this is basically a timecheck against the lockout timer, if the current time less than

			//or equal to 0 then the current time is past the lockout timers expiration point and the proxy is free

			

			// The actual selection of a free proxy is left to the ProxyList class which will manage such things.

			

			//bool return value:  1 = True (the proxy is available for this service), 0 = false

			

			//should be less than zero if the proxy is available

			double timerDifference = 999;

			

			switch (serviceName)

			{

				case "rapidshare.com":

					Console.WriteLine("Stub: rapidshare.com lockout timer activated");

					timerDifference = (rapidshareCom - DateTime.Now).TotalMinutes; 

					break;

				case "rapidshare.de":

					Console.WriteLine("Stub: rapidshare.de lockout timer activated");

					timerDifference = (rapidshareDe - DateTime.Now).TotalMinutes;

					break;

				case "megaupload.com":

					Console.WriteLine("Stub: megaupload.com lockout timer activated");

					timerDifference = (megauploadCom - DateTime.Now).TotalMinutes;

					break;

				case "sexupload.com":

					Console.WriteLine("Stub: sexupload.com lockout timer activated");

					timerDifference = (sexuploadCom - DateTime.Now).TotalMinutes;

					break;

				default:

					Console.WriteLine("Error: serviceName was not an expected service (rapidshare.com/rapidshare.de\nmegaupload.com/sexupload.com");

					break;

			}

			

			if (timerDifference <= 0)

			{

				return true;

			}

			else //proxy is not available for this service

			{

				return false;

			}

		}

	}

}
File 2: proxyList.cs
Code:
// created on 11/8/2006 at 3:04 PM
using System;
using System.Net;
using System.Collections;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using AnoDownload;

namespace AnoDownload
{
	class ProxyList
	{
	
	protected string fileName;
	protected ArrayList proxyList = new ArrayList();
	
		public ProxyList(string inFileName)
		{
			fileName = inFileName;
			load(fileName);
		}
		/*
		public ~ProxyList()
		{
			Console.WriteLine("destructor..");
			//save(fileName);
		} */
		
		public void load(string inFileName)
		{
		/*
			FileInfo file = new FileInfo("serializationData2.dat");
			Stream fileStream = file.Open(FileMode.Open);
			BinaryFormatter reader = new BinaryFormatter();
			myDeSerializedArrayList = (ArrayList)reader.Deserialize(fileStream);
		*/
		
			FileInfo file = new FileInfo(inFileName);
			Stream fileStream = null;
			if ( file.Exists )
			{
				fileStream = file.Open(FileMode.Open);
				BinaryFormatter reader = new BinaryFormatter();
				proxyList = (ArrayList)reader.Deserialize(fileStream);
				fileStream.Close();
			}
			else
			{
				Console.WriteLine("File does not exist, skipping load");
			}
		}
		
		public void save(string inFileName)
		{
		/*
			//Begin serialization process:
			FileInfo file = new FileInfo("serializationData2.dat");
			Stream fileStream = file.Open(FileMode.Create);
			BinaryFormatter writer = new BinaryFormatter();
			writer.Serialize(fileStream, myArrayList);
			fileStream.Close();
		*/
			Console.WriteLine("Debug: ProxyList.save method running (Serialization should occur!)");
			FileInfo file = new FileInfo(inFileName);
			Stream fileStream = null;
			if ( file.Exists )
			{
				fileStream = file.Open(FileMode.Open);
			}
			else
			{
				fileStream = file.Open(FileMode.Create);
			}
			BinaryFormatter writer = new BinaryFormatter();
			writer.Serialize(fileStream, proxyList);
			fileStream.Close();
		}
		
		public void debugPrintProxyList()
		{
			Console.WriteLine("Debug method: debugPrintProxyList() accessed\n");
			
			for ( int i = 0; i < proxyList.Count; i++)
			{
				Console.WriteLine("Proxy number: " + i);
				Console.WriteLine("Url: " + ((Proxy)proxyList[i]).getAddress );
				Console.WriteLine("Username: " + ((Proxy)proxyList[i]).getUsername );
				Console.WriteLine("Password: " + ((Proxy)proxyList[i]).getPassword );
				Console.WriteLine("Domain: " + ((Proxy)proxyList[i]).getDomain );
				Console.WriteLine("-----------------------------------------");
			} 
		}
		
		public void addProxy(string proxyUrl)
		{
			proxyList.Add( new Proxy(proxyUrl, true) );
		}
	}
}
File 3: testProxyList.cs
Code:
// created on 11/8/2006 at 4:26 PM
using System;
using AnoDownload;

class MainClass
{
	public static void Main(string[] args)
	{
		ProxyList newlist = new ProxyList("tabt.dat");
		
		//adding 3 proxies
		newlist.addProxy("http://127.0.0.1:80");
		newlist.addProxy("http://127.0.0.2:80");
		newlist.addProxy("http://127.0.0.3:80");
		
		//Print a list of proxies
		newlist.debugPrintProxyList();
		
		newlist.save("tabt.dat");
	}
}
Now in this state, I tested it on a Windows machine with the MS csc compiler and it flat out worked
but if I try to compile with mono (using either mcs or gmcs) I must comment
Code:
base.GetObjectData( info, context );
out or else it will complain
and say that the WebProxy class doesn't contain a definition for 'GetObjectData' which is bullshit because WebProxy is supposed to inherit from the ISerializable class
MSDN Link confirming that WebProxy inherits from ISerializable: MSDN Page on WebProxy

Please.. I'm close to just scrapping the whole deal and go back to gaming. I've looked through every tutorial I could and it should be good to go as far as I can see.. But it ain't..

If someone manages to help me out. Then thanks a lot man, you have no idea how helpful you'd be!

Last edited by Dark Carnival; 11-08-2006 at 05:01 PM.
 
Old 11-09-2006, 07:22 AM   #2
Dark Carnival
Member
 
Registered: Jun 2003
Posts: 166

Original Poster
Rep: Reputation: 30
To be honest I don't really expect anyone can answer it anyway. But still, it would be a pleasant surprise
 
Old 11-11-2006, 05:34 AM   #3
Limax
LQ Newbie
 
Registered: Jul 2006
Location: Germany
Distribution: Suse 10.1/LFS
Posts: 4

Rep: Reputation: 0
If you look at the Mono Documentation (go-mono.com), it doesn't seem to implement ISerializable (although MS.NET does).
 
Old 11-14-2006, 02:24 PM   #4
Dark Carnival
Member
 
Registered: Jun 2003
Posts: 166

Original Poster
Rep: Reputation: 30
I rewrote the entire thing and suddenly Mono seemed to understand where I was going :P
It's incredibly frustrating, but the only good advice I can give a prospective ISerialize class writer is:

Build an extremely simple testcase in one single file - make that work (like really test: you must make it work so that it can read a file which was written when the program was run before, serializing and deserializing in one go seems to work almost always)

When you've got the test example, save that, copy the entire thing to a new build directory, add some functionality, verify that this with mono too, copy this directory to a new place and repeat the process until you have what you wanted.

Mono is much more picky about these things and you can get the most cryptic of errors (it may throw a file not found exception in the middle of it's deserializing class, for example)
but in the end, it's possible, my class ended up inhering from both IWebProxy and ISerializable, and it worked
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[ ] @ are driving me crazy FireInTheDark Linux - Software 1 11-28-2004 09:42 AM
Sound driving me crazy eatstatic Slackware 7 11-21-2004 03:47 PM
gnuplot 4.0 is driving me crazy windowsrefund Linux - Software 1 09-01-2004 07:42 AM
kppp is driving me crazy rhoniel Linux - Networking 2 07-19-2004 11:35 PM
Help this is driving me crazy!!!!!! Wolfy Linux - Hardware 1 07-07-2004 01:32 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration