Mercurial > projects > hoofbaby
diff src/platif/hoofbaby.cpp @ 0:3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
author | fraserofthenight |
---|---|
date | Mon, 06 Jul 2009 08:06:28 -0700 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/platif/hoofbaby.cpp Mon Jul 06 08:06:28 2009 -0700 @@ -0,0 +1,91 @@ +/** + * Hoofbaby -- http://www.dsource.org/projects/hoofbaby + * Copyright (C) 2009 Robert Fraser + * + * This program is free software; you can redistribute it andor + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program 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 General Public License for more details. + */ + +#include "platif.h" +#include "PltUPnP.h" +#include "PltMediaConnect.h" +#include "PltHttpServer.h" +#include "NptStreams.h" +#include <stdlib.h> +#include <stdio.h> + +static Hoofbaby hoofbaby; + +class HB_InputStream : public NPT_InputStream +{ +private: + void* ctx; + +public: + HB_InputStream(void* _ctx) : ctx(_ctx) {} + NPT_Result Read(void* buffer, NPT_Size bytes_to_read, NPT_Size* bytes_read = 0) { return hoofbaby.Stream_Read(ctx, buffer, bytes_to_read, bytes_read); } + NPT_Result Seek(NPT_Position offset) { return hoofbaby.Stream_Seek(ctx, offset); } + NPT_Result Tell(NPT_Position& offset) { return hoofbaby.Stream_Tell(ctx, &offset); } + NPT_Result GetSize(NPT_LargeSize& size) { return hoofbaby.Stream_GetSize(ctx, &size); } + NPT_Result GetAvailable(NPT_LargeSize& available) { return hoofbaby.Stream_GetAvailable(ctx, &available); } +}; + +class HB_Server : public PLT_MediaConnect +{ + public: + HB_Server(const char* _path, const char* _friendly_name) : PLT_MediaConnect(_path, _friendly_name) {} + NPT_Result ServeFile(NPT_HttpRequest& request, const NPT_HttpRequestContext& context, + NPT_HttpResponse& response, const NPT_String& file_path) + { + NPT_COMPILER_UNUSED(context); + + printf("**********************************\n"); + printf("ServeFile(%s)\n", file_path.GetChars()); + printf("**********************************\n"); + fflush(stdout); + + NPT_Position start, end; + PLT_HttpHelper::GetRange(request, start, end); + + return PLT_FileServer::ServeFile(response, + file_path, + start, + end, + !request.GetMethod().Compare("HEAD")); + } +}; + +void initHoofbaby(Hoofbaby* interf) +{ + hoofbaby = *interf; +} + +int startServer(const char* path) +{ + PLT_DeviceHostReference device(new HB_Server("D:\\Media\\Videos", "Platinum: Hoofbaby: ")); + + PLT_UPnP upnp; + upnp.AddDevice(device); + + if (NPT_FAILED(upnp.Start())) + return 1; + + char buf[256]; + while (gets(buf)) + { + if (*buf == 'q') + { + break; + } + } + + upnp.Stop(); + return 0; +}