The Ripper Application
Ripper.ice
#include <Ice/BuiltinSequences.ice>
module Ripper {
exception EncodingFailedException {
string reason;
};
sequence<short> Samples;
interface Mp3Encoder {
// Input: PCM samples for left and right channels
// Output: MP3 frame(s).
Ice::ByteSeq encode(Samples leftSamples, Samples rightSamples)
throws EncodingFailedException;
// You must flush to get the last frame(s). Flush also
// destroys the encoder object.
Ice::ByteSeq flush()
throws EncodingFailedException;
};
interface Mp3EncoderFactory
{
Mp3Encoder* createEncoder();
};
};
MusicServer.cpp
#include <Ice/Ice.h>
#include "Ripper.h"
using namespace Ripper;
#include <iostream>
using namespace std;
class CMp3Encoder : public Mp3Encoder{
public:
virtual ::Ice::ByteSeq encode(const ::Ripper::Samples& leftSamples, const ::Ripper::Samples& rightSamples, const ::Ice::Current& curr) {
std::vector<unsigned char> vChar;
vChar.push_back('M'); vChar.push_back('u');vChar.push_back('s');vChar.push_back('i');vChar.push_back('c');
for(std::vector<unsigned char>::iterator it = vChar.begin(); it != vChar.end(); it++) {
cout << (*it);
}
cout << endl;
return vChar;
}
virtual ::Ice::ByteSeq flush(const ::Ice::Current& c){
cout << "Music" << endl;
std::vector<unsigned char> vChar;vChar.clear();
vChar.push_back('B'); vChar.push_back('e'); vChar.push_back('y'); vChar.push_back('!');
return vChar;
}
};
class CMp3EncoderFactory : public Mp3EncoderFactory{
public:
virtual ::Ripper::Mp3EncoderPrx createEncoder(const ::Ice::Current& curr){
//适配器通过标识符“Mp3Encoder”来查找;
//通过checkedCast来转换
Ice::Identity oId; oId.name = "Mp3Encoder";
Mp3EncoderPrx spMp3Encoder = Mp3EncoderPrx::checkedCast(curr.adapter->createProxy(oId));
return spMp3Encoder;
}
};
int main(int argc, char* argv[])
{
int iStatus = 0;//程序的退出时的状态,就是否成功执行
Ice::CommunicatorPtr spCommunicator;//来包含Ice run time 的主句柄
cout << "~~~~~~~~~~服务器开始启动~~~~~~~~~~~~~" << endl;
try{
spCommunicator = Ice::initialize(argc, argv);
Ice::PropertiesPtr props = spCommunicator->getProperties();
string sAdapter = props->getProperty("EncoderAdapter.AdapterId");
cout << "Adapter : " << sAdapter << endl;
Ice::ObjectAdapterPtr pAdapter= spCommunicator->createObjectAdapter(sAdapter);
//此处不创建不添加:后期工厂函数返回的Mp3Encoder代理对象不存在
Ice::ObjectPtr pMp3Encoder = new CMp3Encoder;
pAdapter->add(pMp3Encoder, spCommunicator->stringToIdentity("Mp3Encoder"));
Ice::ObjectPtr pMp3EncoderFactory = new CMp3EncoderFactory;
pAdapter->add(pMp3EncoderFactory, spCommunicator->stringToIdentity("factory"));
pAdapter->activate();
spCommunicator->waitForShutdown();
}
catch (const Ice::Exception& e){
cerr << e << endl;
iStatus = 1;
}
catch (const char* msg) {
cerr << msg << endl;
iStatus = 1;
}
if (spCommunicator){
try{
spCommunicator->destroy();
}
catch (const Ice::Exception& e) {
cerr << e << endl;
iStatus = 1;
}
}
return iStatus;
}
MusicClient.cpp
#include <Ice/Ice.h>
#include "Ripper.h"
using namespace Ripper;
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main(int argc, char * argv[])
{
int status = 0;
Ice::CommunicatorPtr spCommunicator;
cout << "~~~~~~~~~~~客户端开始测试~~~~~~~~~~~~~~~~~~" << endl;
try{
spCommunicator = Ice::initialize(argc, argv);
//Ice::ObjectPrx pProxy = spCommunicator->stringToProxy("SimplePrinter:default -p 10000");
//CAbsPrinterPrx pPrinter = CAbsPrinterPrx::checkedCast(pProxy);
//if (!pPrinter) throw "Invalid proxy";
Ice::ObjectPrx proxy = spCommunicator->stringToProxy("factory@EncoderAdapter");
cout << "第一步:获取工厂代理" << endl;
Ripper::Mp3EncoderFactoryPrx factory = Ripper::Mp3EncoderFactoryPrx::checkedCast(proxy);
if (!factory) throw "Invalid factory";
cout << "第二步:通过工厂函数获取压缩对象" << endl;
Ripper::Mp3EncoderPrx encoder = factory->createEncoder();
if (!encoder) throw "Invalid encoder";
std::vector<unsigned char> vChar = encoder->flush();
for(std::vector<unsigned char>::iterator it = vChar.begin(); it != vChar.end(); it++) {
cout << (*it);
}
cout << endl;
}
catch (const Ice::Exception & ex){
cerr << ex << endl;
status = 1;
}
catch (const char * msg){
cerr << msg << endl;
status = 1;
}
if (spCommunicator) spCommunicator->destroy();
system("Pause");
return status;
}
Initial Ripper Architecture
Ripper Registry Configuration
IceGrid.Registry.Client.Endpoints=tcp -p 4061
IceGrid.Registry.Server.Endpoints=tcp
IceGrid.Registry.Internal.Endpoints=tcp
IceGrid.Registry.AdminPermissionsVerifier=IceGrid/NullPermissionsVerifier
IceGrid.Registry.Data=D:\Projects\IceMusic\Release\db\registry
IceGrid.Registry.DynamicRegistration=1
Ripper Server Configuration
EncoderAdapter.AdapterId=EncoderAdapter
EncoderAdapter.Endpoints=tcp
Ice.Default.Locator=IceGrid/Locator:tcp -h localhost -p 4061
Ripper Client Configuration
Ice.Default.Locator=IceGrid/Locator:tcp -h localhost -p 4061
Starting the Registry for the Ripper Application
>icegridregistry --Ice.Config=registry.cfgStarting the Ripper Server
>MusicServer --Ice.Config=server.cfgStarting the Client
>MusicClient.exe --Ice.Config=client.cfg