Added notification tests.

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@2681 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Ingo Weinhold 2003-02-10 00:24:51 +00:00
parent 30bb74bbd2
commit 77d6501f22

View File

@ -3,16 +3,23 @@
// by the OpenBeOS license.
//---------------------------------------------------------------------
#include <stdio.h>
#include <string.h>
#include <Application.h>
#include <DiskDevice.h>
#include <DiskDeviceRoster.h>
#include <Message.h>
#include <Messenger.h>
#include <Partition.h>
#include <Path.h>
#include <OS.h>
#include <Session.h>
#include <Messenger.h> // needed only until we can link against
#include <RegistrarDefs.h> // libopenbeos
#include <Roster.h> //
#include <RosterPrivate.h> //
// needed only until we can link against libopenbeos
#include <RegistrarDefs.h>
#include <Roster.h>
#include <RosterPrivate.h>
// Hack to make BDiskDeviceRoster communicate with our registrar.
@ -63,7 +70,9 @@ init_roster()
}
}
//----------------------------------------------------------------------------
// DumpVisitor
class DumpVisitor : public BDiskDeviceVisitor {
public:
virtual bool Visit(BDiskDevice *device)
@ -108,13 +117,119 @@ public:
}
};
// TestApp
class TestApp : public BApplication {
public:
TestApp(const char *signature)
: BApplication(signature)
{
}
virtual void MessageReceived(BMessage *message)
{
printf("TestApp::MessageReceived(%.4s)\n", (char*)&message->what);
switch (message->what) {
case B_DEVICE_UPDATE:
{
uint32 event;
if (message->FindInt32("event", (int32*)&event) == B_OK) {
switch (event) {
case B_DEVICE_MOUNT_POINT_MOVED:
MountPointMoved(message);
break;
case B_DEVICE_PARTITION_MOUNTED:
PartitionMounted(message);
break;
case B_DEVICE_PARTITION_UNMOUNTED:
PartitionUnmounted(message);
break;
case B_DEVICE_PARTITION_CHANGED:
case B_DEVICE_PARTITION_ADDED:
case B_DEVICE_PARTITION_REMOVED:
case B_DEVICE_MEDIA_CHANGED:
case B_DEVICE_ADDED:
case B_DEVICE_REMOVED:
printf("unhandled event\n");
message->PrintToStream();
break;
}
}
break;
}
default:
BApplication::MessageReceived(message);
break;
}
}
void MountPointMoved(BMessage *message)
{
printf("TestApp::MountPointMoved()\n");
PrintPartitionInfo(message);
entry_ref oldRoot, newRoot;
BPath oldPath, newPath;
if (message->FindRef("old_directory", &oldRoot) == B_OK
&& message->FindRef("new_directory", &newRoot) == B_OK
&& oldPath.SetTo(&oldRoot) == B_OK
&& newPath.SetTo(&newRoot) == B_OK) {
printf("old mount point: `%s'\n", oldPath.Path());
printf("new mount point: `%s'\n", newPath.Path());
}
}
void PartitionMounted(BMessage *message)
{
printf("TestApp::PartitionMounted()\n");
PrintPartitionInfo(message);
}
void PartitionUnmounted(BMessage *message)
{
printf("TestApp::PartitionUnmounted()\n");
PrintPartitionInfo(message);
}
void PrintPartitionInfo(BMessage *message)
{
int32 deviceID;
int32 sessionID;
int32 partitionID;
if (message->FindInt32("device_id", &deviceID) == B_OK
&& message->FindInt32("session_id", &sessionID) == B_OK
&& message->FindInt32("partition_id", &partitionID) == B_OK) {
BDiskDeviceRoster roster;
BDiskDevice device;
BPartition *partition;
if (roster.GetPartitionWithID(partitionID, &device, &partition)
== B_OK) {
DumpVisitor().Visit(partition);
}
}
}
};
// main
int
main()
{
init_roster();
// -----------
TestApp app("application/x-vnd.obos-disk-device-test");
BDiskDeviceRoster roster;
DumpVisitor visitor;
roster.Traverse(&visitor);
status_t error = B_OK;
error = roster.StartWatching(BMessenger(&app));
if (error != B_OK)
printf("start watching failed: %s\n", strerror(error));
if (error == B_OK)
app.Run();
error = roster.StopWatching(BMessenger(&app));
if (error != B_OK)
printf("stop watching failed: %s\n", strerror(error));
return 0;
}