From 4455f219ee20df3ac1f85539de7fc43fa335f4b0 Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Sun, 28 Jul 2002 19:28:09 +0000 Subject: [PATCH] Added a "shell" for the roster. It can list, activate and quit application. git-svn-id: file:///srv/svn/repos/haiku/trunk/current@511 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/tests/servers/registrar/Jamfile | 6 + src/tests/servers/registrar/RosterShell.cpp | 223 ++++++++++++++++++++ 2 files changed, 229 insertions(+) create mode 100644 src/tests/servers/registrar/RosterShell.cpp diff --git a/src/tests/servers/registrar/Jamfile b/src/tests/servers/registrar/Jamfile index 08944d0c98..35fec4aaed 100644 --- a/src/tests/servers/registrar/Jamfile +++ b/src/tests/servers/registrar/Jamfile @@ -7,3 +7,9 @@ SimpleTest RegistrarTest1 : libopenbeos.so be ; +SimpleTest RosterShell + : RosterShell.cpp + : libopenbeos.so + be stdc++.r4 +; + diff --git a/src/tests/servers/registrar/RosterShell.cpp b/src/tests/servers/registrar/RosterShell.cpp new file mode 100644 index 0000000000..0102f86250 --- /dev/null +++ b/src/tests/servers/registrar/RosterShell.cpp @@ -0,0 +1,223 @@ +// RosterShell.cpp + +#include +#include +#include +#include +#include + +#include +#include +#include + +const char kShellUsage[] = +"Commands Parameters Description\n" +"---------------------------------------------------------------------------\n" +"activate, a [ ] activates the application specified by \n" +"exit, e exits the roster shell\n" +"help, h prints this help\n" +"list, l [ ] lists the applications specified by \n" +" or all registered applications\n" +"list-long, ll [ ] lists the applications specified by \n" +" or all registered applications (long format)\n" +"quit, q [ ] quits the applications specified by \n" +"\n" +; + +class Shell { +public: + Shell() + : fTerminating(false) + { + } + + bool IsTerminating() const { return fTerminating; } + + void DoCommand(vector &cmdLine) + { + if (cmdLine.size() > 0) { + string command = cmdLine[0]; + if (command == "activate" || command == "a") + CmdActivate(cmdLine); + else if (command == "exit" || command == "e") + CmdExit(cmdLine); + else if (command == "help" || command == "h") + Usage(); + else if (command == "list" || command == "l") + CmdList(cmdLine); + else if (command == "list-long" || command == "ll") + CmdListLong(cmdLine); + else if (command == "quit" || command == "q") + CmdQuit(cmdLine); + else + Usage(string("Unknown command `") + command + "'"); + } + } + + void Usage(string error = "") + { + if (error.length() > 0) + cout << error << endl; + cout << kShellUsage; + } + + void CmdActivate(vector &args) + { + BRoster roster; + // get a list of team IDs + BList teamList; + if (args.size() <= 1) { + printf("activate: requires exactly one argument\n"); + return; + } + ParseTeamList(args, &teamList); + int32 count = teamList.CountItems(); + if (count != 1) { + printf("activate: requires exactly one argument\n"); + return; + } + // activate the team + team_id team = (team_id)teamList.ItemAt(0); + status_t error = roster.ActivateApp(team); + if (error != B_OK) { + printf("activate: failed to activate application %ld: %s\n", + team, strerror(error)); + } + } + + void CmdExit(vector &) + { + fTerminating = true; + } + + static void ParseTeamList(vector &args, BList *teamList) + { + for (int32 i = 1; i < (int32)args.size(); i++) { + string arg = args[i]; + team_id team = -1; + if (sscanf(arg.c_str(), "%ld", &team) > 0) + teamList->AddItem((void*)team); + } + } + + void CmdList(vector &args) + { + BRoster roster; + // get a list of team IDs + BList teamList; + if (args.size() > 1) + ParseTeamList(args, &teamList); + else + roster.GetAppList(&teamList); + // print an info for each team + int32 count = teamList.CountItems(); + printf("%-8s%-40s\n", "team", "signature"); + printf("---------------------------------------------------------\n"); + for (int32 i = 0; i < count; i++) { + team_id team = (team_id)teamList.ItemAt(i); + app_info info; + status_t error = roster.GetRunningAppInfo(team, &info); + if (error == B_OK) + printf("%-8ld%-40s\n", team, info.signature); + else { + printf("%-8ldfailed to get the app_info: %s\n", team, + strerror(error)); + } + } + printf("\n"); + } + + void CmdListLong(vector &args) + { + BRoster roster; + // get a list of team IDs + BList teamList; + if (args.size() > 1) + ParseTeamList(args, &teamList); + else + roster.GetAppList(&teamList); + // print an info for each team + int32 count = teamList.CountItems(); + for (int32 i = 0; i < count; i++) { + team_id team = (team_id)teamList.ItemAt(i); + printf("team %8ld\n", team); + printf("-------------\n"); + app_info info; + status_t error = roster.GetRunningAppInfo(team, &info); + if (error == B_OK) { + printf("signature: %s\n", info.signature); + printf("thread: %ld\n", info.thread); + printf("port: %ld\n", info.port); + printf("flags: 0x%lx\n", info.flags); + printf("file: %s\n", BPath(&info.ref).Path()); + } else + printf("failed to get the app_info: %s\n", strerror(error)); + printf("\n"); + } + } + + void CmdQuit(vector &args) + { + BRoster roster; + // get a list of team IDs + BList teamList; + if (args.size() <= 1) { + printf("quit: requires at least one argument\n"); + return; + } + ParseTeamList(args, &teamList); + int32 count = teamList.CountItems(); + if (count < 1) { + printf("quit: requires at least one argument\n"); + return; + } + // send a B_QUIT_REQUESTED message to each team + for (int32 i = 0; i < count; i++) { + team_id team = (team_id)teamList.ItemAt(i); + BMessenger messenger(NULL, team); + if (messenger.IsValid()) { + status_t error = messenger.SendMessage(B_QUIT_REQUESTED); + if (error != B_OK) { + printf("quit: failed to deliver the B_QUIT_REQUESTED " + "message to team %ld\n", team); + printf(" %s\n", strerror(error)); + } + } else + printf("quit: could create a messenger for team %ld\n", team); + } + } + +private: + bool fTerminating; +}; + + +// read_line +bool +read_line(char *line, size_t size) +{ + cout << "roster> "; + cout.flush(); + return cin.getline(line, size); +} + +// main +int +main() +{ + Shell shell; + // main input loop + char line[10240]; + while (!shell.IsTerminating() && read_line(line, sizeof(line))) { + // parse args + istrstream in(line); + vector args; + string arg; + while (in >> arg) + args.push_back(arg); + // invoke command + shell.DoCommand(args); + } + return 0; +} +