context: /*
   OpenSDRM - Open Secure Digital Rights Management
   Copyright (c) 2002 Adetti
  
   This software was originaly developed under the MOSES project (IST-2001-34144)
  
  ________                         _________________ __________    _____
  \_____  \ ______   ____   ____  /   _____/\______ \\______   \  /     \
   /   |   \\____ \_/ __ \ /    \ \_____  \  |    |  \|       _/ /  \ /  \
  /    |    \  |_> >  ___/|   |  \/        \ |    `   \    |   \/    Y    \
  \_______  /   __/ \___  >___|  /_______  //_______  /____|_  /\____|__  /
          \/|__|        \/     \/        \/         \/       \/         \/
  
  This library is free software; you can redistribute it and/or modify it under
  the terms of the GNU Lesser General Public License as published by the Free
  Software Foundation; either version 2.1 of the License, or (at your option)
  any later version.
  
  This library is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  details.
  
  You should have received a copy of the GNU Lesser General Public License along
  with this library; if not, write to the Free Software Foundation, Inc., 59
  Temple Place, Suite 330, Boston, MA 02111-1307 USA
  
  
  */
  /*using System;
  using System.Net.Sockets;
  using System.Net;
  using System.Threading;
  using System.IO;
  using System.Text;
  using System.Xml;
  using Microsoft.Win32;
  
  namespace osdrmWallet
  {
  	/// <summary>
  	/// Summary description for WalletClientHdlr.
  	/// </summary>
  	public class WalletClientHdlr
  	{
  		private TcpClient ligCli;
  		Thread thrCli;
  		private string uname, pwd;
  
  		public WalletClientHdlr(TcpClient lig, string uname, string pwd)
  		{
  			//
  			// TODO: Add constructor logic here
  			//
  			ligCli = lig;
  
  			this.uname = uname;
  			this.pwd = pwd;
  
  			thrCli = new Thread(new ThreadStart(this.handleRequest));
  			thrCli.IsBackground = true;
  			thrCli.Start();
  		}
  
  		private void handleRequest()
  		{
  			StreamWriter wCli = null;
  			StreamReader rCli = null;
  
  			try
  			{
  				NetworkStream strLig = ligCli.GetStream();
  				wCli = new StreamWriter(strLig);
  				rCli = new StreamReader(strLig);
  
  				wCli.AutoFlush = true;
  
  				string data=null;
  				data = rCli.ReadLine();
  
  				//char[] _data = data.ToCharArray();
  				// Here we should receive a GETKEY RES_LVL 4 IMG urn:mpeg:mpeg21:diid:doi:pt/adetti/music/00000005
  				//System.Windows.Forms.MessageBox.Show(data.Substring(0,6));
  				if(String.Compare(data.Substring(0,6),"GETKEY")==0 || String.Compare(data.Substring(0,6),"getkey")==0) // This is a valid request
  				{
  					if(String.Compare(data.Substring(7,7),"RES_LVL")==0 || String.Compare(data.Substring(7,7),"res_lvl")==0)
  					{
  						char[] _data = data.ToCharArray(); // convert to chars to extract the image ID and level
  						string RES_LVL = null;
  						// RES_LVL
  						int n=15;
  						do
  						{
  							if(_data[n]!=' ')
  								RES_LVL += _data[n];
  							n++;
  						} while (_data[n]!='I');
  
  						// IMG_ID
  						//RES_LVL=3IMGurn:mpeg:mpeg21:xpto IMG_ID=
  						string IMG_ID = null;
  						for(n=15+RES_LVL.Length+5; n<data.Length; n++)
  						{
  							if(_data[n]!=' ')
  								IMG_ID += _data[n];
  						}
  
  						// now we have the resolution level and the image id
  
  						if (IMG_ID==null)
  						{
  							wCli.Write("ERROR");
  							System.Windows.Forms.MessageBox.Show("The image identifier is empty!!!");
  						}
  						else
  						{
  
  							// the wallet needs to do the following:
  							// 1. Check wheter a license for that image is already on the system
  
  							if(checkLicenseOnSystem(IMG_ID))
  							{
  								// ok, we already have the license on our system
  								// we need to read the license from the file
  								string license = null;
  								CUser cu = new CUser();
  								if(!cu.readUserData(this.uname, this.pwd))
  								{
  									wCli.Write("ERROR");
  									System.Windows.Forms.MessageBox.Show("An error has occured while trying to read the data file.");
  								}
  								else
  								{
  									if(readLicenseFromSystem(IMG_ID, this.uname, this.pwd, ref license))
  									{
  										string key = null;
  										if(checkLicenseValidity(license))
  										{
  											if(getRequestedKey(RES_LVL, license, ref key))
  											{
  												wCli.WriteLine(key);
  											}
  											else
  											{
  												wCli.Write("ERROR");
  											}
  										}
  										else
  										{
  											// delete current license file
  											deleteLicenseFile(IMG_ID);
  											// try to get a new one from the license server
  											wCli.Write("ERROR");
  										}
  									}
  									else
  									{
  										wCli.Write("ERROR");
  									}
  								}
  							}
  							else // we don't have the license file, we need to obtain it remotely from License Server
  							{
  								string license = null;
  								CUser cu = new CUser();
  								if(!cu.readUserData(this.uname, this.pwd))
  								{
  									wCli.Write("ERROR");
  									System.Windows.Forms.MessageBox.Show("An error has occured while trying to read the data file.");
  								}
  								else
  								{
  									//System.Windows.Forms.MessageBox.Show(cu.getUid());
  									if(obtainLicenseFromWS(cu.getUid(), IMG_ID, this.uname, this.pwd, ref license))
  									{
  										//System.Windows.Forms.MessageBox.Show(license);
  										if(createLicenseFile(IMG_ID, this.uname, this.pwd, license))
  										{
  											string key = null;
  											if(checkLicenseValidity(license))
  											{
  												if(getRequestedKey(RES_LVL, license, ref key))
  												{
  													wCli.WriteLine(key);
  												}
  												else
  												{
  													wCli.Write("ERROR");
  												}
  											}
  											else
  											{
  												wCli.Write("ERROR");
  											}
  										}
  										else
  										{
  											wCli.Write("ERROR");
  										}
  									}
  									else
  									{
  										wCli.Write("ERROR");
  									}
  								}
  							}
  						}
  
  						//wCli.WriteLine("RES_LVL="+RES_LVL);
  						//wCli.WriteLine("id_imagem="+IMG_ID);
  					}
  					else // return ERROR
  					{
  						wCli.Write("ERROR");
  					}
  				}
  				else // return ERROR
  				{
  					wCli.Write("ERROR");
  				}
  
  				//System.Windows.Forms.MessageBox.Show(data);
  			}
  			catch (Exception e)
  			{
  				wCli.Write("ERROR");
  				System.Windows.Forms.MessageBox.Show(e.Message);
  				System.Windows.Forms.MessageBox.Show(e.StackTrace);
  			}
  			finally
  			{
  				if(wCli!=null)
  					wCli.Close();
  				if(rCli!=null)
  					rCli.Close();
  				if(ligCli!=null)
  					ligCli.Close();
  			}
  		}
  
  		public bool checkLicenseOnSystem(string iid)
  		{
  			string fname=iid.Replace("/","");
  			string filename=fname.Replace(":","");
  
gt: 			FileInfo licfile = new FileInfo(@".\data\"+filename);