Click here to Skip to main content
15,885,141 members
Articles / Web Development / Apache

The platform-independent code with Mono: Client-server application sample

Rate me:
Please Sign up or sign in to vote.
4.80/5 (23 votes)
24 Mar 2010CPOL10 min read 73.3K   798   59  
This article shows how we can develop the platform-independent software with Mono usage
using System;
using System.Collections.Generic;
using System.Text;

using InfoCenter.Agent.Helpers;

//When LinqToHibernate is implemented 
//we can remove this line and use System.Linq capabilities
using NHibernate.Criterion;
using InfoCenter.Persistence.Core;
using InfoCenter.Persistence.Entities;

namespace InfoCenter.Logic
{
    public class PacketParser
    {
        private IRepository<Computer> computers;
        private IRepository<Process> processes;

        public PacketParser(IRepository<Computer> computersRepository, IRepository<Process> processesRepository)
        {
            computers = computersRepository;
            processes = processesRepository;
        }

        /// <summary>
        /// Add infromation about computer in database
        /// </summary>
        /// <param name="compressedInfo"></param>
        public void AddInfo(string compressedInfo)
        {
            string decompressed = Archiver.Decompress(compressedInfo);
            Agent.Entities.Computer info = (Agent.Entities.Computer) ObjectSerializer.XmlStrToObj<Agent.Entities.Computer>(decompressed);
			
			DetachedCriteria criteria = DetachedCriteria.For<Computer>()
                .Add(NHibernate.Criterion.Expression.Like("Name",info.Name));
			
			Computer oldEntry = computers.FindOne(criteria);
						
			
			
			if(oldEntry != null)
			{
				oldEntry.Name = info.Name;
		        oldEntry.Ip = info.Ip;
		        oldEntry.UserName = info.UserName;
		        oldEntry.OsVersion = info.OsVersion;
		        oldEntry.RecentActivity = DateTime.Now;
				
				computers.Update(oldEntry);
			}
			else
			{
				Computer computer =  new Computer();
				
				computer.Name = info.Name;
		        computer.Ip = info.Ip;
		        computer.UserName = info.UserName;
		        computer.OsVersion = info.OsVersion;
		        computer.RecentActivity = DateTime.Now;
				
            	computers.Add(computer);
			}
        }
		
		
        /// <summary>
        /// Insert process
        /// </summary>
        /// <param name="compName"></param>
        /// <param name="compressedProcesses"></param>
        public void AddProcesses(string compName, string compressedProcesses)
        {
            //decompress input data
            string decompressed = Archiver.Decompress(compressedProcesses);

            //get computer with name 'compName'
            //var computer = computers.FindOne(c => c.Name == compName);
			
			DetachedCriteria criteria = DetachedCriteria.For<Computer>()
                .Add(NHibernate.Criterion.Expression.Like("Name",compName));
			Computer computer = computers.FindOne(criteria);
				
            //TODO Need to log this situation
            if (computer == null)
                return;

            //remove old processes
            //processes.Remove(p => p.Name == compName);
			DetachedCriteria procCriteria = DetachedCriteria.For<Process>().CreateAlias("Host","c")
                .Add(NHibernate.Criterion.Expression.Like("c.Name","ILYA"));
			
			processes.Remove(procCriteria);

            //convert from xml to agent.process collection
			
			
			IList<Agent.Entities.Process> procInfo = new List<Agent.Entities.Process>();
			foreach(Agent.Entities.Process proc in 
			        ObjectSerializer.XmlStrToObj<Agent.Entities.Process[]>(decompressed))
			{
				procInfo.Add(proc);
			}
            //IList<Agent.Entities.Process> procInfo = 
            //   ObjectSerializer.XmlStrToObj<Agent.Entities.Process[]>(decompressed). ToList();

            //convert and add to database
            foreach (Agent.Entities.Process p in procInfo)
			{
				Process process = new Process();
				process.Name = p.Name;
				process.Pid = p.Pid;
				process.Host = computer;
                processes.Add(process);
			}
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Nokia
Germany Germany
Interested in design/development of framework functionality using the best patterns and practices.

Comments and Discussions