Re[2]: Настройка .NET Remoting
От: valia  
Дата: 31.10.08 12:09
Оценка:
Здравствуйте, HowardLovekraft, Вы писали:

HL>Можно увидеть полную версию исходного кода для сервера и для клиента?


Боюсь, что абсолютно полную версию исходного кода, не получится увидеть, т.к. он достаточно большой, но участки, где непосредственно происходит работа с ремотинг, вполне:
сервер:
using System;
using System.Collections.Generic;
using System.ServiceProcess;
using System.Text;
using System.Reflection;
using System.Configuration.Install;
using Remoting; //пространство имен, содержащее класс, наследуемый от MarshalByRefObject
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;

namespace Integrator
{
    [System.ComponentModel.RunInstallerAttribute(true)]
    public class InstallService : System.Configuration.Install.Installer
    {
        private System.ServiceProcess.ServiceInstaller _SInst = new ServiceInstaller();
        private System.ServiceProcess.ServiceProcessInstaller _SPInst = new ServiceProcessInstaller();

        public InstallService()
        {
            _SPInst.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
            _SInst.DisplayName = "Metacluster integrator";
            _SInst.ServiceName = "Metacluster integrator";
            _SInst.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
            this.Installers.Add(_SPInst);
            this.Installers.Add(_SInst);
        }


        public override void Uninstall(System.Collections.IDictionary savedState)
        {
            System.ServiceProcess.ServiceController _sc = new ServiceController("Metacluster integrator", System.Environment.MachineName);
            if (_sc.CanStop) _sc.Stop();
            _sc.Close();
            base.Uninstall(savedState);
        }
    }

    static class Program
    {
        static bool m_test;
        static bool m_install;
        static bool m_uninstall;

        static int ShowHelp()
        {
            System.Console.Write("Usage: integrator.exe [parameter1]\n\n");

            System.Console.Write("Parameter1:\n");
            System.Console.Write("-install - install service\n");
            System.Console.Write("-uninstall - uninstall service\n");
            System.Console.Write("-test - run the program in test mode\n\n");
            return 1;
        }

        static void Main(string[] args)
        {
          System.IO.Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
            if (!Utilities.ProcessCommandArguments(args, ref m_test, ref m_install, ref m_uninstall))
            {
                ShowHelp();
                return;
            }

            if (m_install)
            {
                try
                {
                    ManagedInstallerClass.InstallHelper(
                        new string[] { Assembly.GetExecutingAssembly().Location });
                }
                catch(System.Exception ex)
                {
                    System.Console.WriteLine(ex.Message);
                }
                return;
            }

            if (m_uninstall)
            {
                try
                {
                    ManagedInstallerClass.InstallHelper(
                        new string[] { "/u", Assembly.GetExecutingAssembly().Location });
                }
                catch (System.Exception ex)
                {
                    System.Console.WriteLine(ex.Message);
                }
                return;
            }

            if (m_test)
            {
                RemotingIntegrator rIntegrator = RemotingIntegrator.ConfigureRemotingIntegrator();//статический метод, в котором происходит                                                                                                            //конфигурирование .NET Remoting
                Metacluster.Task task = new Metacluster.Task(); //Task - сериализуемый тип, содержащийся в пространстве Metacluster
                rIntegrator.AddTask(task); //метод, в котором осуществляется добавление в ArrayList
                rIntegrator.field = 100000;//целочисленное поле
                Console.WriteLine(rIntegrator.field);
                Console.ReadLine();
                return;
            }

            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] { new IntegratorService() };

            ServiceBase.Run(ServicesToRun);
        }
    }
}


клиент:
using System.Configuration;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml;
using Remoting;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System;
using System.Collections;
 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1),
WebService(Namespace = "http://gw-114.unn.ac.ru/metaclusterws/")]
public class Service : System.Web.Services.WebService
{
    static RemotingIntegrator rIntegrator;

    static Service()
    {
        HttpChannel channel = new HttpChannel();
        ChannelServices.RegisterChannel(channel);
        object remoteObj = Activator.GetObject(typeof(Remoting.RemotingIntegrator),
                                               "http://localhost:4000/IntegratorURI");
        rIntegrator = (RemotingIntegrator)remoteObj;
    }

    public Service()
    {
    }

    [WebMethod]
    public string AddTest()
    {
        Metacluster.Task task = new Metacluster.Task();
        rIntegrator.AddTask(task);
        return ((Metacluster.Task)rIntegrator.m_RTaskPool[0]).m_ActiveDir;
    }
}

пространство имён Remoting:
using System;
using System.Collections.Generic;
using System.Text;
using Metacluster;
using System.Collections;

namespace Remoting
{
  public class Remoting : MarshalByRefObject
  {
    public ArrayList m_RTaskPool;
    public int field;

    public Remoting() : base() 
    {
        m_RTaskPool = new ArrayList();
    }

    // methods
    public bool AddTask(object task)
    {
      bool resAdd = false;
      int index = -1;
      lock (m_RTaskPool)
      {
        index = m_RTaskPool.Add(task);
      }
      if (index >= 0) resAdd = true;
      return resAdd;
    }

    public override object InitializeLifetimeService()
    {
      return null;
    }

  }
}

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;


namespace Remoting
{
  public class RemotingIntegrator : Remoting
  {
    public RemotingIntegrator() : base() { }

    // Configure infrastructure .NET Remoting
    public static RemotingIntegrator ConfigureRemotingIntegrator()
    {
        int port = 4000;
        HttpChannel channel = new HttpChannel(port);
        ChannelServices.RegisterChannel(channel);
        RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemotingIntegrator), "IntegratorURI", WellKnownObjectMode.Singleton);
        RemotingIntegrator rIntegrator = new RemotingIntegrator();
        RemotingServices.Marshal(rIntegrator, "IntegratorURI");
        return rIntegrator;
    }
  }
}


пространство имён Metacluster:
task.h:
#ifndef __TASK_H__
#define __TASK_H__

using namespace System;

namespace Metacluster
{
[Serializable]
public ref class Task
{
public:
Task(void);
Task(System::String ^_user, System::String ^_name, System::String ^_module_name,
System::String ^ _output_redir, int _priority, Int64 _id, int _user_id, int _nproc, System::String ^_cmd,
Int64 _mode);
Task(const Task ^task);
virtual ~Task();

Task ^operator=(const Task ^task);
System::String ^ Pack();

Int64 m_Id;
Int64 m_Mode;
Int64 m_CCSJobId;

System::String ^m_Name;
System::String ^m_User;
System::String ^m_ModuleName;
System::String ^m_CommandLine;
System::String ^m_OutputRedirection;

int m_UserId;
int m_Priority;
int m_Nproc;

System::String ^m_ActiveDir;
System::Array ^m_BusyMachines;

System::DateTime ^m_StartTime;
System::DateTime ^m_StopTime;
System::DateTime ^m_AddTime;
System::DateTime ^m_DeleteTime;

bool m_IsStoppingRequest;

///If task was submitted to CCS but not started
///it's state is RUNNING but m_queuedInCCS is true
bool m_queuedInCCS;

protected:

private:

};
}
#endif

task.cpp:
#include "stdafx.h"
#include "time.h"
#include <stdlib.h>
#include <memory.h>
#include "Task.h"

using namespace System;
using namespace System::Xml;
using namespace Metacluster;

Task::Task(void)
{
m_OutputRedirection = "";
m_User = "";
m_Name = "";
m_ModuleName = "";
m_Priority = 0;
GenerateId();
m_CCSJobId = 0;
m_UserId = 0;
m_Nproc = 1;
m_CommandLine = "";

m_ActiveDir = ".\\";

m_StartTime = gcnew DateTime(0);
m_StopTime = gcnew DateTime(0);
m_AddTime = gcnew DateTime(0);
m_DeleteTime = gcnew DateTime(0);

m_IsStoppingRequest = false;
m_queuedInCCS = false;
};


Task::Task(System::String ^_user, System::String ^_name, System::String ^_module_name,
System::String ^ _output_redir, int _priority, Int64 _id, int _user_id, int _nproc, System::String ^_cmd,
Int64 _mode)
{
m_User = _user;
m_Name = _name;
m_ModuleName = _module_name;
m_OutputRedirection = _output_redir;
m_Priority = _priority;
m_CCSJobId = 0;
m_Id = _id;
m_UserId = _user_id;
m_Nproc = _nproc;
m_CommandLine = _cmd;
m_State = _state;

m_Mode = _mode;

m_ActiveDir = ".\\";

m_StartTime = gcnew DateTime(0);
m_StopTime = gcnew DateTime(0);
m_AddTime = gcnew DateTime(0);
m_DeleteTime = gcnew DateTime(0);
};

Task::Task(const Task ^task): m_BusyMachines(task->m_BusyMachines)
{
m_User = task->m_User;
m_Name = task->m_Name;
m_ModuleName = task->m_ModuleName;
m_OutputRedirection = task->m_OutputRedirection;
m_Priority = task->m_Priority;
m_UserId = task->m_UserId;
m_Id = task->m_Id;
m_CCSJobId = task->m_CCSJobId;
m_Nproc = task->m_Nproc;
m_CommandLine = task->m_CommandLine;
m_ActiveDir = task->m_ActiveDir;
m_StartTime = task->m_StartTime;
m_StopTime = task->m_StopTime;
m_AddTime = task->m_AddTime;
m_DeleteTime = task->m_DeleteTime;
m_IsStoppingRequest = task->m_IsStoppingRequest;
}

Task::~Task()
{
}


Task ^Task::operator=(const Task ^task)
{
m_User = task->m_User;
m_Name = task->m_Name;
m_ModuleName = task->m_ModuleName;
m_OutputRedirection = task->m_OutputRedirection;
m_Priority = task->m_Priority;
m_UserId = task->m_UserId;
m_Id = task->m_Id;
m_CCSJobId = task->m_CCSJobId;
m_Nproc = task->m_Nproc;
m_CommandLine = task->m_CommandLine;
m_ActiveDir = task->m_ActiveDir;
m_StartTime = task->m_StartTime;
m_StopTime = task->m_StopTime;
m_AddTime = task->m_AddTime;
m_DeleteTime = task->m_DeleteTime;
m_BusyMachines = task->m_BusyMachines;
m_IsStoppingRequest = task->m_IsStoppingRequest;
m_queuedInCCS = task->m_queuedInCCS;
return this;
}

Вроде бы остальное не используется...

Заранее спасибо
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.